Search code examples
c#ui-automationmicrosoft-ui-automationwhite-framework

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


I'm looking for the ways to improve performance of finding text on focused AutomationElement (point). I already have code like this. It uses UIAComWrapper and pretty slow.

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

Solution

  • Found solution. 2 seconds vs 7 seconds using 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;
    }