斑竹求救,其他大虾也帮帮小弟吧!(100分)

  • 主题发起人 主题发起人 p2p
  • 开始时间 开始时间
P

p2p

Unregistered / Unconfirmed
GUEST, unregistred user!
我看可以一个QQ轰炸机的代码,于是自己写了一段代码模仿在另外一个windows程序里的一个button的鼠标单击!代码是这样写的!<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 = '确定' 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;<br>procedure tform1.button1onclick(sender:tobject);<br>var<br>&nbsp; &nbsp;myHandle,temphandle,Fbuttonhandle:Hwnd;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myHandle:=FindWindow(nil,'test'); &nbsp;//就是窗口的Caption<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if myHandle&lt;&gt;nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpHandle := myHandle;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //在这里循环取到想要的句柄为止<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //取按钮的FButtonHandle<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnumChildWindows(tmpHandle,@GetButtonHandle,Integer(@tmpHandle));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FButtonHandle := tmpHandle;<br>end;<br>&nbsp; &nbsp; &nbsp; &nbsp; SendMessage(FButtonHandle,MOUSEEVENTF_LEFTDOWN,0,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; SendMessage(FButtonHandle,MOUSEEVENTF_LEFTUP,0,0);<br>end;<br>程序编译正常,但我怎么按button,在另外一个叫test的窗体里的名为'确定'的按钮也没有反应!<br>请教大虾们,我的程序到底出现了什么问题?拜托!<br>
 
你的代码有些错误。具体如下:<br>1.这段类名有问题,如下:<br>&nbsp; &nbsp; Result := True;<br>&nbsp; &nbsp; //得到目标窗口的控件<br>&nbsp; &nbsp; GetClassName(hwnd,buffer,256);<br>&nbsp; &nbsp; //找到目标窗口的目标控件<br>&nbsp; &nbsp; if StrPas(Buffer)='Button' then ...... &lt;----- 应该是 TButton<br>2. 下面一段从 begin ... end 照我的改:<br>begin<br>&nbsp; myHandle:=FindWindow(nil,'Test'); &nbsp;//就是窗口的Caption &lt;----- 注意大小写<br>&nbsp; &nbsp;if myHandle &lt;&gt; 0 then &nbsp;// &lt;---- 不是 null 是 0<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;tmpHandle := myHandle; // &lt;--- 转值没有意义<br>&nbsp; &nbsp; &nbsp; &nbsp;//在这里循环取到想要的句柄为止<br>&nbsp; &nbsp; &nbsp; &nbsp;//取按钮的FButtonHandle<br>&nbsp; &nbsp; &nbsp; &nbsp;EnumChildWindows(tmpHandle,@GetButtonHandle,Integer(@Fbuttonhandle));<br>// &nbsp; &nbsp; &nbsp; FButtonHandle := tmpHandle; &lt;----- 去掉它,废句<br>&nbsp; &nbsp; &nbsp; &nbsp;SendMessage(FButtonHandle,WM_LBUTTONDOWN,0,0);// 搞错消息了<br>&nbsp; &nbsp; &nbsp; &nbsp;SendMessage(FButtonHandle,WM_LBUTTONUP,0,0); // 搞错消息了<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br>注意我将 SendMessage 移到了判断执行的里面了。反正看清楚!
 
按你的写法的完整修改如下:<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)='TButton' then<br>&nbsp; begin<br>&nbsp; &nbsp; GetWindowText(hwnd,buffer1,100);<br>&nbsp; &nbsp; if buffer1 = '确定' 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;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; &nbsp;myHandle,tmphandle,Fbuttonhandle:Hwnd;<br>begin<br>&nbsp; myHandle:=FindWindow(nil,'Test'); &nbsp;//就是窗口的Caption<br>&nbsp; &nbsp;if myHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;tmpHandle := myHandle;<br>&nbsp; &nbsp; &nbsp; &nbsp;//在这里循环取到想要的句柄为止<br>&nbsp; &nbsp; &nbsp; &nbsp;//取按钮的FButtonHandle<br>&nbsp; &nbsp; &nbsp; &nbsp;EnumChildWindows(tmpHandle,@GetButtonHandle,Integer(@Fbuttonhandle));<br>// &nbsp; &nbsp; &nbsp; FButtonHandle := tmpHandle;<br>&nbsp; &nbsp; &nbsp; &nbsp;SendMessage(FButtonHandle,WM_LBUTTONDOWN{MOUSEEVENTF_LEFTDOWN},0,0);<br>&nbsp; &nbsp; &nbsp; &nbsp;SendMessage(FButtonHandle,WM_LBUTTONUP{MOUSEEVENTF_LEFTUP},0,0);<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br>end.<br>照抄吧。
 
接受答案了.
 
后退
顶部