Beispiel #1
0
        /// <summary>
        /// Get DesktopElement from x, y position
        /// </summary>
        /// <param name="xPos"></param>
        /// <param name="yPos"></param>
        /// <returns></returns>
        public static DesktopElement ElementFromPoint(int xPos, int yPos)
        {
            try
            {
                var uia = UIAutomation.ElementFromPoint(new tagPOINT()
                {
                    x = xPos, y = yPos
                });

                if (!DesktopElement.IsFromCurrentProcess(uia))
                {
#pragma warning disable CA2000 // Call IDisposable.Dispose()
                    var e = new DesktopElement(uia, true, false);
#pragma warning restore CA2000
                    e.PopulateMinimumPropertiesForSelection();

                    return(e);
                }
                else
                {
                    Marshal.ReleaseComObject(uia);
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception e)
            {
                e.ReportException();
            }
#pragma warning restore CA1031 // Do not catch general exception types

            return(null);
        }
        /// <summary>
        /// Clone A11yElement for selection
        /// </summary>
        /// <param name="e"></param>
        /// <returns>if element is not live, don't allow clow</returns>
        public static A11yElement CloneForSelection(this A11yElement e)
        {
            if (e == null)
            {
                return(null);
            }
            if (e.PlatformObject == null)
            {
                return(null);
            }

            try
            {
                var cache = DesktopElementHelper.BuildCacheRequest(MiniumProperties, null);

                var uia = ((IUIAutomationElement)e.PlatformObject).BuildUpdatedCache(cache);
                Marshal.ReleaseComObject(cache);

                var ne = new DesktopElement(uia, true, false);
                ne.PopulateMinimumPropertiesForSelection();

                return(ne);
            }
            catch (COMException ex)
            {
                ex.ReportException();
            }

            return(null);
        }
        /// <summary>
        /// Get DesktopElement from x, y position
        /// </summary>
        /// <param name="xPos"></param>
        /// <param name="yPos"></param>
        /// <returns></returns>
        public static DesktopElement ElementFromPoint(int xPos, int yPos)
        {
            try
            {
                var uia = UIAutomation.ElementFromPoint(new tagPOINT()
                {
                    x = xPos, y = yPos
                });

                if (!DesktopElement.IsFromCurrentProcess(uia))
                {
                    var e = new DesktopElement(uia, true, false);
                    e.PopulateMinimumPropertiesForSelection();

                    return(e);
                }
                else
                {
                    Marshal.ReleaseComObject(uia);
                }
            }
            catch (Exception e)
            {
                e.ReportException();
            }

            return(null);
        }
Beispiel #4
0
        /// <summary>
        /// Get DesktopElement from UIAElement interface.
        /// </summary>
        /// <param name="uia"></param>
        /// <returns></returns>
        private static DesktopElement ElementFromUIAElement(IUIAutomationElement uia)
        {
            if (uia != null)
            {
                if (!DesktopElement.IsFromCurrentProcess(uia))
                {
                    var el = new DesktopElement(uia, true, false);

                    el.PopulateMinimumPropertiesForSelection();

                    return(el);
                }
                else
                {
                    Marshal.ReleaseComObject(uia);
                }
            }

            return(null);
        }
        /// <summary>
        /// Get the focused Element
        /// </summary>
        /// <returns></returns>
        public static DesktopElement GetFocusedElement()
        {
            try
            {
                var uia = UIAutomation.GetFocusedElement();

                if (!DesktopElement.IsFromCurrentProcess(uia))
                {
                    return(new DesktopElement(uia, true));
                }
                else
                {
                    Marshal.ReleaseComObject(uia);
                }
            }
            catch
            {
            }

            return(null);
        }
Beispiel #6
0
        /// <summary>
        /// Get DesktopElement based on Process Id.
        /// </summary>
        /// <param name="pid"></param>
        /// <returns>return null if fail to get an element by process Id</returns>
        public static DesktopElement ElementFromProcessId(int pid)
        {
            IUIAutomationElement   root      = null;
            DesktopElement         element   = null;
            IUIAutomationCondition condition = null;

            try
            {
                // check whether process exist first.
                // if not, it will throw an ArgumentException
                using (var proc = Process.GetProcessById(pid))
                {
                    root      = UIAutomation.GetRootElement();
                    condition = UIAutomation.CreatePropertyCondition(PropertyType.UIA_ProcessIdPropertyId, pid);
                    var uia = root.FindFirst(TreeScope.TreeScope_Descendants, condition);
                    element = ElementFromUIAElement(uia);
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                // report and let it return null
                ex.ReportException();
            }
#pragma warning restore CA1031 // Do not catch general exception types
            finally
            {
                if (root != null)
                {
                    Marshal.ReleaseComObject(root);
                }
                if (condition != null)
                {
                    Marshal.ReleaseComObject(condition);
                }
            }
            return(element);
        }
Beispiel #7
0
        /// <summary>
        /// Get the focused Element
        /// </summary>
        /// <returns></returns>
        public static DesktopElement GetFocusedElement()
        {
            try
            {
                var uia = UIAutomation.GetFocusedElement();

                if (!DesktopElement.IsFromCurrentProcess(uia))
                {
                    return(new DesktopElement(uia, true));
                }
                else
                {
                    Marshal.ReleaseComObject(uia);
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception e)
            {
                e.ReportException();
            }
#pragma warning restore CA1031 // Do not catch general exception types

            return(null);
        }
        /// <summary>
        /// Get DesktopElement based on Process Id.
        /// </summary>
        /// <param name="pid"></param>
        /// <returns>return null if fail to get an element by process Id</returns>
        public static DesktopElement ElementFromProcessId(int pid)
        {
            IUIAutomationElement   root      = null;
            DesktopElement         element   = null;
            IUIAutomationCondition condition = null;

            try
            {
                // check whether process exist first.
                // if not, it will throw an ArgumentException
                using (var proc = Process.GetProcessById(pid))
                {
                    root      = UIAutomation.GetRootElement();
                    condition = UIAutomation.CreatePropertyCondition(PropertyType.UIA_ProcessIdPropertyId, pid);
                    var uia = root.FindFirst(TreeScope.TreeScope_Descendants, condition);
                    element = ElementFromUIAElement(uia);
                }
            }
            catch (Exception ex)
            {
                //silent and let it return null
                Debug.WriteLine(ex);
            }
            finally
            {
                if (root != null)
                {
                    Marshal.ReleaseComObject(root);
                }
                if (condition != null)
                {
                    Marshal.ReleaseComObject(condition);
                }
            }
            return(element);
        }
        /// <summary>
        /// Refresh the property and patterns with Live data.
        /// it also set the Glimpse
        /// the update is done via caching to improve performance.
        /// </summary>
        /// <param name="useProperties">default is false to refresh it from UIElement directly</param>
        private static void PopulatePropertiesAndPatternsFromCache(this A11yElement element)
        {
            try
            {
                // Get the list of properties to retrieve
                A11yAutomation.UIAutomationObject.PollForPotentialSupportedProperties((IUIAutomationElement)element.PlatformObject, out int[] ppids, out string[] ppns);

                var ppl = new List <Tuple <int, string> >();

                for (int i = 0; i < ppids.Length; i++)
                {
                    var id   = (int)ppids.GetValue(i);
                    var name = (string)ppns.GetValue(i);

                    if (DesktopElement.IsExcludedProperty(id, name) == false)
                    {
                        ppl.Add(new Tuple <int, string>(id, name));
                    }
                }

                A11yAutomation.UIAutomationObject.PollForPotentialSupportedPatterns((IUIAutomationElement)element.PlatformObject, out int[] ptids, out string[] ptns);
                var ptl = new List <Tuple <int, string> >();

                for (int i = 0; i < ptids.Length; i++)
                {
                    ptl.Add(new Tuple <int, string>((int)ptids.GetValue(i), (string)ptns.GetValue(i)));
                }

                var pplist = (from pp in ppl
                              select pp.Item1).ToList();
                var ptlist = (from pt in ptl
                              select pt.Item1).ToList();

                // build a cache based on the lists
                var cache = DesktopElementHelper.BuildCacheRequest(pplist, ptlist);

                // buildupdate cached element
                var uia = ((IUIAutomationElement)element.PlatformObject).BuildUpdatedCache(cache);

                // retrieve properties from cache
                var ps = (from pp in ppl//.AsParallel()
                          let val = GetPropertyValueFromCache(uia, pp.Item1)
                                    where val != null
                                    select new A11yProperty(pp.Item1, val, pp.Item2));

                element.Properties = ps.Where(p => !string.IsNullOrEmpty(p.TextValue)).ToDictionary(d => d.Id);

                element.UpdateGlimpse();

                element.InitClickablePointProperty(uia);

                // retrieve patterns from cache
                var ptlst = from pt in ptl
                            let pi = A11yPatternFactory.GetPatternInstance(element, uia, pt.Item1, pt.Item2)
                                     where pi != null
                                     select pi;

                element.Patterns = ptlst.ToList();

                // release previous UIAElement
                Marshal.ReleaseComObject(uia);
                // release cache interface.
                Marshal.ReleaseComObject(cache);

                ppl.Clear();
                ptl.Clear();
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception e)
            {
                e.ReportException();
            }
#pragma warning restore CA1031 // Do not catch general exception types
        }