C# How to determine if hwnd is in tray icons

后端 未结 3 1801
难免孤独
难免孤独 2021-01-21 18:37

I am trying to get the hwnd of the current tray icons. what I did is getting the hWnd of system trat window by using this code:

[DllImport(\"user32.dll\", SetLas         


        
3条回答
  •  别那么骄傲
    2021-01-21 19:07

    I checked that if window is opened on desktop then it has styles:

    WS_VISIBLE=true
    WS_MINIMIZE=false
    

    if window is in taskbar:

    WS_VISIBLE=true
    WS_MINIMIZE=true
    

    if window is in system tray:

    WS_VISIBLE=false
    WS_MINIMIZE=true
    

    So you can play with styles to determine if window is in tray:

    public IsWindowFromTray(hWnd)
    {    
        bool isMinimized = Win32Natives.IsIconic(hWnd);
        bool isVisible = Win32Natives.IsWindowVisible(hWnd);
    
        return isMinimized && !isVisible;
    }
    

    For the majority of apps it works.

    PS: I used pinvoke

    [DllImport("user32.dll")]
    public static extern bool IsIconic(IntPtr hWnd);
    
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsWindowVisible(IntPtr hWnd);
    

提交回复
热议问题