怎样发送按下"F6"键这个消息给一个已知句柄??
应该是SendMessage(Hwnd,Wm_keydown,wparam,lparam)吧?但是后两个参数不会写.

解决方案 »

  1.   

    后两个参数不太重要!主要就是你前面的两个参数SendMessage(句柄,消息名,主参数,辅助参数)用PostMessage也可以 参数也差不多
      

  2.   

    Borland的工程师们早就提供了一个方便的方法:在Delphi5.0企业版的安装盘的\info\Extras\SendKeys\文件夹中,有一个sndKey32.Pas文件, 
    只需把该文件复制到Delphi的安装目录下\Lib\文件夹中,在工程中引用该文件后即可使用SendKeys函数. 
    其中两个主要的函数为: 
    SendKeys(KeyString:Pchar;Wait:Boolean):Boolean; 
    AppActivate(WindowName:Pchar):Boolean; 
    用法为: 
    SendKeys函数向当前拥有焦点的窗口或控件发送字符串,KeySering为字符串的内容。 
    Wait指示是否等待接收字符串的窗口或控件处理完毕后返回。一般设为False即可。 
    例如:SendKeys('abcdefg',false); 
    值得一提的是,SendKeys函数支持发送特殊字符和组合字符,例如方向键、Alt、Ctrl组合键。 
    使用时,只需用规定的前缀指明即可。+为Shift,^为Ctrl,%为Alt。 
    例如: 
    '+monday ' 意为发送Shift+m和onday 
            '+(monday)'意为发送Shift+monday 
    对于不可见字符(比如方向键、F1~F11、回车等),可用{}将其括起来。 
      例如: 
    '%{F4}'即发送Alt+F4的组合送给应用程序。 
    更详尽的说明请参照SndKey32.pas文件中的注释。 
    AppActivate函数的作用是将某个窗口设置为当前窗口。只需将该窗口的标题传过去即可。 
      若成功将某个窗口激活,则返回值为True. 
      
      例子:点击发送后,上面的Edit控件的内容会被发送到下面的Edit控件里面去。 
      程序很简单,只要两个Edit控件和一个Button控件。 
    源程序如下: 
    unit sendkey1; 
    interface 
    uses 
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
      Dialogs,sndkey32, StdCtrls;      //注意该行要添加sndker32!! 
    type 
      TForm1 = class(TForm) 
        Edit1: TEdit; 
        Edit2: TEdit; 
        Button1: TButton; 
        procedure Button1Click(Sender: TObject); 
      private 
        { Private declarations } 
      public 
        { Public declarations } 
      end; 
    var 
      Form1: TForm1; implementation 
    {$R *.dfm} 
    procedure TForm1.Button1Click(Sender: TObject); 
    begin 
    Edit2.SetFocus;  //将焦点置于Edit2 
    SendKeys(Pchar(Edit1.Text),False);  //将Edit1中的字符串发送给Edit2 
                                   //事先需进行转换 
    end; end. 很简单,是不是?其实不仅Edit控件,像Memo、RichEdit等空件均可接收发送的字符。 
      上面的例子只是在本应用程序内传递字符串, 
    下面的例子是将字符串发送给Microsoft Word。其所用控件与上例相差不大。 
    源程序为: 
    unit unit1; 
    interface 
    uses 
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs 
      ,sndkey32, StdCtrls;    //不要忘了sndKey32! 
    type 
      TForm1 = class(TForm) 
        Button1: TButton; 
        Edit1: TEdit; 
        Button2: TButton; 
        procedure Button1Click(Sender: TObject); 
        procedure Button2Click(Sender: TObject); 
      private 
        { Private declarations } 
      public 
        { Public declarations } 
      end; 
    var 
      Form1: TForm1; 
    implementation {$R *.DFM} 
    { 该函数将焦点置于Word,若失败,返回False} 
    function SetFocusToWord:boolean;    
    var 
       f:boolean; 
    begin 
       f:=true; 
       if not AppActivate('文档 1 - Microsoft Word') then 
      { 
          字符串内容为:文档空格1空格-空格Microsoft空格Word 
       } 
       begin 
            f:=False; 
            MessageDlg('没找到Word!',mtError,[mbOk],0); //显示错误 
            exit; 
       end; 
    result:=f; 
    end; 
    procedure SetFormActive; //该子程序将焦点置回 
    begin 
       Appactivate('SendToWord'); 
       form1.SetFocus; 
       form1.Edit1.SetFocus; 
    end; 
    procedure TForm1.Button1Click(Sender: TObject); 
    begin 
       if not SetFocusToWord then exit; //没找到Word,退出 
       sendkeys(Pchar(form1.edit1.text),false); //找到了,发送字符串 
       SetFormActive; 
    end; procedure TForm1.Button2Click(Sender: TObject); 
    begin 
    if not SetFocusToWord then exit; //没找到Word,退出 
       sendkeys('%{F4}',false);    //发送Alt+F4,关闭Word 
       SetFormActive; 
    end; 
    end. 
      

  3.   

    请问Delphi7里面有这些函数没有呀?我没找到有sndkey32单元在那里能找到,谢谢!
      

  4.   

    http://home.conceptsfa.nl/~gvdvenis/downloads/sndkey32.pas