Delphi通过EnumWindows,Enumchildwindows或Spy ++检查TGroupBox内的外部TLabel

And*_*Boc 0 delphi tlabel spy++ delphi-xe2 tcombobox

我正在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;
Run Code Online (Sandbox Code Playgroud)

一切都运行良好,除了对象TGroupBox之后递归停止.但是控制TGroupBox包含其他元素(TLabel).

事实上,即使在Delphi中编写一个简单的应用程序,通过在Form中包含一个TGroupBox然后在TGroupBox中包含一个TLabel,启动应用程序并使用Spy ++(或使用Tool Autoit AU3Info)检查它,您无法进入TGroupBox:内部的TLabel未经检查.

有没有办法在TGroupBox中找到TLabel控件?

Dav*_*nan 6

这不是组框控件的问题.问题是TLabel控件没有窗口化.没有与之关联的窗口句柄,因此Spy ++ EnumChildWindows等无法找到它.

  • @AndreaBoc:更清楚地说-TLabel没有自己的HWND,因此您将无法与它进行外部交互,除非该应用程序手动公开其自身的访问权限,例如通过实现IAccessible接口(默认情况下,VCL不执行)。 (2认同)