请教EnumerateChildWindows的用法(200分)

  • 主题发起人 主题发起人 jarm
  • 开始时间 开始时间
J

jarm

Unregistered / Unconfirmed
GUEST, unregistred user!
请举些例子
 
我想取得窗体中一个按钮的句柄,但不知道应怎么做<br>请举个例子
 
var <br>&nbsp; &nbsp; ret:integer;<br><br>function GetTopMostWindows(Handle: HWND; Info: Pointer): BOOL; stdcall;<br>var<br>buf:array [0..512] of char;<br>begin<br>&nbsp; &nbsp; &nbsp;GetWindowText(Handle,buf,512);<br>&nbsp; &nbsp; &nbsp;if &nbsp;StrLcomp(buf,'qq',33)=0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetForegroundWindow(Handle);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer(Info^) :=1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := False;<br>&nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := True;<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>begin<br>&nbsp; &nbsp; &nbsp;ret := 0 ;<br>&nbsp; &nbsp; &nbsp;EnumWindows(@GetTopMostWindows, Longint(@ret));<br>&nbsp; &nbsp; &nbsp;if ret=0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;end;<br>end.<br>
 
BOOL EnumChildWindows(<br>&nbsp; HWND hWndParent, &nbsp; &nbsp; &nbsp; &nbsp; // handle to parent window<br>&nbsp; WNDENUMPROC lpEnumFunc, &nbsp;// callback function<br>&nbsp; LPARAM lParam &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// application-defined value<br>);<br>上面只比下面多一个参数<br>BOOL EnumWindows(<br>&nbsp; WNDENUMPROC lpEnumFunc, &nbsp;// callback function<br>&nbsp; LPARAM lParam &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// application-defined value<br>);
 
為EnumChildWindows吧!<br><br>Unit Windows.Pas<br><br>Syntax<br>EnumChildWindows(<br>hWndParent: HWND; {the handle of the parent window}<br>lpEnumFunc: TFNWndEnumProc; {a pointer to the callback function}<br>lParam: LPARAM {an application defined 32 bit value}<br>): BOOL; {returns TRUE or FALSE}<br><br>Description<br>EnumChildWindows parses through all of the child windows belonging to the parent window, sending the handle of each child window to an application defined callback function. This function continues until all child windows have been enumerated or the callback function returns FALSE. If a child window has created child windows of its own, these child windows are enumerated as well. This function will ignore child windows that have been destroyed and those which have been created during the enumeration process.<br><br>Parameters<br>hWndParent: The handle of the parent window whose child windows are to be enumerated.<br><br>lpEnumFunc: The address of the application defined callback function.<br><br>lParam: A 32 bit application defined value that will be passed to the callback function.<br><br>Return Value<br>If this function succeeds, it returns TRUE; otherwise it returns FALSE.<br><br><br><br>Callback Syntax<br>EnumChildProc(<br>hWnd: HWND; {a handle to a child window}<br><br>lParam: LPARAM {an application defined 32 bit value}<br>): BOOL; {returns TRUE or FALSE}<br><br>Description<br>This function receives the window handle for each child window belonging to the parent window specified in the call to EnumChildWindows, and may perform any desired task.<br><br>Parameters<br>hWnd: The handle of a child window.<br><br>lParam: A 32 bit application defined number. This value is intended for application specific use inside of the callback function, and is the value of the lParam parameter passed to the EnumChildWindows function.<br><br>Return Value<br>The callback function should return TRUE to continue enumeration; otherwise it should return FALSE.<br><br><br><br><br><br>例子:<br>{our callback function prototype}<br>function EnumerateChildWindows(hWnd:HWND; lParam:LPARAM): BOOL; stdcall;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>procedure TForm1.EnumerateChildWindows1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp;{empty our list box}<br>&nbsp; &nbsp;ListBox1.Items.Clear;<br><br>&nbsp; &nbsp;{enumerate all child windows belonging to Form1}<br>&nbsp; &nbsp;EnumChildWindows(Form1.Handle,@EnumerateChildWindows,0);<br><br>end;<br><br>{these steps execute for every child window belonging to the parent}<br>function EnumerateChildWindows(hWnd: HWND; lParam: LPARAM): BOOL;<br>var<br>&nbsp; &nbsp;ClassName: Array[0..255] of char; &nbsp;// this holds the class name of our child windows<br>begin<br>&nbsp; &nbsp;{get the class name of the given child window}<br>&nbsp; &nbsp;GetClassName(hWnd,ClassName,255);<br><br>&nbsp; &nbsp;{display it in the list box}<br>&nbsp; &nbsp;Form1.ListBox1.Items.Add(ClassName);<br><br>&nbsp; &nbsp;{continue enumeration}<br>&nbsp; &nbsp;Result:=TRUE;<br>end;<br><br>The Tomes of Delphi 3: Win32 Core API Help File by Larry Diehl<br><br>
 
第一步:找到Button所在窗口句柄:<br>var H : THandle;<br>H := FindWindow( nil, '程序Caption' );<br>第二步:找到Button句柄:<br>EnumChildWindows( H, @EnumerateChildWindows, 0 );<br>第三步:发送消息:<br>Sendmessage(H,WM_...,0,0);<br>程序参考:<br><br>//回调函数,列出所有子窗口句柄<br>var<br>&nbsp; HEdt : THandle;<br>function EnumerateChildWindows(hWnd: HWND; lParam: LPARAM): BOOL;<br>var<br>&nbsp; WindowCaption:array[0..254] of Char;<br>begin<br>&nbsp; GetWindowText(Hwnd,WindowCaption,255);<br>&nbsp; if WindowCaption = '是我要的' then <br>&nbsp; &nbsp; HEdt := hWnd;<br>&nbsp; Result:=TRUE;<br>end;<br><br>procedure TFindForm.Button2Click(Sender: TObject);<br>var<br>&nbsp; H : HWnd;<br>&nbsp; I : Integer;<br>begin<br>&nbsp; H := FindWindow( nil, '程序Caption');<br>&nbsp; if H = 0 then exit;<br>&nbsp; EnumChildWindows( H, @EnumerateChildWindows, 0 );<br>&nbsp; SendMessage( HEdt, WM_SETTEXT, 255, LongInt(PChar('afdasf');<br>&nbsp; SendMessage( HBtn, BM_CLICK, 0, 0 );<br>end;
 
function fwc(p:integer;c:cstring;i:integer):integer;<br>begin<br>&nbsp; &nbsp;fwcwnd:=0;<br>strpcoy(cmath,c);<br>cnt:=i;<br>&nbsp; enumchildwindow(p,@enumchildproc,0);//参照上面 要指向回调函数<br>
 
多人接受答案了。
 
我想收藏一下
 
后退
顶部