public static List <AutomationElement> FindAll(AutomationElement root, TreeScope scope, Condition condition, int processId) { return((List <AutomationElement>) STAHelper.Invoke( delegate() { int elementProcessId = (int)root.GetCurrentPropertyValue(AutomationElement.ProcessIdProperty); if (elementProcessId != processId) { // This happens when the element represents the desktop. // We could just filter using the ProcessIdProperty but this searches all nodes and *then* filters, // which is incredibly slow if we're searching a lot of nodes (i.e. TreeScope is Descendant). // Instead we find all direct children with the right process id and then search them inclusively. // Helpfully, there's a Subtree TreeScope which does what we want Condition processCondition = new PropertyCondition2(AutomationElement.ProcessIdProperty, processId); if (scope == TreeScope.Descendants) { List <AutomationElement> roots = AutomationExtensions.FindAllRaw(root, TreeScope.Children, processCondition); List <AutomationElement> mergedResults = new List <AutomationElement>(); foreach (AutomationElement currentRoot in roots) { mergedResults.AddRange(FindAll(currentRoot, TreeScope.Subtree, condition, processId)); } return mergedResults; } else { condition = (condition == null) ? processCondition : new AndCondition(condition, processCondition); } } if (condition == null) { condition = Condition.TrueCondition; } return AutomationExtensions.FindAllRaw(root, scope, condition); } )); }
public static List<AutomationElement> FindAll(AutomationElement root, TreeScope scope, Condition condition, int processId) { return (List<AutomationElement>)STAHelper.Invoke( delegate() { int elementProcessId = (int)root.GetCurrentPropertyValue(AutomationElement.ProcessIdProperty); if (elementProcessId != processId) { // This happens when the element represents the desktop. // We could just filter using the ProcessIdProperty but this searches all nodes and *then* filters, // which is incredibly slow if we're searching a lot of nodes (i.e. TreeScope is Descendant). // Instead we find all direct children with the right process id and then search them inclusively. // Helpfully, there's a Subtree TreeScope which does what we want Condition processCondition = new PropertyCondition2(AutomationElement.ProcessIdProperty, processId); if (scope == TreeScope.Descendants) { List<AutomationElement> roots = AutomationExtensions.FindAllRaw(root, TreeScope.Children, processCondition); List<AutomationElement> mergedResults = new List<AutomationElement>(); foreach (AutomationElement currentRoot in roots) mergedResults.AddRange(FindAll(currentRoot, TreeScope.Subtree, condition, processId)); return mergedResults; } else { condition = (condition == null) ? processCondition : new AndCondition(condition, processCondition); } } if (condition == null) condition = Condition.TrueCondition; return AutomationExtensions.FindAllRaw(root, scope, condition); } ); }
public ScrollAxis GetScrollAxis(OrientationType orientation) { return (ScrollAxis)STAHelper.Invoke( delegate() { if(!scrollAxes.ContainsKey(orientation)) { object pattern; if(AutomationElement.TryGetCurrentPattern(ScrollPattern.Pattern, out pattern)) { scrollAxes[orientation] = new ScrollPatternAxis((ScrollPattern)pattern, orientation); } else if(ControlType == ControlType.ScrollBar) { if(orientation == (OrientationType)this[AutomationElement.OrientationProperty]) scrollAxes[orientation] = new ScrollBarAxis(AutomationElement); else throw new ArgumentException("Cannot get "+orientation+" scroll-axis for a scrollbar that is not "+orientation); } else if (ControlType == ControlType.Pane && Class == "ScrollBar") { scrollAxes[orientation] = new PaneScrollAxis(AutomationElement); } else { Condition scrollBarCondition = new AndCondition( new PropertyCondition2(AutomationElement.OrientationProperty, orientation), new PropertyCondition2(AutomationElement.ControlTypeProperty, ControlType.ScrollBar) ); Condition scrollPaneCondition = new AndCondition( new PropertyCondition2(AutomationElement.ControlTypeProperty, ControlType.Pane), new PropertyCondition2(AutomationElement.ClassNameProperty, "ScrollBar") ); Condition scrollPatternCondition = new PropertyCondition2(AutomationElement.IsScrollPatternAvailableProperty, true); Condition condition = new OrCondition(scrollBarCondition, scrollPaneCondition, scrollPatternCondition); List<AutomationElement> matches = Twin.View.Search.FindAll(AutomationElement, TreeScope.Descendants, 1 ,condition, (int)AutomationElement.GetCurrentPropertyValue(AutomationElement.ProcessIdProperty)); for(int i=0; i<matches.Count; i++) { if(matches[i].GetCurrentPropertyValue(AutomationElement.ControlTypeProperty) != ControlType.Pane) continue; if((bool)matches[i].GetCurrentPropertyValue(AutomationElement.IsScrollPatternAvailableProperty)) continue; Rect bounds = (Rect)matches[i].GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty); if(orientation == OrientationType.Horizontal && bounds.Height > bounds.Width) matches.RemoveAt(i--); if(orientation == OrientationType.Vertical && bounds.Width > bounds.Height) matches.RemoveAt(i--); } if (matches.Count == 0) return null; if (matches.Count > 1) throw new ArgumentException("Scrollable Axis for element ambiguous"); return Element.Create(matches[0], processId).GetScrollAxis(orientation); } } return scrollAxes[orientation]; } ); }
public ScrollAxis GetScrollAxis(OrientationType orientation) { return((ScrollAxis)STAHelper.Invoke( delegate() { if (!scrollAxes.ContainsKey(orientation)) { object pattern; if (AutomationElement.TryGetCurrentPattern(ScrollPattern.Pattern, out pattern)) { scrollAxes[orientation] = new ScrollPatternAxis((ScrollPattern)pattern, orientation); } else if (ControlType == ControlType.ScrollBar) { if (orientation == (OrientationType)this[AutomationElement.OrientationProperty]) { scrollAxes[orientation] = new ScrollBarAxis(AutomationElement); } else { throw new ArgumentException("Cannot get " + orientation + " scroll-axis for a scrollbar that is not " + orientation); } } else if (ControlType == ControlType.Pane && Class == "ScrollBar") { scrollAxes[orientation] = new PaneScrollAxis(AutomationElement); } else { Condition scrollBarCondition = new AndCondition( new PropertyCondition2(AutomationElement.OrientationProperty, orientation), new PropertyCondition2(AutomationElement.ControlTypeProperty, ControlType.ScrollBar) ); Condition scrollPaneCondition = new AndCondition( new PropertyCondition2(AutomationElement.ControlTypeProperty, ControlType.Pane), new PropertyCondition2(AutomationElement.ClassNameProperty, "ScrollBar") ); Condition scrollPatternCondition = new PropertyCondition2(AutomationElement.IsScrollPatternAvailableProperty, true); Condition condition = new OrCondition(scrollBarCondition, scrollPaneCondition, scrollPatternCondition); List <AutomationElement> matches = Twin.View.Search.FindAll(AutomationElement, TreeScope.Descendants, 1, condition, (int)AutomationElement.GetCurrentPropertyValue(AutomationElement.ProcessIdProperty)); for (int i = 0; i < matches.Count; i++) { if (matches[i].GetCurrentPropertyValue(AutomationElement.ControlTypeProperty) != ControlType.Pane) { continue; } if ((bool)matches[i].GetCurrentPropertyValue(AutomationElement.IsScrollPatternAvailableProperty)) { continue; } Rect bounds = (Rect)matches[i].GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty); if (orientation == OrientationType.Horizontal && bounds.Height > bounds.Width) { matches.RemoveAt(i--); } if (orientation == OrientationType.Vertical && bounds.Width > bounds.Height) { matches.RemoveAt(i--); } } if (matches.Count == 0) { return null; } if (matches.Count > 1) { throw new ArgumentException("Scrollable Axis for element ambiguous"); } return Element.Create(matches[0], processId).GetScrollAxis(orientation); } } return scrollAxes[orientation]; } )); }