C#AutomationElement-获取包括隐藏的所有任务栏图标(Windows 10)

时间:2019-03-08 00:42:00

标签: c# windows automation

我正在使用以下代码尝试获取所有托盘图标,包括Windows 10隐藏的图标。

    public static List<AutomationElement> EnumNotificationIcons()
    {
        var data = new List<AutomationElement>();

        foreach (var button in AutomationElement.RootElement.Find(
                        "User Promoted Notification Area").EnumChildButtons())
        {
            data.Add(button);
        }

        foreach (var button in AutomationElement.RootElement.Find(
                      "System Promoted Notification Area").EnumChildButtons())
        {
            data.Add(button);
        }

        var chevron = AutomationElement.RootElement.Find("Notification Chevron");
        if (chevron != null && chevron.InvokeButton())
        {
            foreach (var button in AutomationElement.RootElement.Find(
                               "Overflow Notification Area").EnumChildButtons())
            {
                data.Add(button);
            }
        }

        return data;
    }

但是返回的列表仅包含可见图标。隐藏的所有内容都会被跳过。隐藏的任务栏图标不会退还。

我在这里想念什么?

编辑:

我已经更新了代码,使其看起来像这样。仍然不拉隐藏的图标。 https://blogs.msdn.microsoft.com/oldnewthing/20141013-00/?p=43863

    public static IEnumerable<AutomationElement> EnumNotificationIcons()
    {
        var userArea = AutomationElement.RootElement.Find("User Promoted Notification Area");
        if (userArea != null)
        {
            foreach (var button in userArea.EnumChildButtons())
            {
                yield return button;
            }

            foreach (var button in userArea.GetTopLevelElement().Find("System Promoted Notification Area").EnumChildButtons())
            {
                yield return button;
            }
        }

        var chevron = AutomationElement.RootElement.Find("Notification Chevron");
        if (chevron != null && chevron.InvokeButton())
        {
            foreach (var button in AutomationElement.RootElement.Find("Overflow Notification Area").EnumChildButtons())
            {
                yield return button;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我可以使用以下代码来完成此工作。我不得不强迫它出现。

    /// <summary>
    /// Enums the notification icons in the Tray.
    /// </summary>
    /// <returns>List of Notification Icons from the Tray.</returns>
    public static IEnumerable<AutomationElement> EnumNotificationIcons()
    {
        var userArea = AutomationElement.RootElement.Find(UINotificationAreaConstants.UserPromotedNotificationArea);
        if (userArea != null)
        {
            foreach (var button in userArea.EnumChildButtons())
            {
                yield return button;
            }

            foreach (var button in userArea.GetTopLevelElement().Find(UINotificationAreaConstants.SystemPromotedNotificationArea).EnumChildButtons())
            {
                yield return button;
            }
        }

        var chevron = AutomationElement.RootElement.Find(UINotificationAreaConstants.NotificationChevron);
        if (chevron != null && chevron.InvokeButton())
        {
            var elm = AutomationElement.RootElement.Find(
                                UINotificationAreaConstants.OverflowNotificationArea);

            WaitForElm(elm);

            foreach (var button in elm.EnumChildButtons())
            {
                yield return button;
            }
        }
    }

    /// <summary>
    /// Waits for elm to be ready for processing.
    /// </summary>
    /// <param name="targetControl">The target control.</param>
    /// <returns>The WindowPattern.</returns>
    private static WindowPattern WaitForElm(AutomationElement targetControl)
    {
        WindowPattern windowPattern = null;

        try
        {
            windowPattern =
                targetControl.GetCurrentPattern(WindowPattern.Pattern)
                as WindowPattern;
        }
        catch (InvalidOperationException)
        {
            // object doesn't support the WindowPattern control pattern
            return null;
        }
        // Make sure the element is usable.
        if (!windowPattern.WaitForInputIdle(10000))
        {
            // Object not responding in a timely manner
            return null;
        }
        return windowPattern;
    }