Example #1
0
        public static IUiElement GetElementFromPoint(System.Drawing.Point mousePoint)
        {
            IUiElement element = null;

            // commented 20120618 to switch to UiaCOMWrapper
            element =
                UiElement.FromPoint(
                    new System.Windows.Point(mousePoint.X, mousePoint.Y));
            //element =
            //    //(UiaNET::System.Windows.Automation.AutomationElement)
            //    UiaCOM3.UiaCOMHelper.GetAutomationElementFromPoint();

            return(element);
        }
Example #2
0
        public virtual List <IntPtr> CollectRecursively(
            IUiElement containerElement,
            string name,
            int level)
        {
            var resultHandle       = IntPtr.Zero;
            var controlHandle      = IntPtr.Zero;
            var controlHandles     = new List <IntPtr>();
            var tempControlHandles = new List <IntPtr>();
            // 20140312
            // var containerHandle = new IntPtr(containerElement.Current.NativeWindowHandle);
            var containerHandle = new IntPtr(containerElement.GetCurrent().NativeWindowHandle);

            if (containerHandle == IntPtr.Zero)
            {
                return(controlHandles);
            }

            // search at this level
            do
            {
                // using null instead of name
                controlHandle =
                    NativeMethods.FindWindowEx(containerHandle, controlHandle, null, null);

                if (controlHandle == IntPtr.Zero)
                {
                    continue;
                }
                controlHandles.Add(controlHandle);

                tempControlHandles =
                    CollectRecursively(
                        UiElement.FromHandle(controlHandle),
                        name,
                        level + 1);

                if (null == tempControlHandles || 0 == tempControlHandles.Count)
                {
                    continue;
                }
                controlHandles.AddRange(tempControlHandles);
            } while (controlHandle != IntPtr.Zero);

            return(controlHandles);
        }
Example #3
0
 public virtual List <IUiElement> GetElementsFromHandles(List <IntPtr> pointers)
 {
     return((from pointer in pointers where IntPtr.Zero != pointer select UiElement.FromHandle(pointer)).ToList());
 }
Example #4
0
        List <IUiElement> GetWindowCollectionByName(
            IUiElement rootElement,
            WindowSearcherData data)
        {
            var windowCollectionByProperties =
                new List <IUiElement>();
            var resultCollection =
                new List <IUiElement>();

            if (null == data.Name)
            {
                // 20141001
                // data.Name = new string[]{ string.Empty };
                data.Name = new [] { string.Empty };
            }

            classic.OrCondition conditionsSet = null;
            if (data.Recurse)
            {
                conditionsSet =
                    new classic.OrCondition(
                        new classic.PropertyCondition(
                            classic.AutomationElement.ControlTypeProperty,
                            classic.ControlType.Window),
                        classic.Condition.FalseCondition);
            }
            else
            {
                conditionsSet =
                    new classic.OrCondition(
                        new classic.PropertyCondition(
                            classic.AutomationElement.ControlTypeProperty,
                            classic.ControlType.Window),
                        new classic.PropertyCondition(
                            classic.AutomationElement.ControlTypeProperty,
                            classic.ControlType.Pane),
                        new classic.PropertyCondition(
                            classic.AutomationElement.ControlTypeProperty,
                            classic.ControlType.Menu));
            }

            foreach (string windowTitle in data.Name)
            {
                IUiEltCollection windowCollection =
                    rootElement.FindAll(data.Recurse ? classic.TreeScope.Descendants : classic.TreeScope.Children, conditionsSet);

                var controlSearcherData =
                    new ControlSearcherData {
                    Name         = windowTitle,
                    AutomationId = data.AutomationId,
                    Class        = data.Class,
                    Value        = string.Empty,
                    // 20141001
                    // ControlType = (new string[]{ "Window", "Pane", "Menu" })
                    ControlType = (new [] { "Window", "Pane", "Menu" })
                };

                windowCollectionByProperties =
                    ReturnOnlyRightElements(
                        windowCollection,
                        controlSearcherData,
                        false,
                        true);

                try {
                    if (null != windowCollectionByProperties && 0 < windowCollectionByProperties.Count)
                    {
                        foreach (IUiElement aeWndByTitle in windowCollectionByProperties
                                 .Where(aeWndByTitle => aeWndByTitle != null && (int)aeWndByTitle.GetCurrent().ProcessId > 0))
                        {
                            resultCollection.Add(aeWndByTitle);
                        }

                        windowCollectionByProperties.Clear();
                    }
                    else
                    {
                        IUiElement tempElement = null;

                        // one more attempt using the FindWindow function
                        IntPtr wndHandle = NativeMethods.FindWindowByCaption(IntPtr.Zero, windowTitle);
                        // 20141001
                        // if (wndHandle != null && wndHandle != IntPtr.Zero) {
                        if (wndHandle != IntPtr.Zero)
                        {
                            tempElement =
                                UiElement.FromHandle(wndHandle);
                        }

                        if (null == tempElement || (int)tempElement.GetCurrent().ProcessId <= 0)
                        {
                            continue;
                        }

                        resultCollection.Add(tempElement);
                    }
                }
                catch {}

                // 20140122
                // AutomationFactory.DisposeChildKernel();

                // 20140131
//                if (null != windowCollectionByProperties && 0 < windowCollectionByProperties.Count) {
//                    foreach (IUiElement element in windowCollectionByProperties) {
//                        element.Dispose();
//                    }
//                }
//                if (null != windowCollection && 0 < windowCollection.Count) {
//                    foreach (IUiElement element in windowCollection) {
//                        element.Dispose();
//                    }
//                }
            }

            return(resultCollection);
        }