K
kk2000
Unregistered / Unconfirmed
GUEST, unregistred user!
请各位试一下,目的是获取“类名”和”标题“的值并用memo1显示出来<br> 代码如下:<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, DB, DBTables, ADODB, ExtCtrls;<br>type<br> PwindowstructInfo=^Twindowstruct;<br> Twindowstruct = record<br> caption:string;<br> calname:string;<br> wndhandle:Thandle;<br> end; <br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Memo1: TMemo;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br><br><br> public<br> { Public declarations }<br><br> end;<br><br>var<br> Form1: TForm1;<br> <br>implementation<br><br>{$R *.dfm}<br><br>function Enumwndproc( Hwindow:Hwnd;param: lparam): boolean stdcall ;<br> var<br> buffer1,buffer2char;<br> FoundCaption:boolean;<br> FoundClname:boolean;<br> begin<br> result:=true;<br> getmem(buffer1,255);<br> getmem(buffer2,255);<br> try<br> if GetWindowText(Hwindow,buffer1,255)>0 then<br> if pos(PwindowstructInfo(param).caption,strpas(buffer1))>0 then<br> FoundCaption:=true;<br> if PwindowstructInfo(param).calname='' then<br> FoundClname:=true<br> else<br> if GetClassName(hwindow,buffer2,255)>0 then<br> if pos(PwindowstructInfo(param).calname,strpas(buffer2))>0 then<br> FoundClname:=true;<br> if (Foundcaption and FoundClname) then<br> begin<br> PWindowstructInfo(param).wndhandle:=Hwindow;<br> result:=false;<br> end;<br> finally<br> freemem(buffer1);<br> freemem(buffer2);<br> end;<br> end;<br><br>function FindAwindow(caption, clname: string): Thandle;<br> var<br> wndInfo:Twindowstruct;<br>begin<br> wndInfo.caption:=caption;<br> wndInfo.calname:=clName;<br> wndInfo.wndhandle:=0;<br> EnumWindows(@Enumwndproc,Lparam(@wndInfo));<br> FindAwindow:=wndInfo.wndhandle;<br>end;<br>function Enumchildwndproc(H:Hwnd;param:Lparam) :boolean stdcall;<br> var<br> caption,clnamechar;<br> begin<br> result:=true;<br> getmem(caption,255);<br> getmem(clname,255);<br> try<br> GetWindowText(H,caption,255);<br> GetClassName(H,clname,255);<br> form1.Memo1.Lines.Add('标题: '+strpas(caption) + '类名:' + strpas(clname));<br> finally<br> freemem(caption);<br> freemem(clname);<br> end; <br> end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br> var<br> Thewindowhandle:Thandle;<br>begin<br> Thewindowhandle:=FindAwindow('From1','');<br> showmessage(inttostr(Thewindowhandle));<br> form1.Memo1.Lines.Clear;<br> if Thewindowhandle<>0 then<br> Enumchildwindows(Thewindowhandle,@Enumchildwndproc,0);<br> <br>end;<br><br><br><br>end.