繁体   English   中英

MS UI自动化-如何从ControlType.text获取文本

[英]MS UI Automation - How to get text from ControlType.text

我有一个小型Windows应用程序,上面有一系列标签。 该应用程序将被全球化,并且这些标签上的文本可能会被截断。 我正在尝试自动识别这些标签上的截断文本。

对于其他控件,我可以使用TextPattern.Pattern,通过它可以找到控件内的可见文本和实际文本。 但是对于标签(ControlType.text),不支持TextPattern。 如何使用UI自动化找到这些标签的可见文本。

这是我尝试的代码。 如果我将控件类型作为Document传递,它将起作用。 但是对于Text控件类型,它提供了不受支持的模式异常。

private String TextFromSelection(AutomationElement target, Int32 length)
        {
            // Specify the control type we're looking for, in this case 'Document'
            PropertyCondition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text);

            // target --> The root AutomationElement.
            AutomationElement textProvider = target.FindFirst(TreeScope.Descendants, cond);

            TextPattern textpatternPattern = textProvider.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

            if (textpatternPattern == null)
            {
                Console.WriteLine("Root element does not contain a descendant that supports TextPattern.");
                return null;
            }


            var test = textpatternPattern.DocumentRange.GetText(-1).TrimEnd('\r');

            var tpr = textpatternPattern.GetVisibleRanges();
            var txt = tpr[0].GetText(-1);

            return txt;
        }

标签元素是否支持文本模式,将受到所使用的UI框架的影响。 例如,Win32 Run dlg中的标签不支持文本模式,但Windows 10 XAML计算器中的标签支持。 下图显示了Inspect SDK工具,该报告报告“没有历史记录”标签支持文本模式。

需要特别注意的是,UI框架(如果应用程序直接实现UIA文本模式,则该应用程序是否包含该应用程序)是否包含截断的文本,该文本在您调用IUIAutomationTextPattern :: GetVisibleRanges()时返回给您的数据取决于该框架(或应用)本身。 例如,在Windows 10上运行的WordPad不包含被剪切掉的文本,但是Word 2013确实返回了被剪切的文本。

谢谢,

家伙

在此处输入图片说明

您应该能够简单地使用element.Current.Name (element是标签的AutomationElement实例)。

这是UISpy检索标签文本的示例:

在此处输入图片说明

暂无
暂无

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

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