请问如何使用Findwindow函数,大侠救我,分数一定多给.(200分)

  • 主题发起人 主题发起人 wixll
  • 开始时间 开始时间
W

wixll

Unregistered / Unconfirmed
GUEST, unregistred user!
现在知道金山词霸的:<br>Window Caption:金山词霸 2002<br>Class Name: &nbsp; &nbsp;#32770 (Dialog)(利用Spy++看到的)<br>我的问题是如何利用这个条件和函数Findwindow也可以用其他函数<br>得到金山词霸的句柄,并且如何得到它的子控件(比如它上面的按钮等)<br>的句柄,大侠谢谢,最好给我一个实例.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
 
&nbsp; &nbsp;函数功能:该函数获得一个顶层窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配。这个函数不查找子窗口。<br>在查找时不区分大小写。<br><br>&nbsp; &nbsp; 函数型:HWND FindWindow(LPCTSTR IpClassName,LPCTSTR IpWindowName);<br><br>&nbsp; &nbsp; 参数:<br><br>&nbsp; &nbsp; IpClassName :指向一个指定了类名的空结束字符串,或一个标识类名字符串的成员的指针。如果该参数为一个成员,则它必须为前次调用theGlobafAddAtom函数产生的全局成员。该成员为16位,必须位于IpClassName的低 16位,高位必须为 0。<br><br>&nbsp; &nbsp; IpWindowName:指向一个指定了窗口名(窗口标题)的空结束字符串。如果该参数为空,则为所有窗口全匹配。<br><br>&nbsp; &nbsp; 返回值:如果函数成功,返回值为具有指定类名和窗口名的窗口句柄;如果函数失败,返回值为NULL。<br><br>&nbsp; &nbsp; 若想获得更多错误信息,请调用GetLastError函数。<br><br>&nbsp; &nbsp; 备注:Windows CE:若类名是一个成员,它必须是从 RegisterClass返回的成员。<br><br>&nbsp; &nbsp; 速查:Windows NT:3.1以上版本;Windows:95以上版本;Windows CE:1.0以上版本;头文件:Winuser.h;库文件:user32.lib; Unicode:在 Windows NT上实现为 Unicode和 ANSI两种版本。
 
&nbsp;函数功能:该函数获得包含指定点的窗口的句柄。<br><br>&nbsp; &nbsp; 函数原型:HWND WindowFromPoint(POINT Point);<br><br>&nbsp; &nbsp; 参数:<br><br>&nbsp; &nbsp; Point:指定一个被检测的点的POINT结构。<br><br>&nbsp; &nbsp; 返回值S:返回值为包含该点的窗口的句柄。如果包含指定点的窗口不存在,返回值为NULL。如果该点在静态文本控制之上,返回值是在该静态文本控制的下面的窗口。<br><br>&nbsp; &nbsp; 备注:WindowFromPoint函数不获取隐藏或禁止的窗口句柄,即使点在该窗口内。应用程序应该使用ChildWindowFromPoint函数进行无限制查询。<br><br>&nbsp; &nbsp; 速查:Windows NT:3.1以上版本:Windows:95以上版本;Windows CE:1.0以上版本:头文件:Winuser.h;库文件:user32.lib。<br><br>
 
unit pzjUnit;<br><br>interface<br>&nbsp; uses<br>&nbsp; &nbsp; windows,SysUtils;<br>&nbsp; Function GetComHandle(P_name,C_name: string): Integer;<br>&nbsp; <br><br>implementation<br><br>var<br>&nbsp; comName: string;<br>function GetComHandle(P_name,c_name: string): Integer;<br>var<br>&nbsp; p_handle: Integer;<br>&nbsp; <br>&nbsp; &nbsp; &nbsp; function EnumChildWindowsProc(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>&nbsp; &nbsp; &nbsp; var<br>&nbsp; &nbsp; &nbsp; &nbsp; buffer: array[0..255] of Char;<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := True;<br>&nbsp; &nbsp; &nbsp; &nbsp; //得到目标窗口的控件<br>&nbsp; &nbsp; &nbsp; &nbsp; windows.GetClassName(hwnd,buffer,256);<br>&nbsp; &nbsp; &nbsp; &nbsp; //找到发消息的目标窗口的目标控件<br>&nbsp; &nbsp; &nbsp; &nbsp; if StrPas(Buffer)= ComName &nbsp;then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PInteger(lparam)^ := hwnd;// 得到目标控件的Hwnd(句柄)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=False; &nbsp;//终止循环<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>begin<br>&nbsp; p_handle := FindWindow(nil,pchar(p_name)); &nbsp;//就是窗口的Caption<br>&nbsp; if p_handle&lt;&gt;0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ComName:=c_name;<br>&nbsp; &nbsp; &nbsp; EnumChildWindows(p_handle,@EnumChildWindowsproc,Integer(@p_handle));<br>&nbsp; &nbsp; &nbsp;//如果找到就是Handle,如果没找到Handle,还是窗口<br>&nbsp; &nbsp; &nbsp; result:=p_handle;<br>&nbsp; &nbsp; end;<br>end;<br><br>end.<br><br><br>/////使用例子:GetComHandle('金山词霸','Edit');
 
得到任意窗体和控件(内部和外部)的句柄和类名!<br>你只要将鼠标移动到目标,就会看见,^_^<br><br>procedure TForm1.GetMousePosHwndAndClassName(Sender: TPoint);<br>var<br>&nbsp; hWnd: THandle;<br>&nbsp; aName: array [0..255] of char;<br>begin<br>&nbsp; hWnd := WindowFromPoint(Sender);<br>&nbsp; Label1.Caption := 'Handle : ' + IntToStr(hWnd);<br>&nbsp; if boolean(GetClassName(hWnd, aName, 256)) then<br>&nbsp; &nbsp; Label2.Caption := 'ClassName : ' + string(aName)<br>&nbsp; else<br>&nbsp; &nbsp; Label2.Caption := 'ClassName : not found';<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Form1.FormStyle := fsStayOnTop;<br>&nbsp; Timer1.Interval := 50;<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br>&nbsp; rPos: TPoint;<br>begin<br>&nbsp; if boolean(GetCursorPos(rPos)) then<br>&nbsp; &nbsp; GetMousePosHwndAndClassName(rPos);<br>end;
 
var<br>&nbsp; HWndCalculator : HWnd;<br>begin<br>&nbsp; &nbsp;// find the exist calculator window<br>&nbsp; HWndCalculator := Winprocs.FindWindow(nil, '计算器');<br><br>&nbsp; &nbsp;// close the exist Calculator }<br>&nbsp; if HWndCalculator &lt;&gt; 0 then &nbsp;<br>&nbsp; &nbsp; &nbsp;SendMessage(HWndCalculator, WM_CLOSE, 0, 0);<br>end;<br>请打开计算器,然后执行这段程序如何?
 
感谢回答的各位的帮助.真的谢谢你们了.<br>我还有一个问题,比如我将鼠标指向了金山词霸的Edit对话框,利用以上<br>各位结束的方法我已经得到了句柄,但我现在想得到其内容怎么办?我试着<br>用GetWindowText但失败,查了帮助告诉我This function cannot retrieve <br>the text of an edit control in another application.请问有没有其他<br>办法可以获得Edit Control 的内容?另外我要想模拟鼠标点击按钮怎么办,<br>有什么好办法吗.谢谢了,这个问题回答后我一定给分了.真的,谢谢各位大侠了!<br>
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=794058<br><br><br>procedure TMainForm.TimerTimer(Sender: TObject);<br>var<br>&nbsp; Pos: TPoint;<br>&nbsp; Handle: HWND;<br>&nbsp; Buf: array[0..1024] of Char;<br>begin<br>&nbsp; GetCursorPos(Pos);<br>&nbsp; Handle := WindowFromPoint(Pos);<br>&nbsp; HandleEdit.Text := IntToStr(Handle);<br>&nbsp; GetClassName(Handle, Buf, 1024);<br>&nbsp; ClassEdit.Text := Buf;<br>&nbsp; SendMessage(Handle, WM_GETTEXT, 1024, Integer(@Buf));<br>&nbsp; TextEdit.Text := Buf;<br>end;<br><br><br>来自:xiao_min, 时间:2001-12-20 13:15:00, ID:800252 <br>其实方法我上面已经说过了,你试一下:<br>passmark:=sendmessage(curwin,em_getpasswordchar,0,0);<br>if passmark&lt;&gt;0 then<br>&nbsp; begin<br>postmessage(curwin,em_setpasswordchar,0,0);<br>count:=sendmessage(curwin,wm_gettext,100,lparam(@text1));<br>postmessage(curwin,em_setpasswordchar,wparam(ord('*')),0);<br>end;<br><br><br><br><br>测试结果:<br>xp下可以,其实直接向目标控件发送<br>SendMessage(hwnd, EM_SETPASSWORDCHAR, longint(0), 0)<br>就可以显示出来。<br>但是2000下绝对不行!也许是我们的服务器安装了pack的缘故吧。<br><br>
 
unit pzjUnit;<br><br>interface<br>&nbsp; uses<br>&nbsp; &nbsp; windows,SysUtils;<br>&nbsp; Function GetComHandle(P_name,C_name: string): Integer;<br>&nbsp; <br><br>implementation<br><br>var<br>&nbsp; comName: string;<br>function GetComHandle(P_name,c_name: string): Integer;<br>var<br>&nbsp; p_handle: Integer;<br>&nbsp; <br>&nbsp; &nbsp; &nbsp; function EnumChildWindowsProc(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>&nbsp; &nbsp; &nbsp; var<br>&nbsp; &nbsp; &nbsp; &nbsp; buffer: array[0..255] of Char;<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := True;<br>&nbsp; &nbsp; &nbsp; &nbsp; //得到目标窗口的控件<br>&nbsp; &nbsp; &nbsp; &nbsp; windows.GetClassName(hwnd,buffer,256);<br>&nbsp; &nbsp; &nbsp; &nbsp; //找到发消息的目标窗口的目标控件<br>&nbsp; &nbsp; &nbsp; &nbsp; if StrPas(Buffer)= ComName &nbsp;then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PInteger(lparam)^ := hwnd;// 得到目标控件的Hwnd(句柄)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=False; &nbsp;//终止循环<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>begin<br>&nbsp; p_handle := FindWindow(nil,pchar(p_name)); &nbsp;//就是窗口的Caption<br>&nbsp; if p_handle&lt;&gt;0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ComName:=c_name;<br>&nbsp; &nbsp; &nbsp; EnumChildWindows(p_handle,@EnumChildWindowsproc,Integer(@p_handle));<br>&nbsp; &nbsp; &nbsp; result:=p_handle;<br>&nbsp; &nbsp; end;<br>end;<br>end.<br><br>使用时:GetComHandle('窗口名称','组件名称');
 
我想得到金山词霸的窗口的句柄,还可以这样:<br>FindWindow(PChar(GlobalAddAtom(MAKEINTATOM($00008002))), '金山词霸 2002');<br>其中,$00008002是32770的十六进制,'金山词霸 2002'是窗口的Caption。<br>模拟鼠标点击按钮,可以用<br>SendMessage(handle, WM_LButtonDown,……);<br>SendMessage(handle, WM_RButtonDown,……);<br>好像还可以用mouse_event,可我没用过:)
 
多人接受答案了。
 

Similar threads

后退
顶部