C# AutomationElement - 获取包括隐藏在内的所有托盘图标 (Windows 10)

标签 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;
            }
        }
    }

最佳答案

我能够通过以下代码实现这一点。我不得不强制它出现。

    /// <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;
    }

关于C# AutomationElement - 获取包括隐藏在内的所有托盘图标 (Windows 10),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55055110/

相关文章:

visual-studio - 是否有命令行选项,如 devenv.exe/Edit for Sql Server Management Studio

ubuntu - ubuntu 上的 Appium 设置

C# 字典映射

c# - 将 Linq 表达式转换为 sql server 查询

windows - 错误: could not find native static library `ssl` ,也许缺少-L标志?在窗口上

c++ - 如何通过 Windows API 关闭电脑?

windows - 大多数(如果不是全部)音频端点是 PCM 吗?

.net - 从 .Net Access 模块中的调用例程?

c# - 为什么使用 Log4Net 而不是只写入我的数据库?

c# - 一次阻塞收集过程 n 个项目 - 一旦完成 1 个就继续