繁体   English   中英

如何使用Microsoft UI Automation从Point获取文本值?

[英]How to get the text value from Point using Microsoft UI Automation?

我正在寻找提高在关注的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;
    }

另一种选择是尝试通过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模式而无需进行另一个cross-proc调用。

比较在您感兴趣的元素上工作的代码的性能,相对于托管的.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...
}

找到解决方案 使用UIAComWrapper 2秒vs 7秒

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;
}

暂无
暂无

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

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