Win32:如何通过hWnd隐藏任务栏中的第三方窗口

And*_*rko 16 c++ windows winapi

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

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

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);      
Run Code Online (Sandbox Code Playgroud)

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

Set*_*gie 20

使用之前SetWindowLong,调用ShowWindow(hWnd, SW_HIDE),然后调用SetWindowLong,然后调用ShowWindow又喜欢ShowWindow(hWnd, SW_SHOW).所以你的代码看起来像这样:

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
Run Code Online (Sandbox Code Playgroud)

以下是微软网站的相关引用:

要防止窗口按钮放在任务栏上,请使用WS_EX_TOOLWINDOW扩展样式创建无主窗口.作为替代方案,您可以创建隐藏窗口并使此隐藏窗口成为可见窗口的所有者.

仅当窗口的样式支持可见任务栏按钮时,Shell才会从任务栏中删除窗口的按钮.如果要将窗口的样式动态更改为不支持可见任务栏按钮的样式,则必须先隐藏窗口(通过使用SW_HIDE调用ShowWindow),更改窗口样式,然后显示窗口.