////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // InvokeLinkInternal() // // Invoke a hyperlink in the browser window. // // Runs on the background thread. // ////////////////////////////////////////////////////////////////////////////////////////////////////////////// private void InvokeLinkInternal(IUIAutomationElement elementLink, bool fUseCache) { if (elementLink != null) { IUIAutomationInvokePattern pattern = null; int iPatternId = _patternIdInvoke; // Will we be calling the Invoke() method through the Invoke pattern. // So first get the pattern for the hyperlink element. if (fUseCache) { // This does not result in a cross-proc call here, and should not fail. pattern = (IUIAutomationInvokePattern)elementLink.GetCachedPattern(iPatternId); } else { // This will fail if the element no longer exists. try { pattern = (IUIAutomationInvokePattern)elementLink.GetCurrentPattern(iPatternId); } catch { // If an exception is throw trying to access the element, do nothing. This will // occur if the element no longer exists, (eg the browser window has been closed.) } } if (pattern != null) { pattern.Invoke(); } } }
/// <summary> /// Get the Instance of specific pattern. /// if there is no matching pattern, it is wrapped up as Unknown. /// </summary> /// <param name="e"></param> /// <param name="id"></param> /// <param name="name"></param> /// <returns></returns> public static A11yPattern GetPatternInstance(A11yElement e, IUIAutomationElement uia, int id, string name) { try { dynamic pt = null; if (PatternType.GetInstance().Exists(id)) { pt = uia.GetCachedPattern(id); } if (pt != null) { switch (id) { case PatternType.UIA_AnnotationPatternId: return(new AnnotationPattern(e, pt)); case PatternType.UIA_CustomNavigationPatternId: return(new CustomNavigationPattern(e, pt)); case PatternType.UIA_DockPatternId: return(new DockPattern(e, pt)); case PatternType.UIA_DragPatternId: return(new DragPattern(e, pt)); case PatternType.UIA_DropTargetPatternId: return(new DropTargetPattern(e, pt)); case PatternType.UIA_ExpandCollapsePatternId: return(new ExpandCollapsePattern(e, pt)); case PatternType.UIA_GridItemPatternId: return(new GridItemPattern(e, pt)); case PatternType.UIA_GridPatternId: return(new GridPattern(e, pt)); case PatternType.UIA_InvokePatternId: return(new InvokePattern(e, pt)); case PatternType.UIA_ItemContainerPatternId: return(new ItemContainerPattern(e, pt)); case PatternType.UIA_LegacyIAccessiblePatternId: return(new LegacyIAccessiblePattern(e, pt)); case PatternType.UIA_MultipleViewPatternId: return(new MultipleViewPattern(e, pt)); case PatternType.UIA_ObjectModelPatternId: return(new ObjectModelPattern(e, pt)); case PatternType.UIA_RangeValuePatternId: return(new RangeValuePattern(e, pt)); case PatternType.UIA_ScrollItemPatternId: return(new ScrollItemPattern(e, pt)); case PatternType.UIA_ScrollPatternId: return(new ScrollPattern(e, pt)); case PatternType.UIA_SelectionItemPatternId: return(new SelectionItemPattern(e, pt)); case PatternType.UIA_SelectionPatternId: return(new SelectionPattern(e, pt)); case PatternType.UIA_SelectionPattern2Id: return(new SelectionPattern2(e, pt)); case PatternType.UIA_SpreadsheetPatternId: return(new SpreadsheetPattern(e, pt)); case PatternType.UIA_SpreadsheetItemPatternId: return(new SpreadsheetItemPattern(e, pt)); case PatternType.UIA_StylesPatternId: return(new StylesPattern(e, pt)); case PatternType.UIA_SynchronizedInputPatternId: return(new SynchronizedInputPattern(e, pt)); case PatternType.UIA_TableItemPatternId: return(new TableItemPattern(e, pt)); case PatternType.UIA_TablePatternId: return(new TablePattern(e, pt)); case PatternType.UIA_TextChildPatternId: return(new TextChildPattern(e, pt)); case PatternType.UIA_TextEditPatternId: return(new TextEditPattern(e, pt)); case PatternType.UIA_TextPatternId: return(new TextPattern(e, pt)); case PatternType.UIA_TextPattern2Id: return(new TextPattern2(e, pt)); case PatternType.UIA_TogglePatternId: return(new TogglePattern(e, pt)); case PatternType.UIA_TransformPatternId: return(new TransformPattern(e, pt)); case PatternType.UIA_TransformPattern2Id: return(new TransformPattern2(e, pt)); case PatternType.UIA_ValuePatternId: return(new ValuePattern(e, pt)); case PatternType.UIA_VirtualizedItemPatternId: return(new VirtualizedItemPattern(e, pt)); case PatternType.UIA_WindowPatternId: return(new WindowPattern(e, pt)); } } return(new UnKnownPattern(e, id, name)); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { ex.ReportException(); return(null); } #pragma warning restore CA1031 // Do not catch general exception types }
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); }