C#System.Windows.Automation获取元素文本 [英] C# System.Windows.Automation get element text

查看:84
本文介绍了C#System.Windows.Automation获取元素文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#中的Automation从应用程序控件中获取文本/标签.

I am trying to get text/labels from application controls with Automation in C#.

到目前为止,我可以使用此功能获取应用程序的AutomationElement树(例如记事本):

So far I am able to obtain AutomationElement tree of application (for example Notepad) with this function:

    private void WalkControlElements(AutomationElement rootElement, TreeNode treeNode)
    {
        AutomationElement elementNode = TreeWalker.ContentViewWalker.GetFirstChild(rootElement);;

        while (elementNode != null)
        {
            TreeNode childTreeNode = treeNode.Nodes.Add(elementNode.Current.ControlType.LocalizedControlType);

            // here I want to get text from 'elementNode'

            WalkControlElements(elementNode, childTreeNode);
            elementNode = TreeWalker.ControlViewWalker.GetNextSibling(elementNode);
        }
    }

我试图按照本文 http://msdn.microsoft.com/zh-cn/library/ms788751(v=vs.110).aspx ,但它只能获取文本属性,如字体名称,字体粗细等.

I tried to follow this article http://msdn.microsoft.com/en-us/library/ms788751(v=vs.110).aspx but it only can get text attributes as font name, font weight and so on.

有人可以为我指出正确的过程如何使用自动化获取元素文本吗?

Could anybody point me to the right procedure how to get element text with Automation?

推荐答案

该示例向您展示如何获取文本属性,即有关UI中文本显示的信息,而不是实际的显示的文字.要获得一般应用程序的所有实际显示文本,要比它第一次出现要困难得多.

That sample is showing you how to get text attributes, i.e. information about the display of the text in the UI, not the actual displayed text. Getting all the actual displayed text for a general application is more difficult that it might first appear.

由于存在多种获取文本的方法,并且应用程序和控件的支持不一致,这一事实使该操作变得很困难.有两种有用的模式: ValuePattern TextPattern .按照惯例,Name属性包含向用户显示的文本,但是对它的遵循不一致.以下是我在UI自动化中用于测试的辅助方法.它基本上会通过那些模式检查控件的支持,然后退回到Name.

It is made difficult by the fact that there are several ways get text and there is inconsistent support by applications and controls. There are two patterns that are of some use, ValuePattern and TextPattern. By convention the Name property contains text displayed to the user however adherence to this is inconsistent. Below is a helper method that I've used in UI automation for testing. It basically goes through those patterns checking the control for support and falls back to the Name.

public static class AutomationExtensions
{
    public static string GetText(this AutomationElement element)
    {
        object patternObj;
        if (element.TryGetCurrentPattern(ValuePattern.Pattern, out patternObj))
        {
            var valuePattern = (ValuePattern)patternObj;
            return valuePattern.Current.Value;
        }
        else if (element.TryGetCurrentPattern(TextPattern.Pattern, out patternObj))
        {
            var textPattern = (TextPattern)patternObj;
            return textPattern.DocumentRange.GetText(-1).TrimEnd('\r'); // often there is an extra '\r' hanging off the end.
        }
        else
        {
            return element.Current.Name;
        }
    }
}

这有助于从简单的控件(例如标签,文本框(香草文本框和Richtextbox)和按钮)中提取文本.诸如列表框和组合框(尤其是WPF中的控件)之类的控件可能更容易被欺骗,因为它们的项可以虚拟化,因此直到用户与它们进行交互之前,它们可能不存在于自动化树中.您可能只想对某些您知道包含文本的UI自动化控件类型(例如编辑",文本"和文档")进行过滤和调用此方法.

This takes care of getting the text out of simple controls like labels, textboxes (both vanilla textbox and richtextbox), and buttons. Controls like listboxes and comboboxes (esp. in WPF) can be tricker because their items can be virtualized so they may not exist in the automation tree until the user interacts with them. You may want to filter and call this method only on certain UI Automation control types like Edit, Text, and Document which you know contain text.

这篇关于C#System.Windows.Automation获取元素文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆