【发布时间】:2015-12-05 08:00:02
【问题描述】:
我有一个小型 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;
}
【问题讨论】:
标签: c# automation ui-automation microsoft-ui-automation