18

I have to hide popup windows in third party library.

I have implemented windows hook stuff with SetWindowsHookEx and know all the newely created hWnd(s). I listen to HSHELL_WINDOWCREATED callback and do the following:

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);      

What I do wrong here to hide newely created windows in task bar?

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
Andrew Florko
  • 7,672
  • 10
  • 60
  • 107
  • 1
    WS_EX_* need to be get/set with GWL_EXSTYLE - not GWL_STYLE. (WS_EX_TOOLWINDOW has value 0x00000080, which if you use it as a GWL_STYLE instead of an _EXSTYLE, will end up modifying the part of a windows style that's typically not used for app windows, so will seem to be a no-op. The low part of a windows style is typically used for control-specific style bits - eg. for a button, this has the same value as BS_BITMAP.) – BrendanMcK Aug 28 '11 at 05:36

3 Answers3

23

Before you use SetWindowLong, call ShowWindow(hWnd, SW_HIDE), then call SetWindowLong, then call ShowWindow again like ShowWindow(hWnd, SW_SHOW). So your code will look like this:

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

Here is a relevant quote from Microsoft's Website:

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.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • sorry, it doesn't work for me. Windows that I hide previously successfully now come visible as well as shown in taskbar. – Andrew Florko Aug 28 '11 at 04:49
  • @Andrew that's very weird. It might not work, but for curiosity, try `SetWindowLong` after you call `ShowWindow(hWnd, SW_SHOW);` too. So the code would go `ShowWindow(hWnd, SW_HIDE); SetWindowLong(hWnd, GWL_STYLE, style); ShowWindow(hWnd, SW_SHOW); SetWindowLong(hWnd, GWL_STYLE, style);` – Seth Carnegie Aug 28 '11 at 04:52
  • @Andrew also what flag are you using as the first parameter to `SetWindowsHookEx`? – Seth Carnegie Aug 28 '11 at 04:56
  • @Andrew and if that modification doesn't work, change the second call to `SetWindowLong` to `ShowWindow(hWnd, SW_HIDE)` – Seth Carnegie Aug 28 '11 at 04:59
  • @Andrew Excellent. Good luck with your project. – Seth Carnegie Aug 28 '11 at 05:35
  • WS_EX_TOOLWINDOW and WS_EX_APPWINDOW flags are exStyles, so, you have to use GWL_EXSTYLE instead of GWL_STYLE. – Tutankhamen Apr 09 '16 at 06:31
  • 1
    What is the point of the last hiding? If the window is invisible, of course it's not in the taskbar. I don't understand why this solve the problem. – Wu Zhenwei Nov 07 '19 at 06:23
2

You must use GWL_EXSTYLE to get/set the EX flags, GWL_STYLE will not work for EX flags.

0

you can use this command in Init function: ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW);

starball
  • 20,030
  • 7
  • 43
  • 238
mimon
  • 1