gpt4 book ai didi

c# - 使用 UI 自动化获取 ComboBox 中的项目时出现奇怪的结果

转载 作者:行者123 更新时间:2023-11-30 16:35:21 25 4
gpt4 key购买 nike

我们使用下面的代码从另一个应用程序窗口内的 ComboBox 中获取项目列表。此代码适用于我们测试过此代码的任何其他应用程序中的 ComboBoxes(正确检索项目列表),但是对于此特定应用程序,为每个 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);
}

这是我们最终为这个应用程序制作的屏幕截图。

alt text

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

最佳答案

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

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

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

关于c# - 使用 UI 自动化获取 ComboBox 中的项目时出现奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1929326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com