Beispiel #1
0
        private bool GetMenuItems(AutomationElement element, string parent, List <string> groups)
        {
            bool ret       = false;
            var  menuItems = element.FindAll(TreeScope.Children, Condition.TrueCondition).Cast <AutomationElement>();

            foreach (var item in menuItems)
            {
                if (_isCanceled)
                {
                    break;
                }

                try
                {
                    var         elementInfo = item.Current;
                    ControlType elementType = elementInfo.ControlType;
                    if (elementType.IsThroughElement())
                    {
                        continue;
                    }

                    string addName = elementInfo.Name;
                    if (addName == null || addName == "")
                    {
                        continue;
                    }
                    string itemName = addName.Clone() as string;
                    string fullpath;
                    string shortcutKey;
                    bool   canExpand;
                    if (FormatName(parent, elementType, item, ref addName, out fullpath, out shortcutKey, out canExpand))
                    {
                        //bool isOffscreen = false;
                        if (item.CanExpandElement(elementType))
                        {
                            // Expand
                            item.TryDoDefaultAction();
                            groups.Add(itemName);
                            string ancestor = parent;
                            if (parent == null)
                            {
                                ancestor = itemName;
                            }
                            else
                            {
                                ancestor = parent + Consts.Delimiter + itemName;
                            }
                            GetMenuItems(item, ancestor, groups);
                            groups.RemoveAt(groups.Count - 1);
                            // Collapse
                            item.TryDoDefaultAction();
                            //isOffscreen = true;
                        }
                        else if (HasChild(item))
                        {
                            string ancestor = parent;
                            if (parent == null)
                            {
                                ancestor = itemName;
                            }
                            else
                            {
                                ancestor = parent + Consts.Delimiter + itemName;
                            }
                            groups.Add(itemName);
                            GetMenuItems(item, ancestor, groups);
                            groups.RemoveAt(groups.Count - 1);
                            continue;
                        }
                        var result = new MenuItemUIA(itemName, fullpath, elementInfo.BoundingRectangle, elementInfo.IsEnabled, canExpand, item, element, groups);
                        _results.Add(result);
                    }

                    ret = true;
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.Print("{0}", e.Message);
                }
            }
            Update();
            return(ret);
        }
Beispiel #2
0
        private bool GetMenuItems(AutomationElement element, string parent, List <AutomationElement> groups)
        {
            bool ret       = false;
            var  menuItems = element.FindAll(TreeScope.Children, System.Windows.Automation.Condition.TrueCondition).Cast <AutomationElement>();

            foreach (var item in menuItems)
            {
                if (_isCanceled)
                {
                    break;
                }

                try
                {
                    var         elementInfo = item.Current;
                    ControlType elementType = elementInfo.ControlType;
                    if (elementType.IsThroughElement())
                    {
                        continue;
                    }

                    string addName = elementInfo.Name;
                    if (addName == null || addName == "")
                    {
                        continue;
                    }
                    string itemName = addName.Clone() as string;
                    string fullpath;
                    string shortcutKey;
                    bool   canExpand;
                    if (FormatName(parent, elementType, item, ref addName, out fullpath, out shortcutKey, out canExpand))
                    {
                        if (item.CanExpandElement(elementType))
                        {
                            string ancestor = parent;
                            if (parent == null)
                            {
                                ancestor = itemName;
                            }
                            else
                            {
                                ancestor = parent + Consts.Delimiter + itemName;
                            }

                            if (item.IsWPF())
                            {
                                _expandableItems.Add(itemName);
                                item.TryExpand();
                                GetMenuItems(item, ancestor, new List <AutomationElement>());
                                item.TryCollapse();
                                _expandableItems.Remove(itemName);
                                continue;
                            }
                            else
                            {
                                var observer = SearchByText.UIAssistantAPI.AutomationAPI.CreateObserver(Interfaces.Events.ObserverKinds.PopupObserver);
                                observer.Callback += x =>
                                {
                                    // HACK: デッドロックを避けるために、2箇所で Dispose しているけれど、微妙
                                    observer.Dispose();
                                    GetMenuItems(x, ancestor, new List <AutomationElement>());
                                    item.TryCollapse();
                                    _expandableItems.Remove(itemName);
                                };
                                _expandableItems.Add(itemName);
                                observer.Observe();
                                item.TryExpand();
                                observer.Wait();
                                observer.Dispose();
                                continue;
                            }
                        }
                        var result = new MenuItemUIA(itemName, fullpath, elementInfo.BoundingRectangle, elementInfo.IsEnabled, canExpand, item, ContextRoot, _expandableItems.ToArray().ToList());
                        _results.Add(result);
                    }

                    ret = true;
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.Print("{0}", e.Message);
                }
            }
            Update();
            return(ret);
        }