【问题标题】:How to get the text value from Point using Microsoft UI Automation?如何使用 Microsoft UI 自动化从 Point 获取文本值?
【发布时间】:2015-12-15 20:21:56
【问题描述】:
我正在寻找提高在重点AutomationElement(点)上查找文本的性能的方法。
我已经有这样的代码。它使用 UIAComWrapper 并且非常慢。
public static string GetValueFromNativeElementFromPoint(Point p)
{
var element = UIAComWrapper::System.Windows.Automation.AutomationElement.FromPoint(p);
var pattern =
((UIAComWrapper::System.Windows.Automation.LegacyIAccessiblePattern)
element.GetCurrentPattern(UIAComWrapper::System.Windows.Automation.LegacyIAccessiblePattern.Pattern));
return pattern.Current.Value;
}
【问题讨论】:
标签:
c#
ui-automation
microsoft-ui-automation
white-framework
【解决方案1】:
找到解决方案。 2 秒 vs 7 秒使用 UIAComWrapper。
public static string GetValueFromNativeElementFromPoint2(Point p)
{
var element = AutomationElement.FromPoint(p);
object patternObj;
if (element.TryGetCurrentPattern(ValuePattern.Pattern, out patternObj))
{
var valuePattern = (ValuePattern) patternObj;
return valuePattern.Current.Value;
}
return null;
}
【解决方案2】:
另一种选择是尝试通过 tlbimp 工具生成的托管包装器使用本机 Windows UIA API。作为测试,我只是通过以下方式生成了包装器...
"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64\tlbimp.exe" c:\windows\system32\uiautomationcore.dll /out:Interop .UIAutomationCore.dll
然后我编写了下面的代码并在 C# 项目中引用了包装器。
代码在感兴趣的点获取 UIA 元素,并请求在我们获取元素时缓存元素是否支持 Value 模式的信息。这意味着一旦我们有了元素,我们就可以了解它是否支持 Value 模式,而无需进行另一个跨进程调用。
比较此代码在您感兴趣的元素上运行的性能(相对于托管的 .NET UIA API 和 UIAComWrapper 的使用)会很有趣。
IUIAutomation uiAutomation = new CUIAutomation8();
int patternIdValue = 10002; // UIA_ValuePatternId
IUIAutomationCacheRequest cacheRequestValuePattern = uiAutomation.CreateCacheRequest();
cacheRequestValuePattern.AddPattern(patternIdValue);
IUIAutomationElement element = uiAutomation.ElementFromPointBuildCache(pt, cacheRequestValuePattern);
IUIAutomationValuePattern valuePattern = element.GetCachedPattern(patternIdValue);
if (valuePattern != null)
{
// Element supports the Value pattern...
}