关于C#:Win32:如何通过hWnd在任务栏中隐藏第3方窗口

Win32: How to hide 3rd party windows in taskbar by hWnd

我必须在第三方库中隐藏弹出窗口。

我已经用SetWindowsHookEx实现了Windows钩子,并且知道所有新创建的hWnd。我听HSHELL_WINDOWCREATED回调并执行以下操作:

1
2
3
4
5
6
7
long style= GetWindowLong(hWnd, GWL_STYLE);
style &= ~(WS_VISIBLE);    // this works - window become invisible

style |= WS_EX_TOOLWINDOW;   // flags don't work - windows remains in taskbar
style &= ~(WS_EX_APPWINDOW);

SetWindowLong(hWnd, GWL_STYLE, style);

要在任务栏中隐藏新创建的窗口,我在这里做错了什么?


在使用SetWindowLong之前,先呼叫ShowWindow(hWnd, SW_HIDE),然后呼叫SetWindowLong,然后再次呼叫ShowWindow,就像ShowWindow(hWnd, SW_SHOW)。因此您的代码将如下所示:

1
2
3
4
5
6
7
8
9
10
long style= GetWindowLong(hWnd, GWL_STYLE);
style &= ~(WS_VISIBLE);    // this works - window become invisible

style |= WS_EX_TOOLWINDOW;   // flags don't work - windows remains in taskbar
style &= ~(WS_EX_APPWINDOW);

ShowWindow(hWnd, SW_HIDE); // hide the window
SetWindowLong(hWnd, GWL_STYLE, style); // set the style
ShowWindow(hWnd, SW_SHOW); // show the window for the new style to come into effect
ShowWindow(hWnd, SW_HIDE); // hide the window so we can't see it

以下是微软网站的相关报价:

To prevent the window button from being placed on the taskbar, create
the unowned window with the WS_EX_TOOLWINDOW extended style. As an
alternative, you can create a hidden window and make this hidden
window the owner of your visible window.

The Shell will remove a window's button from the taskbar only if the
window's style supports visible taskbar buttons. If you want to
dynamically change a window's style to one that doesn't support
visible taskbar buttons, you must hide the window first (by calling
ShowWindow with SW_HIDE), change the window style, and then show the
window.


您必须使用GWL_EXSTYLE来获取/设置EX标志,GWL_STYLE不适用于EX标志。