C# System.Windows.Automation get element text

前端 未结 2 925
北海茫月
北海茫月 2020-12-16 04:29

I am trying to get text/labels from application controls with Automation in C#.

So far I am able to obtain AutomationElement tree of application (for example Notepad

2条回答
  •  借酒劲吻你
    2020-12-16 04:58

    Mike Zboray answer works fine. In case you have access to pattern-Matching, here is the same (condensed) code :

    public static class AutomationExtensions
    {
        public static string GetText(this AutomationElement element)
        => element.TryGetCurrentPattern(ValuePattern.Pattern, out object patternValue) ? ((ValuePattern)patternValue).Current.Value
            : element.TryGetCurrentPattern(TextPattern.Pattern, out object patternText) ? ((TextPattern)patternText).DocumentRange.GetText(-1).TrimEnd('\r') // often there is an extra '\r' hanging off the end.
            : element.Current.Name;
    }
    

提交回复
热议问题