UI Automation通过相对位置来进行对象识别

下面这个类是用来辅助我们通过相对位置关系来找控件。

class LocationFinder 
    { 
        AutomationElement element; 
        public LocationFinder(AutomationElement element) 
        { 
            this.element = element; 
        } 
 
        public AutomationElement getWindow(AutomationElement element) 
        { 
            TreeWalker tWalker = TreeWalker.ControlViewWalker; 
            AutomationElement parentItem = tWalker.GetParent(element); 
            if (!parentItem.Current.ControlType.Equals(ControlType.Window)) 
            { 
                return getWindow(parentItem); 
            } 
            return parentItem; 
        } 
 
        public AutomationElement getSameXAutomationElement(ControlType controlType, LocationDirection direction) 
        { 
            List<AutomationElement> xelements = getSameXAutomationElement(controlType); 
            foreach (AutomationElement xelement in xelements) 
            { 
                if (xelement.Current.ControlType.Equals(controlType)) 
                { 
                    if ((xelement.Current.BoundingRectangle.Top - element.Current.BoundingRectangle.Top) > 2 && direction.Equals(LocationDirection.UP)) return xelement; 
                    if ((element.Current.BoundingRectangle.Top - xelement.Current.BoundingRectangle.Top) > 2 && direction.Equals(LocationDirection.DOWN)) return xelement; 
                } 
            } 
 
            return null; 
        } 
 
        public AutomationElement getSameYAutomationElement(ControlType controlType, LocationDirection direction) 
        { 
            List<AutomationElement> yelements = getSameYAutomationElement(controlType); 
            foreach (AutomationElement yelement in yelements) 
            { 
                if (yelement.Current.ControlType.Equals(controlType)) 
                { 
                    if ((yelement.Current.BoundingRectangle.Left - element.Current.BoundingRectangle.Left) > 2 && direction.Equals(LocationDirection.RIGHT)) return yelement; 
                    if ((element.Current.BoundingRectangle.Left - yelement.Current.BoundingRectangle.Left) > 2 && direction.Equals(LocationDirection.LEFT)) return yelement; 
                } 
            } 
 
            return null; 
        } 
 
        public List<AutomationElement> getSameXAutomationElement(ControlType controlType) 
        { 
            List<AutomationElement> elements = new List<AutomationElement>(); 
            List<AutomationElement> xelements = getSameXAutomationElement(); 
            foreach (AutomationElement element in xelements) 
            { 
                if (!element.Current.ControlType.Equals(controlType)) 
                { 
                    elements.Add(element); 
                } 
            } 
 
            return elements; 
        } 
 
        public List<AutomationElement> getSameYAutomationElement(ControlType controlType) 
        { 
            List<AutomationElement> elements = new List<AutomationElement>(); 
            List<AutomationElement> yelements = getSameYAutomationElement(); 
            foreach (AutomationElement element in yelements) 
            { 
                if (!element.Current.ControlType.Equals(controlType)) 
                { 
                    elements.Add(element); 
                } 
            } 
 
            return elements; 
        } 
 
        public List<AutomationElement> getSameXAutomationElement() 
        { 
            List<AutomationElement> elements = new List<AutomationElement>(); 
            AutomationElement window = getWindow(element); 
 
            TreeWalker tWalker = TreeWalker.ControlViewWalker; 
            AutomationElement childElement = tWalker.GetFirstChild(window); 
 
            while (childElement != null) 
            { 
                if (childElement != element && Math.Abs(childElement.Current.BoundingRectangle.Top - element.Current.BoundingRectangle.Top) < 5) 
                { 
                    elements.Add(childElement); 
                } 
                childElement = tWalker.GetNextSibling(childElement); 
            } 
            return elements; 
        } 
 
        public List<AutomationElement> getSameYAutomationElement() 
        { 
            List<AutomationElement> elements = new List<AutomationElement>(); 
            AutomationElement window = getWindow(element); 
 
            TreeWalker tWalker = TreeWalker.ControlViewWalker; 
            AutomationElement childElement = tWalker.GetFirstChild(window); 
 
            while (childElement != null) 
            { 
                if (childElement != element && Math.Abs(childElement.Current.BoundingRectangle.Left - element.Current.BoundingRectangle.Left) < 5) 
                { 
                    elements.Add(childElement); 
                } 
                childElement = tWalker.GetNextSibling(childElement); 
            } 
            return elements; 
        } 
    }
enum LocationDirection
{
    LEFT = 0,
    RIGHT = 1,
    UP = 2,
    DOWN = 3
}
1 month ago, this page was being read.

,

Subscribe to Comments