public AutomationElement FindFirst(TreeScope scope, Condition condition) { Validate.ArgumentNotNull(parameter: condition, parameterName: nameof(condition)); var firstBuildCache = IUIAutomationElement.FindFirstBuildCache(scope: UiaConvert.Convert(treeScope: scope), condition: condition.IUIAutomationCondition, cacheRequest: DefaultCacheRequest.IUIAutomationCacheRequest); return(firstBuildCache != null ? new AutomationElement(autoElement: firstBuildCache) : null); }
public string GetMail() { string strMailContent = ""; // Try to find a Windows Live Mail window for composing and reading e-mails. // Using the Spy tool, the class of the main window can be found. This test // app assumes there's only one Windows Live Mail window of interest. IntPtr hwnd = Win32.FindWindow("ATH_Note", null); if (hwnd != IntPtr.Zero) { // We found a window, so get the UIA element associated with the window. IUIAutomationElement elementMailAppWindow = _automation.ElementFromHandle( hwnd); // Find an element somewhere beneath the main window element in the UIA // tree which represents the main area where the e-mail content is shown. // Using the Inspect SDK tool, we can see that the main e-mail content // is contained within an element whose accessible name is "NoteWindow". // So create a condition such that the FindFirst() call below will only // return an element if its name is "NoteWindow". IUIAutomationCondition conditionNote = _automation.CreatePropertyCondition( _propertyIdName, "NoteWindow"); IUIAutomationElement elementNoteWindow = elementMailAppWindow.FindFirst( TreeScope.TreeScope_Descendants, conditionNote); // As it happens, the actual element that supports the Text Pattern is // somewhere beneath the "NoteWindow" element in the UIA tree. Using // Inspect we can see that there is an element that supports the // Text Pattern. Once we have that element, we can avoid a cross-process // call to access the Text Pattern object by using cache request. IUIAutomationCacheRequest cacheRequest = _automation.CreateCacheRequest(); cacheRequest.AddPattern(_patternIdTextPattern); // Now find the element that supports the Text Pattern. This test app assumes // there’s only one element that can be returned which supports the Text Pattern. bool fTextPatternSupported = true; IUIAutomationCondition conditionTextPattern = _automation.CreatePropertyCondition( _propertyIdIsTextPatternAvailable, fTextPatternSupported); IUIAutomationElement elementMailContent = elementMailAppWindow.FindFirstBuildCache( TreeScope.TreeScope_Descendants, conditionTextPattern, cacheRequest); // Because the Text Pattern object is cached, we don't have to make a cross-process // call here to get object. Later calls which actually use methods and properties // on the Text Pattern object will incur cross-proc calls. IUIAutomationTextPattern textPattern = (IUIAutomationTextPattern) elementMailContent.GetCachedPattern( _patternIdTextPattern); // This test app is only interested in getting all the e-mail text, so we get that through // the DocumentRange property. A more fully featured app might be interested in getting a // collection of Text Ranges from the e-mail. Each range might relate to an individual // word or paragraph. Given that a provider which supports the Text Pattern allows a // client to find the bounding rectangles of these ranges, the client could choose to use // its own method of highlighting the text as the text is being spoken. IUIAutomationTextRange rangeDocument = textPattern.DocumentRange; // Pass -1 here because we're not interested in limiting the amount of text here. strMailContent = rangeDocument.GetText(-1); } return(strMailContent); }