我正在Delphi XE2中开发一个应用程序,该应用程序通过函数EnumWindows和EnumChildWindows检查同样用Delphi编写的正在运行的应用程序的窗口。
这是主代码(改编自以下示例:
http://www.swissdelphicenter.ch/torry/showcode.php?id=410
)
function EnumChildWindowsProc(Wnd: HWnd; Form: TForm1): Bool; export;
{$ifdef Win32} stdcall; {$endif}
var
Buffer: array[0..99] of Char;
begin
GetWindowText(Wnd, Buffer, 100);
if StrPas(Buffer) = '' then Buffer := 'Empty';
new(AWindows);
with AWindows^ do
begin
WindowHandle := Wnd;
WindowText := StrPas(Buffer);
end;
CNode := Form1.TreeView1.Items.AddChildObject(PNode,
AWindows^.WindowText + ':' +
IntToHex(AWindows^.WindowHandle, 8), AWindows);
if GetWindow(Wnd, GW_CHILD) = 0 then
begin
PNode := CNode;
Enumchildwindows(Wnd, @EnumChildWindowsProc, 0);
end;
Result := True;
end;
function EnumWindowsProc(Wnd: HWnd; Form: TForm1): Bool;
export; {$ifdef Win32} stdcall; {$endif}
var
Buffer: array[0..99] of Char;
begin
GetWindowText(Wnd, Buffer, 100);
if StrPas(Buffer) = '' then Buffer := 'Empty';
new(AWindows);
with AWindows^ do
begin
WindowHandle := Wnd;
WindowText := StrPas(Buffer);
end;
if Pos(Form1.edAppToFind.Text,AWindows^.WindowText) > 0 then // <- inspect child only for my Application
begin
PNode := Form1.TreeView1.Items.AddObject(nil, AWindows^.WindowText + ':' +
IntToHex(AWindows^.WindowHandle, 8), AWindows);
EnumChildWindows(Wnd, @EnumChildWindowsProc, 0);
end;
Result := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumWindowsProc, self.Handle);
end;
除了对象TGroupBox之外,所有操作都很好,递归在该对象之后停止。但是控件TGroupBox包含在其他元素(TLabel)内部。
事实上,即使是在Delphi中编写一个简单的应用程序,通过在表单中包含一个TGroupBox,然后在TGroupBox中添加一个TLabel,启动应用程序并用Spy++(或使用工具Autoit AU3Info)检查它,你也无法进入TGroupBox:内部的TLabel不会被检查。
有没有办法在TGroupBox中找到TLabel控件?