Windows UI 自动化未显示所有子元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7238883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:57:31  来源:igfitidea点击:

Windows UI Automation not showing all child elements?

c#windowsui-automation

提问by Ozzah

I have a TreeView control on my form, and I'm recursively going through the elements of another window starting with the window itself. I'm using this to find the elements:

我的窗体上有一个 TreeView 控件,我从窗口本身开始递归遍历另一个窗口的元素。我正在使用它来查找元素:

getRecursiveElements(AutomationElement parent)
{
  children = parent.FindAll(TreeScope.Children, Condition.TrueCondition);

  foreach (AutomationElement child in children)
  {
    addToTreeView(child);
    getRecursiveElements(child);
  }
}

Generally speaking, the code works quite well in most cases. The tree is populated and I have a bit of other supporting code allowing me to double click, for example, an element in the tree-view and it will highlight that element on the target form.

一般来说,代码在大多数情况下都运行良好。树被填充,我有一些其他支持代码允许我双击,例如,树视图中的一个元素,它将在目标表单上突出显示该元素。

The issue I'm having is that, while it generates an awesome tree, there are still some elements missing for certain target programs.

我遇到的问题是,虽然它生成了一个很棒的树,但某些目标程序仍然缺少一些元素。

What possible reason could there be for this, and is there any way to get around it? If I call EnumChildWindows()from user32.dll will that have the same problem?

这有什么可能的原因,有什么办法可以解决吗?如果我EnumChildWindows()从 user32.dll调用会出现同样的问题吗?

采纳答案by David Heffernan

Not all programs use separate windowed controls for all their logical children. Mostly this depends on the GUI framework used.

并非所有程序都对其所有逻辑子项使用单独的窗口控件。这主要取决于所使用的 GUI 框架。

As an extreme example, Qt uses a single window for each top-level window. It then paints all the widgets on the form from the form's WM_PAINT message handler.

作为一个极端的例子,Qt 为每个顶级窗口使用一个窗口。然后它从窗体的 WM_PAINT 消息处理程序中绘制窗体上的所有小部件。

Programs that take this approach are typically impossible to automate through generic methods.

采用这种方法的程序通常不可能通过通用方法实现自动化。

It sounds like you have encountered an application that uses some windowed controls but also uses custom controls with a single window for what appears to be multiple widgets. Again this is quite common.

听起来您遇到了一个应用程序,它使用一些窗口控件,但也使用带有单个窗口的自定义控件来显示多个小部件。这也是很常见的。

回答by antiduh

Could you give a better example of what fails? Thinking about the problem, it may be that the 'element' in the other form is being drawn manually, and so doesn't have distinct registered handles for everything.

你能举一个更好的例子来说明失败的原因吗?考虑这个问题,可能是另一种形式中的“元素”是手动绘制的,因此对于所有内容都没有明确的注册句柄。