Example #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>
        /// 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);
        }
Example #3
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);
        }
Example #5
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);
        }