2015-09-18 33 views
1

我正在尋找提高搜索文本集中性的方法AutomationElement(point)。 我已經有這樣的代碼。它使用UIAComWrapper並且非常慢。如何使用Microsoft UI Automation從Point獲取文本值?

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

回答

1

找到解決辦法。 使用UIAComWrapper 2秒對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; 
} 
2

另一個選擇是嘗試使用本地Windows UIA API通過tlbimp工具生成的託管包裝器。作爲一個測試,我只是以下面的方式生成包裝...

「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模式,而不必進行另一個跨處理的調用。

將您感興趣的元素相對於受管.NET .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... 
} 
相關問題