首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MS UI自动化-如何从ControlType.text获取文本

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

Stack Overflow用户
提问于 2015-09-09 19:13:42
回答 2查看 3.7K关注 0票数 2

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

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

这是我尝试过的代码。如果我将控件类型作为文档传递,它就会起作用。但是对于文本控件类型,它给出了一个不受支持的模式异常。

代码语言:javascript
复制
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;
        }
EN

回答 2

Stack Overflow用户

发布于 2015-09-15 08:13:32

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

值得注意的是,当您调用IUIAutomationTextPattern::GetVisibleRanges()时,UI框架(如果应用程序直接实现UIA文本模式,则是应用程序)是否在返回给您的数据中包含截断的文本,这取决于框架(或应用程序)本身。例如,在Windows10上运行的WordPad不包含剪切出视图的文本,但Word 2013会返回剪切后的文本。

谢谢,

盖伊

票数 3
EN

Stack Overflow用户

发布于 2015-09-10 14:08:15

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32478038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档