怎样查找正在运行的某个程序的控件句柄!(100分)

  • 主题发起人 主题发起人 cmd9x
  • 开始时间 开始时间
C

cmd9x

Unregistered / Unconfirmed
GUEST, unregistred user!
在一个程序中需要查找另一个程序中的TreeView的Handle并取得这个TreeView的数据,用WindowFromPoint的方法可以轻松获得另一个程序中TreeView的Handle,但是需要手工操作一下,特麻烦,我希望可以直接在这个程序中搜索<br>最好有一小段代码,多谢
 
FindWindowEx 。
 
这个TreeView只有一个类名,而且可能在很深层下,光用FindWindowEx根本找不到
 
根据我的研究,可以用列举子窗口的方式进行<br><br>function EnumChildWindowsProc(H: HWND; L: LPARAM): bool; stdcall;<br>var<br> &nbsp;ClassName: array[0..255] of char;<br> &nbsp;str2: string;<br>begin<br> &nbsp;GetClassName(H, @ClassName, 255);<br> &nbsp;str2 := ClassName;<br> &nbsp;if Form1.FClsName = str2 then<br> &nbsp;begin<br> &nbsp; &nbsp;Form1.FRetHWnd := H;<br> &nbsp; &nbsp;form1.MemoText.Lines.add(format('ID:%8d,CsName:%s', [h, str2]));<br> &nbsp; &nbsp;Result:=False;<br> &nbsp;end else<br> &nbsp;Result := True;<br>end;<br><br>function TForm1.FindCenterTree(const sCharacter,sClassName: string): HWND;<br>var//窗口标题特征串,要查找的控件类名<br> &nbsp;hCurrWnd,hParent: HWnd;<br> &nbsp;szText: array[0..254] of char;<br>begin<br> &nbsp;Form1.FRetHWnd := 0;<br> &nbsp;Form1.FClsName := sClassName;<br> &nbsp;hParent := 0;<br> &nbsp;hParent := GetWindow(Handle, GW_HWNDFIRST);<br> &nbsp;while hCurrWnd &lt;&gt; 0 do<br> &nbsp;begin<br> &nbsp; &nbsp;if GetWindowText(hParent, @szText, 255) &gt; 0 then<br> &nbsp; &nbsp; &nbsp;if Pos(sCharacter, StrPas(szText)) &gt; 0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Memo1.Lines.Add(Format('ID:%8d, Caption:%s', [hCurrWnd, StrPas(@szText)]));<br> &nbsp; &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;hParent := GetWindow(hParent, GW_HWNDNEXT);<br> &nbsp;end;<br> &nbsp;if hParent &lt;&gt; 0 then<br> &nbsp; &nbsp;EnumChildWindows(hParent, @EnumChildWindowsProc, 0);<br><br> &nbsp;Result := Form1.FRetHWnd;<br>end;<br><br>在EnumChildWindowsProc中对ClassName进行直接比较时会出错,所以用窗口算定义属性却不会出错,也不知道为什么。
 
while hCurrWnd &lt;&gt; 0 do<br> &nbsp;begin<br> &nbsp; &nbsp;if GetWindowText(hParent, @szText, 255) &gt; 0 then<br> &nbsp; &nbsp; &nbsp;if Pos(sCharacter, StrPas(szText)) &gt; 0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Memo1.Lines.Add(Format('ID:%8d, Caption:%s', [hCurrWnd, StrPas(@szText)]));<br> &nbsp; &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;hParent := GetWindow(hParent, GW_HWNDNEXT);<br> &nbsp;end;<br>应该改成这样才对吧:<br> &nbsp;while hCurrWnd &lt;&gt; 0 do<br> &nbsp;begin<br> &nbsp; &nbsp;if GetWindowText(hCurrWnd, @szText, 255) &gt; 0 then<br> &nbsp; &nbsp; &nbsp;if Pos(sCharacter, StrPas(szText)) &gt; 0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;hParent:=hCurrWnd; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;MemoText.Lines.Add(Format('ID:%8d, Caption:%s', [hCurrWnd, StrPas(@szText)]));<br> &nbsp; &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;hCurrWnd := GetWindow(hCurrWnd, GW_HWNDNEXT);<br> &nbsp;end;
 
后退
顶部