使用 UI 自动化获取组合框中的项目时出现奇怪的结果
我们使用下面的代码从另一个应用程序窗口内的 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.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果此组合框具有
CBS_OWNERDRAWFIXED
或CBS_OWNERDRAWVARIABLE
样式,或者包含的列表框具有LBS_OWNERDRAWFIXED
或LBS_OWNERDRAWVARIABLE
样式。那么控件根本不知道该文本。当应用程序使用其中一种样式时,每当控件需要绘制时,它都会收到 WM_DRAWITEM 消息,然后它会从其口袋中提取文本并在需要的地方绘制它。这是一个技巧,允许应用程序快速轻松地更改列表框或组合框的内容,它主要在内容不稳定或有大量项目时使用。这是绕过列表框/组合框可以容纳的项目数量限制的一种方法。
使用 Spy++ 检查这些窗口上的样式。
If this combobox has the
CBS_OWNERDRAWFIXED
orCBS_OWNERDRAWVARIABLE
style, or the contained listbox has theLBS_OWNERDRAWFIXED
orLBS_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.