C# How to determine if hwnd is in tray icons

懵懂的女人 提交于 2019-12-02 06:26:05

The 'children' of the ToolbarWindow32 are not windows. They are toolbar buttons. You'd use the TB_BUTTONCOUNT message to retrieve the number of buttons, TB_GETBUTTONINFO message to retrieve info about such a button. Quite hard to do btw since the window belongs to another process, just using SendMessage() doesn't work because the pointer isn't valid. And ultimately futile, such a button doesn't contain any information about what kind of process is associated with the icon. That's info that's buried inside the shell, you can't get to it.

There are no child handles. You can verify this via Spy++.

It is not hosing sub-controls, but rendering and handling things like tooltips directly.

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!