/// <summary> /// Populate siblings /// </summary> /// <param name="parentNode"></param> /// <param name="poiNode"></param> /// <param name="startId"></param> private int PopulateSiblingTreeNodes(A11yElement parentNode, A11yElement poiNode) { int childId = 1; IUIAutomationTreeWalker walker = this.TreeWalker; IUIAutomationElement child = null; if ((IUIAutomationElement)parentNode.PlatformObject != null) { try { child = walker.GetFirstChildElement((IUIAutomationElement)parentNode.PlatformObject); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { ex.ReportException(); child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } #pragma warning restore CA1031 // Do not catch general exception types while (child != null) { #pragma warning disable CA2000 // Use recommended dispose patterns var childNode = new DesktopElement(child, true, false); #pragma warning restore CA2000 // Use recommended dispose patterns childNode.PopulateMinimumPropertiesForSelection(); if (childNode.IsSameUIElement(poiNode) == false) { childNode.UniqueId = childId++; childNode.Parent = parentNode; childNode.TreeWalkerMode = this.TreeWalkerMode; this.Items.Add(childNode); } else { childNode = poiNode as DesktopElement; } parentNode.Children.Add(childNode); try { child = walker.GetNextSiblingElement(child); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { ex.ReportException(); child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } #pragma warning restore CA1031 // Do not catch general exception types } } return(childId); }
public AutomationElement FindMatchingChild(Predicate <AutomationElement> pred) { IUIAutomationTreeWalker walker = factory.CreateTreeWalker(); try { IUIAutomationElement elem; walker.GetFirstChildElement(pElement, out elem); while (elem != null) { AutomationElement ae = new AutomationElement(elem, factory); if (pred(ae)) { return(ae); } IUIAutomationElement next; walker.GetNextSiblingElement(elem, out next); elem = next; } return(null); } finally { if (walker != null) { Marshal.ReleaseComObject(walker); } } }
private IUIAutomationElement GetNexSibbling(IUIAutomationTreeWalker treeWalker, IUIAutomationElement element) { if (element == null) { return(null); } return(treeWalker?.GetNextSiblingElement(element)); }
/// <summary> /// Populate tree by retrieving all children at once. /// </summary> /// <param name="rootNode"></param> private void PopulateChildrenTreeNode(A11yElement rootNode, int startId) { int childId = startId; IUIAutomationTreeWalker walker = A11yAutomation.GetTreeWalker(this.WalkerMode); IUIAutomationElement child = (IUIAutomationElement)rootNode.PlatformObject; if (child != null) { try { child = walker.GetFirstChildElement(child); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { ex.ReportException(); child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } #pragma warning restore CA1031 // Do not catch general exception types while (child != null) { #pragma warning disable CA2000 // childNode will be disposed by the parent node // Create child without populating basic property. it will be set all at once in parallel. var childNode = new DesktopElement(child, true, false) { UniqueId = childId++ }; #pragma warning restore CA2000 // childNode will be disposed by the parent node rootNode.Children.Add(childNode); childNode.Parent = rootNode; childNode.TreeWalkerMode = this.WalkerMode; this.Elements.Add(childNode); try { child = walker.GetNextSiblingElement(child); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { ex.ReportException(); child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } #pragma warning restore CA1031 // Do not catch general exception types } } Marshal.ReleaseComObject(walker); }
/// <summary> /// Populate siblings /// </summary> /// <param name="parentNode"></param> /// <param name="poiNode"></param> /// <param name="startId"></param> private int PopulateSiblingTreeNodes(A11yElement parentNode, A11yElement poiNode) { int childId = 1; IUIAutomationTreeWalker walker = this.TreeWalker; IUIAutomationElement child = null; if ((IUIAutomationElement)parentNode.PlatformObject != null) { try { child = walker.GetFirstChildElement((IUIAutomationElement)parentNode.PlatformObject); } catch (Exception ex) { ex.ReportException(); child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } while (child != null) { var childNode = new DesktopElement(child, true, false); childNode.PopulateMinimumPropertiesForSelection(); if (childNode.IsSameUIElement(poiNode) == false) { childNode.UniqueId = childId++; childNode.Parent = parentNode; childNode.TreeWalkerMode = this.TreeWalkerMode; this.Items.Add(childNode); } else { childNode = poiNode as DesktopElement; } parentNode.Children.Add(childNode); try { child = walker.GetNextSiblingElement(child); } catch (Exception ex) { ex.ReportException(); child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } } } return(childId); }
/// <summary> /// Populate tree by retrieving all children at once. /// </summary> /// <param name="rootNode"></param> /// <param name="parentNode"></param> /// <param name="startChildId"></param> private int PopulateChildrenTreeNode(A11yElement rootNode, A11yElement parentNode, int startChildId) { this.Elements.Add(rootNode); rootNode.Parent = parentNode; rootNode.TreeWalkerMode = this.WalkerMode; // set tree walker mode. IUIAutomationTreeWalker walker = A11yAutomation.GetTreeWalker(this.WalkerMode); IUIAutomationElement child = (IUIAutomationElement)rootNode.PlatformObject; if (child != null) { try { child = walker.GetFirstChildElement(child); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { ex.ReportException(); child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } #pragma warning restore CA1031 // Do not catch general exception types while (child != null && _elementCounter.TryIncrement()) { // Create child without populating basic property. it will be set all at once in parallel. var childNode = new DesktopElement(child, true, false); rootNode.Children.Add(childNode); childNode.Parent = rootNode; childNode.UniqueId = startChildId++; startChildId = PopulateChildrenTreeNode(childNode, rootNode, startChildId); try { child = walker.GetNextSiblingElement(child); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { ex.ReportException(); child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } #pragma warning restore CA1031 // Do not catch general exception types } } Marshal.ReleaseComObject(walker); return(startChildId); }
public AutomationElement GetNextSibling() { IUIAutomationTreeWalker walker = factory.CreateTreeWalker(); try { IUIAutomationElement elem; walker.GetNextSiblingElement(pElement, out elem); return((elem == null) ? null : new AutomationElement(elem, factory)); } finally { if (walker != null) { Marshal.ReleaseComObject(walker); } } }
/// <summary> /// Populate tree by retrieving all children at once. /// </summary> /// <param name="rootNode"></param> private void PopulateChildrenTreeNode(A11yElement rootNode, int startId) { int childId = startId; IUIAutomationTreeWalker walker = A11yAutomation.GetTreeWalker(this.WalkerMode); IUIAutomationElement child = (IUIAutomationElement)rootNode.PlatformObject; if (child != null) { try { child = walker.GetFirstChildElement(child); } catch (Exception ex) { child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } while (child != null) { // Create child without populating basic property. it will be set all at once in parallel. var childNode = new DesktopElement(child, true, false) { UniqueId = childId++ }; rootNode.Children.Add(childNode); childNode.Parent = rootNode; childNode.TreeWalkerMode = this.WalkerMode; this.Elements.Add(childNode); try { child = walker.GetNextSiblingElement(child); } catch (Exception ex) { child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } } } Marshal.ReleaseComObject(walker); }
private static IUIAutomationElement FirstControlUnder(IUIAutomationElement element, IUIAutomationTreeWalker walker, int controlType) { var child = walker.GetFirstChildElement(element); while (child != null) { if (child.CurrentControlType == controlType) { return(child); } var textElement = FirstControlUnder(child, walker, controlType); if (textElement != null) { return(textElement); } child = walker.GetNextSiblingElement(child); } return(null); }
public IEnumerable <AutomationElement> GetChildren() { IUIAutomationElement elem = null; IUIAutomationTreeWalker walker = factory.CreateTreeWalker(); try { try { walker.GetFirstChildElement(pElement, out elem); } catch (COMException) { if (elem != null) { Marshal.ReleaseComObject(elem); } yield break; } while (elem != null) { yield return(new AutomationElement(elem, factory)); IUIAutomationElement next = null; try { walker.GetNextSiblingElement(elem, out next); } catch (COMException) { if (next != null) { Marshal.ReleaseComObject(next); } yield break; } elem = next; } } finally { if (walker != null) { Marshal.ReleaseComObject(walker); } } }
////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // GetCurrentDataFromElement() // // Get the current name from a UIA element, (incurring the time cost of various a cross-proc calls). // If the element doesn't have a name, optionally try to find a name from the children of the element // by using a TreeWalker. // // Runs on the background thread. // ////////////////////////////////////////////////////////////////////////////////////////////////////////////// private string GetCurrentDataFromElement(IUIAutomationElement element, bool fSearchLinkChildren) { string strName = null; // Call back to the target app to retrieve the bounds of the hyperlink element. tagRECT rectBounds = element.CurrentBoundingRectangle; // If we're the hyperlink has a zero-size bounding rect, ignore the element. if ((rectBounds.right > rectBounds.left) && (rectBounds.bottom > rectBounds.top)) { // Get the name of the element, (again incurring a cross-proc call). This name will often // be the text shown on the screen. string strNameFound = element.CurrentName; if ((strNameFound != null) && (strNameFound.Length > 0)) { // We have a usable name. strName = strNameFound; } else { // The hyperlink has no usable name. Consider using the name of a child element of the hyperlink. if (fSearchLinkChildren) { // Use a Tree Walker here to try to find a child element of the hyperlink // that has a name. Tree walking is a time consuming action, so would be // avoided by a shipping app if alternatives like FindFirst, FindAll, or // BuildUpdatedCache could get the required data. IUIAutomationTreeWalker controlWalker = _automation.ControlViewWalker; IUIAutomationElement elementChild = controlWalker.GetFirstChildElement(element); while (elementChild != null) { string strNameChild = elementChild.CurrentName; if ((strNameChild != null) && (strNameChild.Length > 0)) { // Use the name of this child element. strName = strNameChild; break; } // Continue to the next child element. elementChild = controlWalker.GetNextSiblingElement(elementChild); } } } } // While this sample doesn't use the destination of the hyperlink, if it wanted // to get the destination, that might be available as the Value property of the // element. The Value property is part of the Value pattern, and so is accessed // through the Value pattern. // Check first whether the element supports the Value pattern. IUIAutomationValuePattern valuePattern = (IUIAutomationValuePattern)element.GetCurrentPattern(_patternIdValue); if (valuePattern != null) { string strValue = valuePattern.CurrentValue; // This is where the destination of the link would be used... } return(strName); }
private void GetDescendantItems(IUIAutomationElement element, string path) { IUIAutomationTreeWalker w = UIAManager.CurrentTreeWalker; Queue <Tuple <IUIAutomationElement, string> > q = new Queue <Tuple <IUIAutomationElement, string> >(); q.Enqueue(Tuple.Create(element, path)); while (q.Count > 0) { Tuple <IUIAutomationElement, string> t = q.Dequeue(); for (IUIAutomationElement e = w.GetFirstChildElement(t.Item1); e != null; e = w.GetNextSiblingElement(e)) { string p = JoinPath(t.Item2, Converter.RuntimeIdToString(e.GetRuntimeId())); WriteItemObject( item: UIElement.Wrap(e, p), path: p, isContainer: true); q.Enqueue(Tuple.Create(e, p)); } } }