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

标签 c# ui-automation microsoft-ui-automation white-framework

我正在寻找提高在聚焦的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 工具生成的托管包装器使用 native 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 元素,并请求在我们获取元素时缓存该元素是否支持值模式的信息。这意味着一旦我们有了该元素,我们就可以了解它是否支持值模式,而无需进行另一个跨过程调用。

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

关于c# - 如何使用 Microsoft UI Automation 从 Point 获取文本值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32656178/

相关文章:

C# 如何读取 mdb(MS Access) 文件,然后传入在数组中选择的检索列以供以后使用?

c# - 拖放时吞下异常

c# - 使用 UI 自动化获取 ComboBox 中的项目时出现奇怪的结果

java - Selenium 有条件等待和中间操作

wpf - Microsoft UI 自动化库与编码 UI 测试

white-framework - 使用 UIAutomation .NET 在桌面上查找所有窗口

c# - 后台 C# 中的线程

c# - Entity Framework 中的通用查询方法

c# - 测试 UI/将焦点设置在 TextBox 上

windows - 复选框(选中或未选中)