怎么去实现对一个窗口消息的监听呢﹖仿真QQ”发送”按钮﹐为什幺信息发不出去呢﹖ ( 积分: 100 )

  • 主题发起人 胡圖崇
  • 开始时间

胡圖崇

Unregistered / Unconfirmed
GUEST, unregistred user!
开始我始终找不到输入文本消息的控件是什幺﹐后来把控件名都写入一个文本文件后﹐发现是RichEdit20A﹐这样才获得了文本输入框的句柄﹐但是消息发出去一条﹐然后没反应了﹐而且发出去的这条信息对方都收不到﹐不知道是什幺原因﹐请高手指点咯<br>以下是我的程序源代码﹕<br><br>unit UQicqSendForm;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, <br>Dialogs,inifiles,<br> &nbsp;StdCtrls, Buttons, ExtCtrls;<br><br>type<br> &nbsp;TFrmMain = class(TForm)<br> &nbsp; &nbsp;btnStart: TButton;<br> &nbsp; &nbsp;btnEnd: TButton;<br> &nbsp; &nbsp;Bevel1: TBevel;<br> &nbsp; &nbsp;Timer: TTimer;<br> &nbsp; &nbsp;edtTime: TEdit;<br> &nbsp; &nbsp;memText: TMemo;<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;Label2: TLabel;<br> &nbsp; &nbsp;procedure btnStartClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnEndClick(Sender: TObject);<br> &nbsp; &nbsp;procedure TimerTimer(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp; &nbsp;FTextHandle: HWND; &nbsp; //qq消息Text句柄<br> &nbsp; &nbsp;FButtonHandle: HWND; //SendText Button's Handle;<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;FrmMain: TFrmMain;<br><br> &nbsp;<br>implementation<br><br><br>//Fetch sendText's Button<br>//function EnumChildWindowsProc(hwnd: Integer; lparam: Longint):Boolean;stdcall;<br>function GetButtonHandle(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>var<br> &nbsp;buffer: array[0..255] of Char;<br> &nbsp;buffer1: array[0..255] of Char;<br>begin<br> &nbsp;Result := True;<br> &nbsp;//Fetch aim window's component<br> &nbsp;GetClassName(hwnd,buffer,256);<br> &nbsp;//Find SendText's Button<br> &nbsp;if StrPas(Buffer)='Button' then<br> &nbsp;begin<br> &nbsp; &nbsp;GetWindowText(hwnd,buffer1,100);<br> &nbsp; &nbsp;if buffer1 = '傳送(&amp;S)' then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;PInteger(lparam)^ := hwnd; //Fetch Handle<br> &nbsp; &nbsp; &nbsp;Result:=False; &nbsp;//Break Loop<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;//end of function<br><br>//Fetch SendText's Text handle<br>//function EnumChildWindowsProc(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>function GetEditHandle(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>var<br> &nbsp;buffer: array[0..255] of Char;<br> &nbsp;buffer1: array[0..255] of Char;<br>begin<br> &nbsp;Result := True;<br> &nbsp;//Fetch aim window's component<br> &nbsp;GetClassName(hwnd,buffer,256);<br> &nbsp;//Find text's Handle<br> if StrPas(Buffer)='RichEdit20A' then<br> &nbsp;begin<br> &nbsp; &nbsp;GetWindowText(hwnd,buffer1,100);<br> &nbsp; &nbsp;PInteger(lparam)^ := hwnd; //Fetch Text's Handle<br> &nbsp; &nbsp;Result:=False; &nbsp;//Break Loop<br> &nbsp;end; &nbsp; &nbsp; &nbsp; <br>end;//end of function<br><br>{$R *.DFM}<br><br>procedure TfrmMain.btnStartClick(Sender: TObject);<br>var<br> &nbsp;Handle: Integer;<br> &nbsp;tmpHandle: Integer;<br> &nbsp;sendMan:string;<br>begin<br> &nbsp;{取句柄}<br> &nbsp;sendMan:=edit1.Text;<br> &nbsp;Handle := FindWindow(nil,pchar(sendMan)); &nbsp;//就是窗口的Caption<br> &nbsp;if Handle&lt;&gt;0 then<br> &nbsp;begin<br> &nbsp; &nbsp;tmpHandle := Handle;<br> &nbsp; &nbsp;//Loop For Find Button's Handle;<br> &nbsp; &nbsp;EnumChildWindows(Handle,@GetButtonHandle,Integer(@Handle));<br> &nbsp; &nbsp;FButtonHandle := Handle;<br> &nbsp; &nbsp;//Loop For Find SendText's Handle<br> &nbsp; &nbsp;EnumChildWindows(tmpHandle,@GetEditHandle,Integer(@tmpHandle));<br> &nbsp; &nbsp;FTextHandle := tmpHandle;<br> &nbsp; &nbsp;Timer.Interval := StrToInt(edtTime.Text);<br> &nbsp; &nbsp;Timer.Enabled := true;<br> &nbsp;end;<br><br>end;<br><br>procedure TfrmMain.btnEndClick(Sender: TObject);<br>begin<br> &nbsp;Timer.Enabled := false;//關閉定時器<br>end;<br><br>[red]procedure TfrmMain.TimerTimer(Sender: TObject);<br>begin<br><br> &nbsp;//Send Text<br> &nbsp;SendMessage(FTextHandle,WM_SETTEXT,0,Integer(pchar(memText.Text)));<br><br> &nbsp;//SendText Button<br> &nbsp; SendMessage(FButtonHandle,WM_LBUTTONDOWN,0,0);<br> &nbsp;SendMessage(FButtonHandle,WM_LBUTTONUP,0,0);<br>end;[/red]<br><br>end.<br><br>还有﹕假如上面的问题解决了﹐我怎幺解决监听QQ窗口信息呢﹖就象QQ自动回复一样。
 
开始我始终找不到输入文本消息的控件是什幺﹐后来把控件名都写入一个文本文件后﹐发现是RichEdit20A﹐这样才获得了文本输入框的句柄﹐但是消息发出去一条﹐然后没反应了﹐而且发出去的这条信息对方都收不到﹐不知道是什幺原因﹐请高手指点咯<br>以下是我的程序源代码﹕<br><br>unit UQicqSendForm;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, <br>Dialogs,inifiles,<br> &nbsp;StdCtrls, Buttons, ExtCtrls;<br><br>type<br> &nbsp;TFrmMain = class(TForm)<br> &nbsp; &nbsp;btnStart: TButton;<br> &nbsp; &nbsp;btnEnd: TButton;<br> &nbsp; &nbsp;Bevel1: TBevel;<br> &nbsp; &nbsp;Timer: TTimer;<br> &nbsp; &nbsp;edtTime: TEdit;<br> &nbsp; &nbsp;memText: TMemo;<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;Label2: TLabel;<br> &nbsp; &nbsp;procedure btnStartClick(Sender: TObject);<br> &nbsp; &nbsp;procedure btnEndClick(Sender: TObject);<br> &nbsp; &nbsp;procedure TimerTimer(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp; &nbsp;FTextHandle: HWND; &nbsp; //qq消息Text句柄<br> &nbsp; &nbsp;FButtonHandle: HWND; //SendText Button's Handle;<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;FrmMain: TFrmMain;<br><br> &nbsp;<br>implementation<br><br><br>//Fetch sendText's Button<br>//function EnumChildWindowsProc(hwnd: Integer; lparam: Longint):Boolean;stdcall;<br>function GetButtonHandle(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>var<br> &nbsp;buffer: array[0..255] of Char;<br> &nbsp;buffer1: array[0..255] of Char;<br>begin<br> &nbsp;Result := True;<br> &nbsp;//Fetch aim window's component<br> &nbsp;GetClassName(hwnd,buffer,256);<br> &nbsp;//Find SendText's Button<br> &nbsp;if StrPas(Buffer)='Button' then<br> &nbsp;begin<br> &nbsp; &nbsp;GetWindowText(hwnd,buffer1,100);<br> &nbsp; &nbsp;if buffer1 = '傳送(&amp;S)' then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;PInteger(lparam)^ := hwnd; //Fetch Handle<br> &nbsp; &nbsp; &nbsp;Result:=False; &nbsp;//Break Loop<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;//end of function<br><br>//Fetch SendText's Text handle<br>//function EnumChildWindowsProc(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>function GetEditHandle(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>var<br> &nbsp;buffer: array[0..255] of Char;<br> &nbsp;buffer1: array[0..255] of Char;<br>begin<br> &nbsp;Result := True;<br> &nbsp;//Fetch aim window's component<br> &nbsp;GetClassName(hwnd,buffer,256);<br> &nbsp;//Find text's Handle<br> if StrPas(Buffer)='RichEdit20A' then<br> &nbsp;begin<br> &nbsp; &nbsp;GetWindowText(hwnd,buffer1,100);<br> &nbsp; &nbsp;PInteger(lparam)^ := hwnd; //Fetch Text's Handle<br> &nbsp; &nbsp;Result:=False; &nbsp;//Break Loop<br> &nbsp;end; &nbsp; &nbsp; &nbsp; <br>end;//end of function<br><br>{$R *.DFM}<br><br>procedure TfrmMain.btnStartClick(Sender: TObject);<br>var<br> &nbsp;Handle: Integer;<br> &nbsp;tmpHandle: Integer;<br> &nbsp;sendMan:string;<br>begin<br> &nbsp;{取句柄}<br> &nbsp;sendMan:=edit1.Text;<br> &nbsp;Handle := FindWindow(nil,pchar(sendMan)); &nbsp;//就是窗口的Caption<br> &nbsp;if Handle&lt;&gt;0 then<br> &nbsp;begin<br> &nbsp; &nbsp;tmpHandle := Handle;<br> &nbsp; &nbsp;//Loop For Find Button's Handle;<br> &nbsp; &nbsp;EnumChildWindows(Handle,@GetButtonHandle,Integer(@Handle));<br> &nbsp; &nbsp;FButtonHandle := Handle;<br> &nbsp; &nbsp;//Loop For Find SendText's Handle<br> &nbsp; &nbsp;EnumChildWindows(tmpHandle,@GetEditHandle,Integer(@tmpHandle));<br> &nbsp; &nbsp;FTextHandle := tmpHandle;<br> &nbsp; &nbsp;Timer.Interval := StrToInt(edtTime.Text);<br> &nbsp; &nbsp;Timer.Enabled := true;<br> &nbsp;end;<br><br>end;<br><br>procedure TfrmMain.btnEndClick(Sender: TObject);<br>begin<br> &nbsp;Timer.Enabled := false;//關閉定時器<br>end;<br><br>[red]procedure TfrmMain.TimerTimer(Sender: TObject);<br>begin<br><br> &nbsp;//Send Text<br> &nbsp;SendMessage(FTextHandle,WM_SETTEXT,0,Integer(pchar(memText.Text)));<br><br> &nbsp;//SendText Button<br> &nbsp; SendMessage(FButtonHandle,WM_LBUTTONDOWN,0,0);<br> &nbsp;SendMessage(FButtonHandle,WM_LBUTTONUP,0,0);<br>end;[/red]<br><br>end.<br><br>还有﹕假如上面的问题解决了﹐我怎幺解决监听QQ窗口信息呢﹖就象QQ自动回复一样。
 
怎么就沒人回答我的問題呢﹖
 
高手們是不是認為我的問題比較幼稚啊﹖<br>怎么沒人理我呢﹖
 
可能高手觉得很幼稚,我觉得很难。<br><br>一点看法,一个是监视QQ的网络数据包,一种是监视某个已经打开的对话框,看对话框里面的内容变化,可能后面一种实现起来容易一点,这个监视我不会弄,有人说用Hook可以做到,不过我不会(废话),能找到当前的信息,发送回去就方便了,这段时间论坛这方面的代码比较多,我找了一点资料,希望对你有用,原理也比较清爽<br><br><br>---------------------以下转自网络<br><br>http://www.delphibbs.com/delphibbs/dispq.asp?LID=600569<br><br>其实这个程序主要的内容不是如何发qq信息<br>而是如何找到某一个正在运行的应用程序窗口上任意一个控件的句柄<br>我的方法是(以一个button为例)<br>用FindWindow找到窗体的句柄后<br>根据按钮的类别,循环找这个窗体上的每个'Button',然后用GetWindowText和Button的Caption比较,就可以找到想要的按钮句柄了<br><br>下面贴出我的程序,望大家批评指教<br>其中部分代码参考大富翁上某一帖子(忘了是哪个了)<br><br>unit OicqSendForm;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;StdCtrls, Buttons, ExtCtrls;<br><br>type<br> &nbsp;TfrmMain = class(TForm)<br> &nbsp; &nbsp;memText: TMemo;<br> &nbsp; &nbsp;edtTime: TEdit;<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;Bevel1: TBevel;<br> &nbsp; &nbsp;Label2: TLabel;<br> &nbsp; &nbsp;bbtnStart: TBitBtn;<br> &nbsp; &nbsp;bbtnEnd: TBitBtn;<br> &nbsp; &nbsp;bbtnHelp: TBitBtn;<br> &nbsp; &nbsp;Timer: TTimer;<br> &nbsp; &nbsp;procedure bbtnStartClick(Sender: TObject);<br> &nbsp; &nbsp;procedure bbtnEndClick(Sender: TObject);<br> &nbsp; &nbsp;procedure TimerTimer(Sender: TObject);<br> &nbsp; &nbsp;procedure bbtnHelpClick(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp; &nbsp;FTextHandle: HWND; &nbsp; //qq消息输入框句柄<br> &nbsp; &nbsp;FButtonHandle: HWND; //发送按钮输入框<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;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> &nbsp;buffer: array[0..255] of Char;<br> &nbsp;buffer1: array[0..255] of Char;<br>begin<br> &nbsp;Result := True;<br> &nbsp;//得到目标窗口的控件<br> &nbsp;GetClassName(hwnd,buffer,256);<br> &nbsp;//找到发消息的目标窗口的目标控件<br> &nbsp;if StrPas(Buffer)='Button' then<br> &nbsp;begin<br> &nbsp; &nbsp;GetWindowText(hwnd,buffer1,100);<br> &nbsp; &nbsp;if buffer1 = '送讯息(&amp;S)' then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;PInteger(lparam)^ := hwnd; //得到目标控件的Hwnd(句柄)<br> &nbsp; &nbsp; &nbsp;Result:=False; &nbsp;//终止循环<br> &nbsp; &nbsp;end;<br> &nbsp;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> &nbsp;buffer: array[0..255] of Char;<br> &nbsp;buffer1: array[0..255] of Char;<br>begin<br> &nbsp;Result := True;<br> &nbsp;//得到目标窗口的控件<br> &nbsp;GetClassName(hwnd,buffer,256);<br> &nbsp;//找到发消息的目标窗口的目标控件<br> &nbsp;if StrPas(Buffer)='Edit' then<br> &nbsp;begin<br> &nbsp; &nbsp;GetWindowText(hwnd,buffer1,100);<br> &nbsp; &nbsp;PInteger(lparam)^ := hwnd; //得到目标控件的Hwnd(句柄)<br> &nbsp; &nbsp;Result:=False; &nbsp;//终止循环<br> &nbsp;end;<br>end;//end of function<br><br>{$R *.DFM}<br><br>procedure TfrmMain.bbtnStartClick(Sender: TObject);<br>var<br> &nbsp;Handle: Integer;<br> &nbsp;tmpHandle: Integer;<br>begin<br> &nbsp;{取句柄}<br> &nbsp;Handle := FindWindow(nil,'对话模式'); &nbsp;//就是窗口的Caption<br> &nbsp;if Handle&lt;&gt;0 then<br> &nbsp;begin<br> &nbsp; &nbsp;tmpHandle := Handle;<br> &nbsp; &nbsp;//在这里循环取到想要的句柄为止<br> &nbsp; &nbsp;//取发送按钮的,FButtonHandle<br> &nbsp; &nbsp;EnumChildWindows(Handle,@GetButtonHandle,Integer(@Handle));<br> &nbsp; &nbsp;FButtonHandle := Handle;<br><br> &nbsp; &nbsp;//取消息输入框的,FTextHandle<br> &nbsp; &nbsp;EnumChildWindows(tmpHandle,@GetEditHandle,Integer(@tmpHandle));<br> &nbsp; &nbsp;FTextHandle := tmpHandle;<br><br> &nbsp; &nbsp;Timer.Interval := StrToInt(edtTime.Text);<br> &nbsp; &nbsp;Timer.Enabled := true;<br> &nbsp;end;//end of if<br><br>end;<br><br>procedure TfrmMain.bbtnEndClick(Sender: TObject);<br>begin<br> &nbsp;Timer.Enabled := false;//关闭定时器<br>end;<br><br>procedure TfrmMain.TimerTimer(Sender: TObject);<br>begin<br> &nbsp;{定时发送}<br> &nbsp;//设发送文本<br> &nbsp;SendMessage(FTextHandle,WM_SETTEXT,0,Integer(pchar(memText.Text)));<br><br> &nbsp;//发送按钮<br> &nbsp;//SendMessage(FButtonHandle,BN_CLICKED,0,0);<br> &nbsp;SendMessage(FButtonHandle,WM_LBUTTONDOWN,0,0);<br> &nbsp;SendMessage(FButtonHandle,WM_LBUTTONUP,0,0);<br>end;<br><br>procedure TfrmMain.bbtnHelpClick(Sender: TObject);<br>var<br> &nbsp;sHelp: String;<br>begin<br> &nbsp;//帮助<br> &nbsp;sHelp := '打开要发送的对象窗口'+#13+<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '选择对话模式后点[开始]!';<br> &nbsp;ShowMessage(sHelp); &nbsp; &nbsp; &nbsp; &nbsp; <br><br>end;<br><br>end.
 
為什么沒人理我的問題呢﹖[:(]
 
转无泪大侠的,里面有你要的东西<br><br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=3173133
 
多人接受答案了。
 
顶部