【问题标题】:Delphi inspect external TLabels inside a TGroupBox via EnumWindows, Enumchildwindows or Spy++Delphi 通过 EnumWindows、Enumchildwindows 或 Spy++ 检查 TGroupBox 内的外部 TLabels
【发布时间】:2013-09-19 10:28:51
【问题描述】:

我正在用 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 中编写一个简单的应用程序,通过在 Form 中包含一个 TGroupBox,然后在 TGroupBox 中包含一个 TLabel,启动应用程序并使用 Spy++(或使用 Autoit AU3Info 工具)检查它,您也无法进入TGroupBox:里面的TLabel没有被检查。

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

【问题讨论】:

  • TLabel 不是窗口。

标签: delphi delphi-xe2 spy++ tlabel tcombobox


【解决方案1】:

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

【讨论】:

  • 其实TLabel没有枚举,问题不是TComboBox而是TLabel...有办法在Window里面找到Label吗?
  • 这正是我要告诉你的。由于TLabel 没有窗口句柄,依赖EnumChildWindows 的外部工具(如AutoIT 和Spy++)找不到它。为自动化公开 UI 的标准方法是使用自动化界面。当前的标准是 UIAutomation。
  • 我必须检查第三方应用程序,所以如果应用程序包含简单的 TLabel,则无法使用 EnumWindow 或 EnumChildwindow 读取其上的文本,对吗?还有其他的阅读方式吗?
  • 除非应用程序公开自动化接口,否则您会被卡住。与供应商交谈。我想我已经回答了你提出的问题。
  • @AndreaBoc:为了更清楚 - TLabel 没有自己的 HWND,因此您将无法在外部与其交互,除非应用程序手动公开其自己的访问权限例如通过实现IAccessible 接口(VCL 默认不这样做)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多