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

C#AutomationElement-获取所有托盘图标,包括隐藏(Windows 10),c#,windows,automation,C#,Windows,Automation,我正在使用以下代码尝试获取所有托盘图标,包括Windows 10隐藏的图标 public static List<AutomationElement> EnumNotificationIcons() { var data = new List<AutomationElement>(); foreach (var button in AutomationElement.RootElement.Find(

我正在使用以下代码尝试获取所有托盘图标,包括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;
    }
公共静态列表

公共静态IEnumerable EnumNotificationIcons()
{
var userArea=AutomationElement.RootElement.Find(“用户升级通知区域”);
if(userArea!=null)
{
foreach(userArea.EnumChildButtons()中的var按钮)
{
屈服返回按钮;
}
foreach(userArea.GetToLevel元素()中的var按钮。查找(“系统提升的通知区域”)。EnumChildButtons()
{
屈服返回按钮;
}
}
var chevron=AutomationElement.RootElement.Find(“通知chevron”);
if(chevron!=null&&chevron.InvokeButton())
{
foreach(AutomationElement.RootElement.Find(“溢出通知区域”).EnumChildButtons()中的var按钮)
{
屈服返回按钮;
}
}
}

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

    /// <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;
    }
//
///枚举托盘中的通知图标。
/// 
///托盘中的通知图标列表。
公共静态IEnumerable EnumNotificationIcons()
{
var userArea=AutomationElement.RootElement.Find(UINotificationAreaConstants.UserPromotedNotificationArea);
if(userArea!=null)
{
foreach(userArea.EnumChildButtons()中的var按钮)
{
屈服返回按钮;
}
foreach(userArea.GetTopLevel()中的var按钮。查找(UINotificationAreaConstants.SystemPromotedNotificationArea.EnumChildButtons())
{
屈服返回按钮;
}
}
var chevron=AutomationElement.RootElement.Find(UINotificationAreaConstants.NotificationChevron);
if(chevron!=null&&chevron.InvokeButton())
{
var elm=AutomationElement.RootElement.Find(
UINotificationAreaConstants.OverflowNotificationArea);
韦特弗勒姆(榆树);
foreach(elm.EnumChildButtons()中的var按钮)
{
屈服返回按钮;
}
}
}
/// 
///等待elm准备好进行处理。
/// 
///目标控制。
///窗户图案。
专用静态WindowPattern WaitForElm(AutomationElement targetControl)
{
WindowPattern WindowPattern=null;
尝试
{
窗口模式=
targetControl.GetCurrentPattern(WindowPattern.Pattern)
作为窗口模式;
}
捕获(无效操作异常)
{
//对象不支持WindowPattern控件模式
返回null;
}
//确保元素可用。
如果(!windowPattern.WaitForInputId(10000))
{
//对象未及时响应
返回null;
}
返回窗口模式;
}

根据定义,通知区域中不存在隐藏项。确定。那么我如何访问它们呢?如果它们是隐藏的,那么它们就不存在了。你不能访问一些不存在的东西。嗯,那么就没有办法访问它们了?我需要自动点击一个win forms应用程序,自动加载最小化。这是用于自动测试的,首先让它可见。UI自动化用于自动化UI。因此,让它成为UI的一部分。
    /// <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;
    }