使用 UI 自动化获取组合框中的项目时出现奇怪的结果

发布于 2024-08-15 06:51:27 字数 1282 浏览 7 评论 0原文

我们使用下面的代码从另一个应用程序窗口内的 ComboBox 中获取项目列表。此代码适用于我们测试此代码的任何其他应用程序中的 ComboBox(正确检索项目列表),但是对于此特定应用程序,为每个 ListItem 检索的 Name 属性是乱码。

这是代码:

using System.Windows.Automation;

var condition = new PropertyCondition(AutomationElement.NameProperty, "Change/Add/Delete Setting");
var condition2 = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window);
var condition3 = new AndCondition(new Condition[] {condition, condition2});
var window = AutomationElement.RootElement.FindFirst(TreeScope.Subtree, condition3);

condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ComboBox);
var combo = window.FindFirst(TreeScope.Subtree, condition);

condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem);
AutomationElementCollection children = combo.FindAll(TreeScope.Subtree, condition);

var comboItems = new List<string>();
foreach (AutomationElement child in children)
{
    comboItems.Add(child.Current.Name);
}

这是我们最终得到的这个应用程序的屏幕截图。

替代文本

  • 什么会导致Name属性出现这样的乱码?这可能是编码问题吗?
  • 我们如何才能获得每个项目的正确文本?

We are using the code below to get a list of items out of a ComboBox inside another application's window. This code works (correctly retrieves the list of items) for ComboBoxes in any other application we've tested this code on, however for this particular application the Name property retrieved for each ListItem is garbled.

Here is the code:

using System.Windows.Automation;

var condition = new PropertyCondition(AutomationElement.NameProperty, "Change/Add/Delete Setting");
var condition2 = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window);
var condition3 = new AndCondition(new Condition[] {condition, condition2});
var window = AutomationElement.RootElement.FindFirst(TreeScope.Subtree, condition3);

condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ComboBox);
var combo = window.FindFirst(TreeScope.Subtree, condition);

condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem);
AutomationElementCollection children = combo.FindAll(TreeScope.Subtree, condition);

var comboItems = new List<string>();
foreach (AutomationElement child in children)
{
    comboItems.Add(child.Current.Name);
}

And here is a screenshot of what we end up with for this one app.

alt text

  • What could cause the Name property to be garbled like this? Could this be an encoding problem?
  • How can we get the correct text for each item?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-08-22 06:51:27

如果此组合框具有 CBS_OWNERDRAWFIXEDCBS_OWNERDRAWVARIABLE 样式,或者包含的列表框具有 LBS_OWNERDRAWFIXEDLBS_OWNERDRAWVARIABLE 样式。那么控件根本不知道该文本。当应用程序使用其中一种样式时,每当控件需要绘制时,它都会收到 WM_DRAWITEM 消息,然后它会从其口袋中提取文本并在需要的地方绘制它。

这是一个技巧,允许应用程序快速轻松地更改列表框或组合框的内容,它主要在内容不稳定或有大量项目时使用。这是绕过列表框/组合框可以容纳的项目数量限制的一种方法。

使用 Spy++ 检查这些窗口上的样式。

If this combobox has the CBS_OWNERDRAWFIXED or CBS_OWNERDRAWVARIABLE style, or the contained listbox has the LBS_OWNERDRAWFIXED or LBS_OWNERDRAWVARIABLE style. then the text isn't known by the control at all. When an app uses one of these styles, it gets WM_DRAWITEM messages whenever the control needs to draw, then it pulls the text from it's pocket and draws it wherever it was asked to.

This is a trick that allows an application to quickly and easily change the contents of a listbox or combobox on the fly, it's mostly used when the contents are volatile or when there are LOTS of items. It's one way to get around the limit on the number of items an listbox/combobox can hold.

Use Spy++ to check the styles on these windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文