我在试写枚举涵数和回调涵数时,为什么我定义的记录类型会出错??? (50分)

K

kk2000

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp; &nbsp;请各位试一下,目的是获取“类名”和”标题“的值并用memo1显示出来<br>&nbsp; 代码如下:<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls, DB, DBTables, ADODB, ExtCtrls;<br>type<br>&nbsp; PwindowstructInfo=^Twindowstruct;<br>&nbsp; Twindowstruct = record<br>&nbsp; &nbsp; caption:string;<br>&nbsp; &nbsp; calname:string;<br>&nbsp; &nbsp; wndhandle:Thandle;<br>&nbsp; end; &nbsp; &nbsp;<br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br><br><br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br><br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp;<br>implementation<br><br>{$R *.dfm}<br><br>function Enumwndproc( Hwindow:Hwnd;param: lparam): boolean &nbsp;stdcall ;<br>&nbsp;var<br>&nbsp; &nbsp;buffer1,buffer2:pchar;<br>&nbsp; &nbsp;FoundCaption:boolean;<br>&nbsp; &nbsp;FoundClname:boolean;<br>&nbsp;begin<br>&nbsp; &nbsp; result:=true;<br>&nbsp; &nbsp; getmem(buffer1,255);<br>&nbsp; &nbsp; getmem(buffer2,255);<br>&nbsp; try<br>&nbsp; &nbsp;if GetWindowText(Hwindow,buffer1,255)&gt;0 &nbsp; then<br>&nbsp; &nbsp; if &nbsp;pos(PwindowstructInfo(param).caption,strpas(buffer1))&gt;0 &nbsp; then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FoundCaption:=true;<br>&nbsp; &nbsp;if &nbsp;PwindowstructInfo(param).calname='' &nbsp; then<br>&nbsp; &nbsp; &nbsp; &nbsp; FoundClname:=true<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp;if &nbsp;GetClassName(hwindow,buffer2,255)&gt;0 &nbsp; then<br>&nbsp; &nbsp; &nbsp; if pos(PwindowstructInfo(param).calname,strpas(buffer2))&gt;0 &nbsp; &nbsp;then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FoundClname:=true;<br>&nbsp;if &nbsp;(Foundcaption and FoundClname) &nbsp;then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; PWindowstructInfo(param).wndhandle:=Hwindow;<br>&nbsp; &nbsp; &nbsp; result:=false;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp;freemem(buffer1);<br>&nbsp; &nbsp;freemem(buffer2);<br>&nbsp;end;<br>&nbsp;end;<br><br>function FindAwindow(caption, clname: string): Thandle;<br>&nbsp;var<br>&nbsp; wndInfo:Twindowstruct;<br>begin<br>&nbsp; wndInfo.caption:=caption;<br>&nbsp; wndInfo.calname:=clName;<br>&nbsp; wndInfo.wndhandle:=0;<br>&nbsp; EnumWindows(@Enumwndproc,Lparam(@wndInfo));<br>&nbsp; FindAwindow:=wndInfo.wndhandle;<br>end;<br>function Enumchildwndproc(H:Hwnd;param:Lparam) :boolean stdcall;<br>&nbsp; &nbsp;var<br>&nbsp; &nbsp; &nbsp;caption,clname:pchar;<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;result:=true;<br>&nbsp; &nbsp; &nbsp;getmem(caption,255);<br>&nbsp; &nbsp; &nbsp;getmem(clname,255);<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; GetWindowText(H,caption,255);<br>&nbsp; &nbsp; &nbsp; GetClassName(H,clname,255);<br>&nbsp; &nbsp; &nbsp; form1.Memo1.Lines.Add('标题: '+strpas(caption) &nbsp; &nbsp; + &nbsp;'类名:' &nbsp;+ strpas(clname));<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; freemem(caption);<br>&nbsp; &nbsp; &nbsp; freemem(clname);<br>&nbsp; &nbsp; end; &nbsp;<br>&nbsp; end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>&nbsp; var<br>&nbsp; &nbsp;Thewindowhandle:Thandle;<br>begin<br>&nbsp; Thewindowhandle:=FindAwindow('From1','');<br>&nbsp; showmessage(inttostr(Thewindowhandle));<br>&nbsp; form1.Memo1.Lines.Clear;<br>&nbsp;if &nbsp;Thewindowhandle&lt;&gt;0 &nbsp; then<br>&nbsp; &nbsp; Enumchildwindows(Thewindowhandle,@Enumchildwndproc,0);<br>&nbsp;<br>end;<br><br><br><br>end.
 
function Enumwndproc( Hwindow:Hwnd;param: Pointer): bool;<br>stdcall//少了这个
 
&nbsp; &nbsp;reallearning:谢谢!现在没有错了!但是showmessage(inttostr(Thewindowhandle));<br>&nbsp;显示的是"0 " 证明没有找到我指定的窗体! 我还发现是: &nbsp;FoundClname不为true;<br>应该没有理由吧!我去掉"FoundClname",但显示的"标题"是空的,”类名“ 也不是<br>"form1"上的类名。
 
&nbsp; 谢谢了!问题解决。
 
接受答案了.
 
顶部