繁体   English   中英

如何使用 UI 自动化从 ListView 或类似控件中获取文本?

[英]How to get text out of a ListView or similar control using UI Automation?

我正在尝试从外部应用程序中抓取类似 ListView 的控件。 现在我正在使用System.Windows.Automation 使用 AutoIt v3,我提取了有关我想从中抓取文本的确切控件的以下信息:

>>>> Control <<<<
Class:  WindowsForms10.Window.8.app.0.34f5582_r6_ad1
Instance:   20
ClassnameNN:    WindowsForms10.Window.8.app.0.34f5582_r6_ad120
Name:   
Advanced (Class):   [CLASS:WindowsForms10.Window.8.app.0.34f5582_r6_ad1; INSTANCE:20]
ID: 1510520
Text:   
Position:   182, 164
Size:   1411, 639
ControlClick Coords:    300, 202
Style:  0x56010000
ExStyle:    0x00000000
Handle: 0x0000000000170C78

现在,我注意到ID = 1510520并且通过使用它我将能够获得控制权

AutomationElement element = AutomationElement.FromHandle(1510520);

该控件看起来像一个 ListView 或类似的,但我不能用它做任何其他事情。

LisrView 类似控件

我现在如何获取此控件的内容?

更新:
感谢 Jimi 的推荐,Windows 10 SDK 中的 inspect.exe 效果最好! 我能够深入到 DataGridView。

在此处输入图片说明

我假设您可以找到包含从中提取数据的 DataGridView 的 Window。 GeDataGridViewDataTable()方法需要那个窗口的句柄。

让我们分解这些方法:

要在其句柄已知时获取感兴趣的窗口的 AutomationElement,我们可以使用window = AutomationElement.FromHandle([Window Handle])

▶ 这里我使用AndCodition,因为您可能有 ProcessID 和 Window Title,因此您可以使用AutomationElement.ProcessIdPropertyAutomationElement.NameProperty作为条件,而不是使用AutomationElement.ControlTypePropertyAutomationElement.NativeWindowHandleProperty进行过滤。

如果找到 Window,则TreeScope.SubTree范围中的第一个子元素(该窗口中的所有 UI 元素)将被解析以查找 Table ( ControlType.Table ) 类型的第一个元素。

▶ 当然,那个 Window 可能承载多个 DataGridView:在这种情况下,我们可以使用FindAll()而不是FindFirst() ,然后使用其他条件(列数,标题的文本,标题的内容FindFirst()确定哪个是哪个单元格、位置、大小、父容器等)。

当找到感兴趣的 DataGridView 时,我们可以提取其 Cells 的内容。
这是第二种方法, GetDataGridViewRowsCollection()

  • 第一个OrCondition过滤掉 DataGridView 滚动条和网格的其他可能的子控件(自定义可能包括一些)。
  • 之后,我们检查 DGV 是否有 Header:如果有,第一个子 Row 元素名称是Top Row 然后,我们可以使用标题文本来命名将存储提取的数据的 DataTable 的列。 否则,只需添加一些默认名称。
  • 然后,对于每个 Row 元素,我们枚举它的子元素,代表 Cells。 我添加了一个NotCondition过滤器来排除 Row Header Cell, ControlType.Header ,如果有的话。
  • 然后我们迭代 Cell 的集合,使用GetCurrentPropertyValue()方法提取它们的值,将 Property Type 设置为ValuePattern.ValueProperty ,将这些值添加到一个 List 中,该 List 将提供DataTable.Rows.Add()param数组参数。

private DataTable GeDataGridViewDataTable(IntPtr windowHwnd)
{
    var condition = new AndCondition(
        new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
        new PropertyCondition(AutomationElement.NativeWindowHandleProperty, windowHwnd.ToInt32())
    );
    var window = AutomationElement.RootElement.FindFirst(TreeScope.Children, condition);
    if (window == null) return null;
    var dgv = window.FindFirst(TreeScope.Subtree, 
        new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Table));

    if (dgv == null) return null;
    var dt = GetDataGridViewRowsCollection(dgv);
    return dt;
}

private DataTable GetDataGridViewRowsCollection(AutomationElement dgv)
{
    var dt = new DataTable();

    // Skips ScrollBars and other child elements
    var condition = new OrCondition(
        new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom),
        new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Header)
    );

    var rows = dgv.FindAll(TreeScope.Children, condition).OfType<AutomationElement>().ToList();
    bool hasColumnHeader = (rows[0].Current.Name == "Top Row");

    // First element is the Header (if there's one)
    var dgvHeaderColumns = rows[0].FindAll(TreeScope.Children, Condition.TrueCondition);
        
    // Skip the Top/Left header
    for (int i = 1; i < dgvHeaderColumns.Count; i++) {
        dt.Columns.Add(hasColumnHeader ? dgvHeaderColumns[i].Current.Name : "Column"+i);
    }

    // Skips the Row Header, if any
    var notCondition = new NotCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Header));
    foreach (AutomationElement row in rows) {
        var cells = row.FindAll(TreeScope.Children, notCondition);
        var values = new List<object>();
        foreach (AutomationElement cell in cells) {
            values.Add(cell.GetCurrentPropertyValue(ValuePattern.ValueProperty));
        }
        dt.Rows.Add(values.ToArray());
    }
    return dt;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM