VisualTreeとLogicalTreeのユーティリティー。 対象プロセス内部で実行するためには、RM.Friendly.WPFStandardControls.3.dllをインジェクションする必要があります。 RM.Friendly.WPFStandardControls.WPFStandardControls_3.Injectionメソッドを利用してください。
Esempio n. 1
0
        static int[] GetActiveIndices(TreeView tree)
        {
            DependencyObject focusedElement = null;

            if (tree.IsKeyboardFocusWithin)
            {
                focusedElement = Keyboard.FocusedElement as DependencyObject;
            }
            else if (tree.IsMouseCaptureWithin)
            {
                focusedElement = Mouse.Captured as DependencyObject;
            }
            if (focusedElement == null)
            {
                return(new int[0]);
            }

            var focusedVisual = (List <DependencyObject>)TreeUtilityInTarget.VisualTree(focusedElement, TreeRunDirection.Ancestors);

            for (var i = 0; i < focusedVisual.Count; i++)
            {
                var item = focusedVisual[i] as TreeViewItem;
                if (item == null)
                {
                    continue;
                }

                var targetItemIndex = i;
                i++;
                for (; i < focusedVisual.Count; i++)
                {
                    var findedTreeView = focusedVisual[i] as TreeView;
                    if (findedTreeView == null)
                    {
                        continue;
                    }

                    //TreeView in TreeView
                    if (findedTreeView != tree)
                    {
                        break;
                    }

                    //Get Keys.
                    var indices = new List <int>();
                    for (int j = targetItemIndex; j < i; j++)
                    {
                        var focusedItem = focusedVisual[j] as TreeViewItem;
                        if (focusedItem == null)
                        {
                            continue;
                        }
                        indices.Add(GetIndex(focusedItem, focusedVisual, j, i));
                    }
                    indices.Reverse();
                    return(indices.ToArray());
                }
            }
            return(new int[0]);
        }
        static void GetIndices(IEnumerable <MenuItem> items, string[] headerTexts, int[] indices, int index)
        {
            int i = 0;

            foreach (var e in items)
            {
                if (e.Header.ToString() == headerTexts[index])
                {
                    indices[index] = i;
                    if (index == indices.Length - 1)
                    {
                        return;
                    }

                    //次のメニューを開く
                    IInvokeProvider invoker = new MenuItemAutomationPeer(e);
                    invoker.Invoke();

                    foreach (var popup in SearcherInTarget.ByType <Popup>(TreeUtilityInTarget.VisualTree(e)))
                    {
                        GetIndices(SearcherInTarget.ByType <MenuItem>(TreeUtilityInTarget.VisualTree(popup.Child)), headerTexts, indices, index + 1);
                        return;
                    }
                    break;
                }
                i++;
            }
            throw new NotSupportedException(ResourcesLocal3.Instance.ErrorNotFoundItem);
        }
Esempio n. 3
0
        internal static ContextMenu OpenMenu(UIElement target, bool openByKey, out Clean cleaner)
        {
            cleaner = null;

            FrameworkElement owner = null;
            var tree = TreeUtilityInTarget.VisualTree(target, TreeRunDirection.Ancestors);

            foreach (var e in tree)
            {
                var f = e as FrameworkElement;
                if (f != null && f.ContextMenu != null)
                {
                    owner = f;
                    break;
                }
            }
            if (owner == null)
            {
                throw new NotSupportedException();
            }
            var menu = owner.ContextMenu;

            if (openByKey)
            {
                target.Focus();
                SendInputEx.SendKey(System.Windows.Forms.Keys.Apps);
            }
            else
            {
                int count = menu.CommandBindings.Count;

                foreach (var e in TreeUtilityInTarget.VisualTree(target, TreeRunDirection.Ancestors))
                {
                    var u = e as UIElement;
                    if (u != null && u.CommandBindings != null)
                    {
                        foreach (CommandBinding command in u.CommandBindings)
                        {
                            menu.CommandBindings.Add(command);
                        }
                    }
                }
                target.Focus();
                menu.IsOpen = true;
                InvokeUtility.DoEvents();

                //数を元に戻す
                cleaner = () =>
                {
                    while (count < menu.CommandBindings.Count)
                    {
                        menu.CommandBindings.RemoveAt(menu.CommandBindings.Count - 1);
                    }
                    menu.IsOpen = false;
                };
            }

            return(menu);
        }
        static ContextMenu GetContextMenu(UIElement target)
        {
            var tree = TreeUtilityInTarget.VisualTree(target, TreeRunDirection.Ancestors);

            foreach (var e in tree)
            {
                var f = e as FrameworkElement;
                if (f != null && f.ContextMenu != null)
                {
                    return(f.ContextMenu);
                }
            }
            throw new NotSupportedException();
        }
 static bool HasFocus(Visual visual)
 {
     foreach (var e in TreeUtilityInTarget.VisualTree(visual))
     {
         var uielement = e as UIElement;
         if (uielement == null)
         {
             continue;
         }
         if (uielement.IsFocused)
         {
             return(true);
         }
     }
     return(false);
 }
        static int GetItemCount(MenuItem item)
        {
            IInvokeProvider invoker = new MenuItemAutomationPeer(item);

            invoker.Invoke();
            foreach (var p in SearcherInTarget.ByType <Popup>(TreeUtilityInTarget.VisualTree(item)))
            {
                int count = 0;
                foreach (var e in SearcherInTarget.ByType <MenuItem>(TreeUtilityInTarget.VisualTree(p.Child)))
                {
                    count++;
                }
                return(count);
            }
            return(0);
        }
        static int GetActiveIndex(ListBox list)
        {
            DependencyObject focusedElement = null;

            if (list.IsKeyboardFocusWithin)
            {
                focusedElement = Keyboard.FocusedElement as DependencyObject;
            }
            else if (list.IsMouseCaptureWithin)
            {
                focusedElement = Mouse.Captured as DependencyObject;
            }
            if (focusedElement == null)
            {
                return(-1);
            }

            var focusedVisual = (List <DependencyObject>)TreeUtilityInTarget.VisualTree(focusedElement, TreeRunDirection.Ancestors);

            for (var i = 0; i < focusedVisual.Count; i++)
            {
                var item = focusedVisual[i] as ListBoxItem;
                if (item == null)
                {
                    continue;
                }

                i++;
                for (; i < focusedVisual.Count; i++)
                {
                    var findedListBox = focusedVisual[i] as ListBox;
                    if (findedListBox == null)
                    {
                        continue;
                    }

                    //ListBox in ListBox
                    if (findedListBox != list)
                    {
                        break;
                    }

                    return(list.ItemContainerGenerator.IndexFromContainer(item));
                }
            }
            return(-1);
        }
        static int[] GetIndices(UIElement target, string[] headerTexts)
        {
            Clean cleaner = null;

            try
            {
                var   menu    = OpenMenu(target, out cleaner);
                int[] indices = new int[headerTexts.Length];
                GetIndices(SearcherInTarget.ByType <MenuItem>(TreeUtilityInTarget.VisualTree(menu)), headerTexts, indices, 0);
                return(indices);
            }
            finally
            {
                if (cleaner != null)
                {
                    cleaner();
                }
            }
        }
        internal static int GetItemCount(UIElement target, bool openByKey)
        {
            Clean cleaner = null;

            try
            {
                var menu  = OpenMenu(target, openByKey, out cleaner);
                int count = 0;
                foreach (var e in SearcherInTarget.ByType <MenuItem>(TreeUtilityInTarget.VisualTree(menu)))
                {
                    count++;
                }
                return(count);
            }
            finally
            {
                if (cleaner != null)
                {
                    cleaner();
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Enumerate DependencyObject continuing to LogicalTree.
        /// </summary>
        /// <param name="start">Start DependencyObject.</param>
        /// <param name="direction">Run direction.</param>
        /// <returns>Enumerated DependencyObject.</returns>
#else
        /// <summary>
        /// LogicalTreeに連なるDependencyObjectを列挙。
        /// </summary>
        /// <param name="start">列挙を開始するDependencyObject。</param>
        /// <param name="direction">走査方向。</param>
        /// <returns>列挙されたDependencyObject。</returns>
#endif
        public static IEnumerable <DependencyObject> LogicalTree(this DependencyObject start, TreeRunDirection direction = TreeRunDirection.Descendants)
        {
            return(TreeUtilityInTarget.LogicalTree(start, direction));
        }