转贴:(可能对你有帮助)<br>=============<br>其实这个程序主要的内容不是如何发qq信息<br>而是如何找到某一个正在运行的应用程序窗口上任意一个控件的句柄<br>我的方法是(以一个button为例)<br>用FindWindow找到窗体的句柄后<br>根据按钮的类别,循环找这个窗体上的每个'Button',然后用GetWindowText和Button的Caption比较,就可以找到想要的按钮句柄了<br><br>下面贴出我的程序,望大家批评指教<br>其中部分代码参考大富翁上某一帖子(忘了是哪个了)<br><br>To wjiachun:在下只想和大家交流交流,没敢要分<br>何况。。。这东西那么“土”。。。。 <br><br>unit OicqSendForm;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls, Buttons, ExtCtrls;<br><br>type<br> TfrmMain = class(TForm)<br> memText: TMemo;<br> edtTime: TEdit;<br> Label1: TLabel;<br> Bevel1: TBevel;<br> Label2: TLabel;<br> bbtnStart: TBitBtn;<br> bbtnEnd: TBitBtn;<br> bbtnHelp: TBitBtn;<br> Timer: TTimer;<br> procedure bbtnStartClick(Sender: TObject);<br> procedure bbtnEndClick(Sender: TObject);<br> procedure TimerTimer(Sender: TObject);<br> procedure bbtnHelpClick(Sender: TObject);<br> private<br> { Private declarations }<br> FTextHandle: HWND; //qq消息输入框句柄<br> FButtonHandle: HWND; //发送按钮输入框<br> public<br> { Public declarations }<br> end;<br><br>var<br> frmMain: TfrmMain;<br><br>implementation<br><br><br>//这个函数取当前qq发送窗口上“送讯息”的按钮的句柄<br>//function EnumChildWindowsProc(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>function GetButtonHandle(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>var<br> buffer: array[0..255] of Char;<br> buffer1: array[0..255] of Char;<br>begin<br> Result := True;<br> //得到目标窗口的控件<br> GetClassName(hwnd,buffer,256);<br> //找到发消息的目标窗口的目标控件<br> if StrPas(Buffer)='Button' then<br> begin<br> GetWindowText(hwnd,buffer1,100);<br> if buffer1 = '送讯息(&S)' then<br> begin<br> PInteger(lparam)^ := hwnd; //得到目标控件的Hwnd(句柄)<br> Result:=False; //终止循环<br> end;<br> end;<br>end;//end of function<br><br>//这个函数取当前qq发送窗口上消息框句柄<br>//function EnumChildWindowsProc(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>function GetEditHandle(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>var<br> buffer: array[0..255] of Char;<br> buffer1: array[0..255] of Char;<br>begin<br> Result := True;<br> //得到目标窗口的控件<br> GetClassName(hwnd,buffer,256);<br> //找到发消息的目标窗口的目标控件<br> if StrPas(Buffer)='Edit' then<br> begin<br> GetWindowText(hwnd,buffer1,100);<br> PInteger(lparam)^ := hwnd; //得到目标控件的Hwnd(句柄)<br> Result:=False; //终止循环<br> end;<br>end;//end of function<br><br>{$R *.DFM}<br><br>procedure TfrmMain.bbtnStartClick(Sender: TObject);<br>var<br> Handle: Integer;<br> tmpHandle: Integer;<br>begin<br> {取句柄}<br> Handle := FindWindow(nil,'对话模式'); //就是窗口的Caption<br> if Handle<>0 then<br> begin<br> tmpHandle := Handle;<br> //在这里循环取到想要的句柄为止<br> //取发送按钮的,FButtonHandle<br> EnumChildWindows(Handle,@GetButtonHandle,Integer(@Handle));<br> FButtonHandle := Handle;<br><br> //取消息输入框的,FTextHandle<br> EnumChildWindows(tmpHandle,@GetEditHandle,Integer(@tmpHandle));<br> FTextHandle := tmpHandle;<br><br> Timer.Interval := StrToInt(edtTime.Text);<br> Timer.Enabled := true;<br> end;//end of if<br><br>end;<br><br>procedure TfrmMain.bbtnEndClick(Sender: TObject);<br>begin<br> Timer.Enabled := false;//关闭定时器<br>end;<br><br>procedure TfrmMain.TimerTimer(Sender: TObject);<br>begin<br> {定时发送}<br> //设发送文本<br> SendMessage(FTextHandle,WM_SETTEXT,0,Integer(pchar(memText.Text)));<br><br> //发送按钮<br> //SendMessage(FButtonHandle,BN_CLICKED,0,0);<br> SendMessage(FButtonHandle,WM_LBUTTONDOWN,0,0);<br> SendMessage(FButtonHandle,WM_LBUTTONUP,0,0);<br>end;<br><br>procedure TfrmMain.bbtnHelpClick(Sender: TObject);<br>var<br> sHelp: String;<br>begin<br> //帮助<br> sHelp := '打开要发送的对象窗口'+#13+<br> '选择对话模式后点[开始]!';<br> ShowMessage(sHelp); <br><br>end;<br><br>end.