Exemple #1
0
        /// <summary>
        /// Mouse timer event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ontimerMouseElapsedEvent(object sender, ElapsedEventArgs e)
        {
            lock (_elementSetterLock)
            {
                if (this.timerMouse != null && this.IsStarted)
                {
                    NativeMethods.GetCursorPos(out Point p);

                    if (LastMousePoint.Equals(p) && this.POIPoint.Equals(p) == false)
                    {
                        var element = GetElementBasedOnScope(A11yAutomation.ElementFromPoint(p.X, p.Y));

                        if (element != null && element.IsRootElement() == false && element.IsSameUIElement(this.SelectedElementRuntimeId, this.SelectedBoundingRectangle, this.SelectedControlTypeId, this.SelectedName) == false && !POIPoint.Equals(p))
                        {
                            this.SelectedElementRuntimeId  = element.RuntimeId;
                            this.SelectedBoundingRectangle = element.BoundingRectangle;
                            this.SelectedControlTypeId     = element.ControlTypeId;
                            this.SelectedName = element.Name;
                            this.SetElement?.Invoke(element);
                        }
                        else
                        {
                            element?.Dispose();
                            element = null;
                        }

                        POIPoint = p;
                    }

                    LastMousePoint = p;

                    this.timerMouse?.Start(); // make sure that it is disabled.
                }
            }
        }
        /// <summary>
        /// Constructor DesktopElementAncestry
        /// Get Ancestry Tree elements up to Desktop(at best)
        /// </summary>
        /// <param name="walker"></param>
        /// <param name="e"></param>
        public DesktopElementAncestry(TreeViewMode mode, A11yElement e, bool setMem = false)
        {
            this.TreeWalker     = A11yAutomation.GetTreeWalker(mode);
            this.TreeWalkerMode = mode;
            this.Items          = new List <A11yElement>();
            this.SetMembers     = setMem;
            SetParent(e, -1);

            if (Items.Count != 0)
            {
                this.First = Items.Last();
                this.Last  = Items.First();
                if (this.Last.IsRootElement() == false)
                {
                    this.Last.Children.Clear();
                    this.NextId = PopulateSiblingTreeNodes(this.Last, e);
                }
                else
                {
                    this.NextId = 1;
                }
            }

            Marshal.ReleaseComObject(this.TreeWalker);
        }
Exemple #3
0
        private IUIAutomationElement GetNearbyElement(GetElementDelegate getNextElement)
        {
            var currentElement = GetCurrentElement();

            if (currentElement == null)
            {
                return(null);
            }

            var treeWalker = A11yAutomation.GetTreeWalker(this.TreeViewMode);

            var nextElement = getNextElement?.Invoke(treeWalker, currentElement);

            if (nextElement == null)
            {
                Marshal.ReleaseComObject(treeWalker);
                return(null);
            }

            // make sure that we skip an element from current process while walking tree.
            // this code should be hit only at App level. but for sure.
            if (DesktopElement.IsFromCurrentProcess(nextElement))
            {
                var tmp = nextElement;

                nextElement = getNextElement?.Invoke(treeWalker, nextElement);

                // since element is not in use, release.
                Marshal.ReleaseComObject(tmp);
            }

            Marshal.ReleaseComObject(treeWalker);

            return(nextElement);
        }
        /// <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 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);
        }
Exemple #6
0
 public IEnumerable <A11yElement> LocateRootElements(int processId)
 {
     try
     {
         var desktopElements = A11yAutomation.ElementsFromProcessId(processId);
         return(GetA11YElementsFromDesktopElements(desktopElements));
     }
     catch (Exception ex)
     {
         throw new AxeWindowsAutomationException(string.Format(CultureInfo.InvariantCulture, DisplayStrings.ErrorFailToGetRootElementOfProcess, processId, ex), ex);
     }
 }
        public A11yElement LocateRootElement(int processId)
        {
            try
            {
                var element = A11yAutomation.ElementFromProcessId(processId);

                return(new ElementContext(element).Element);
            }
            catch (Exception ex)
            {
                throw new AxeWindowsAutomationException(string.Format(CultureInfo.InvariantCulture, DisplayStrings.ErrorFailToGetRootElementOfProcess, processId, ex), ex);
            }
        }
        public A11yElement LocateRootElement(int processId)
        {
            try
            {
                var element = A11yAutomation.ElementFromProcessId(processId);

                #pragma warning disable CA2000 // Call IDisposable.Dispose()
                return(new ElementContext(element).Element);

                #pragma warning restore CA2000
            }
            catch (Exception ex)
            {
                throw new AxeWindowsAutomationException(string.Format(CultureInfo.InvariantCulture, DisplayStrings.ErrorFailToGetRootElementOfProcess, processId, ex), ex);
            }
        }
        /// <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);
        }
        /// <summary>
        /// Get Element based on Scope
        /// if Scope is Element, returns the current element
        /// if Scope is App, find out App element and return the App element
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        protected A11yElement GetElementBasedOnScope(A11yElement e)
        {
            if (e != null && Scope == SelectionScope.App)
            {
                var el = A11yAutomation.GetAppElement(e);

                // if the original selection is Top most element of the app, it should not be released.
                if (e != el)
                {
                    e.Dispose();
                }

                return(el);
            }

            return(e);
        }
        /// <summary>
        /// Locate the target element in the UIA tree
        /// </summary>
        /// <param name="parameters">The caller-provided parameters</param>
        /// <returns>The ElementContext that matches the targeting parameters</returns>
        internal static ElementContext LocateElement(CommandParameters parameters)
        {
            if (parameters.TryGetLong(CommandConstStrings.TargetProcessId, out long parameterPid))
            {
                try
                {
                    var element = A11yAutomation.ElementFromProcessId((int)parameterPid);

                    return(new ElementContext(element));
                }
                catch (Exception ex)
                {
                    throw new A11yAutomationException(string.Format(CultureInfo.InvariantCulture, DisplayStrings.ErrorFailToGetRootElementOfProcess, parameterPid, ex), ex);
                }
            }

            throw new A11yAutomationException(DisplayStrings.ErrorNoProcessIdSet);
        }
Exemple #12
0
        /// <summary>
        /// Mouse timer event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ontimerMouseElapsedEvent(object sender, ElapsedEventArgs e)
        {
            lock (_elementSetterLock)
            {
                if (this.timerMouse != null && this.IsStarted)
                {
                    NativeMethods.GetCursorPos(out Point p);

                    if (LastMousePoint.Equals(p) && !this.POIPoint.Equals(p))
                    {
                        var element = GetElementBasedOnScope(A11yAutomation.NormalizedElementFromPoint(p.X, p.Y, this.TreeViewMode));
                        if (!SelectElementIfItIsEligible(element))
                        {
                            element?.Dispose();
                        }
                        POIPoint = p;
                    }

                    LastMousePoint = p;

                    this.timerMouse?.Start(); // make sure that it is enabled.
                }
            }
        }