请教各位大侠:已知一个窗口的句柄,如何检索该Form上的所有控件,并判断其类型(100分)

  • 主题发起人 主题发起人 baoling
  • 开始时间 开始时间
B

baoling

Unregistered / Unconfirmed
GUEST, unregistred user!
请教各位大侠:已知一个窗口的句柄,如何检索该Form上的所有控件,并判断其类型,<br>该窗口可能是其他应用程序的窗体<br>
 
另:还可以修改检索到的控件的属性,如:Tlabel,我可以修改它的caption的属性
 
不知道其它的应用程序的窗口行不行,你试试看吧!<br>你可以使用FORM的Components属性或Controls属性,如:<br>&nbsp; Temp:TComponets;<br>&nbsp; for I :=0 to Form.ComponentCount - 1 do<br>&nbsp; begin<br>&nbsp; &nbsp; Temp := Components;<br>&nbsp; &nbsp; if (Temp is TLabel) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; (Temp as TLabel).caption:="be changed";<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>
 
多使用几次getwindow就行了。
 
其它的应用程序的窗口似乎不行,另Temp的类型不是TComponets,而是TComponet<br>
 
API函数 EnumChildWindows
 
(FindComponent('label5') as Tlabel).caption ;= 'haha'; <br>
 
&gt; API函数 EnumChildWindows <br>补充一点,获取句柄就可以用GetClassName获取类名,SPY,WinSight之类的工具都是这样调用的。<br>
 
就是这样:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var i:integer;<br>begin<br>&nbsp; for I :=0 to Form1.ComponentCount - 1 do<br>&nbsp; begin<br>&nbsp; &nbsp; if (Form1.Components is TLabel) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Form1.Components as TLabel).Caption:='This is a label';<br>&nbsp; &nbsp;end;<br>end;<br>已经测试通过<br>
 
procedure TForm1.Button1Click(Sender: TObject);<br>&nbsp; function EnumChildWindowsProc(hwnd: Integer): Boolean; stdcall;<br>&nbsp; var<br>&nbsp; &nbsp; buffer: array[0..255] of Char;<br>&nbsp; begin<br>&nbsp; &nbsp; Result := True; &nbsp; <br>&nbsp; &nbsp; GetClassName(hwnd,buffer,256); &nbsp;//得到类名<br>&nbsp; &nbsp; Form1.Memo1.Lines.add(buffer); &nbsp;//加到Memo显示<br>&nbsp; end;<br>var<br>&nbsp; hwnd: Integer;<br>&nbsp; buffer: Array[0..1023] of Char;<br>begin<br>&nbsp; Memo1.Clear;<br>&nbsp; hwnd := FindWindow(nil,'窗口'); &nbsp; //得到目标窗口句柄<br>&nbsp; if hwnd&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; EnumChildWindows(hwnd,@EnumChildWindowsProc,Integer(@hwnd));<br>end;<br><br>给分吧。
 
沒必要這麼難吧<br>這樣用吧<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>procedure TForm1.Button1Click(Sender: TObject);<br>var i:integer;<br>begin<br>&nbsp; listbox1.Items.Clear ;<br>&nbsp; for I :=0 to Form1.ComponentCount - 1 do<br>&nbsp; &nbsp; &nbsp;listbox1.Items.Add(Form1.Components.ClassName) ;<br>end;<br>
 
就是需要使用tinytao的办法。<br>得到不同于本可执行文件的其他窗体上元素的信息
 
同意 DragonPC_??? 的说法!
 
肯定是要使用API函数的,使用ComponentCount检索的方法适合Delphi自己的Form,其他应用<br>程序的就不可能了,使用EnumChildWindows函数有基本可以,但是没有句柄的按钮或其他对象好<br>像无法检索到,另有没有修改属性的办法
 
function hasChildWindow(H: HWND): boolean;<br>begin<br>&nbsp; if getWindow(H,GW_CHILD)=0 then<br>&nbsp; result:=false<br>else<br>&nbsp; result:=true;<br>end;<br><br>procedure allwindow(H: HWND);<br>var P: PChar;<br>&nbsp; &nbsp; w:HWND;<br>begin<br>&nbsp; if HasChildWindow(H) then<br>&nbsp; begin<br>&nbsp; &nbsp; getmem(P,256);<br>&nbsp; &nbsp; w:=getwindow(H,GW_CHILD);<br>&nbsp; &nbsp; while w&lt;&gt;0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; GetClassName(w,P,256);<br>&nbsp; &nbsp; &nbsp; GetWindowText(w,P,256);<br>&nbsp; &nbsp; &nbsp; if &nbsp;HasChildWindow(w) then<br>&nbsp; &nbsp; &nbsp; allwindow(w); &nbsp;//地跪调用<br><br>&nbsp; &nbsp; &nbsp; GetWindow(w,GW_HWNDNEXT);<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp; Freemem(P); &nbsp; <br>&nbsp; end;<br>end
 
尤其是label没handle怎么取啊?
 
后退
顶部