I've been a .NET developer for several years now and this is still one of those things I don't know how to do properly. It's easy to hide a window from the taskbar via a property in both Windows Forms and WPF, but as far as I can tell, this doesn't guarantee (or necessarily even affect) it being hidden from the Alt+↹Tab dialog. I've seen invisible windows show up in Alt+↹Tab, and I'm just wondering what is the best way to guarantee a window will never appear (visible or not) in the Alt+↹Tab dialog.
Update: Please see my posted solution below. I'm not allowed to mark my own answers as the solution, but so far it's the only one that works.
Update 2: There's now a proper solution by Franci Penov that looks pretty good, but haven't tried it out myself. Involves some Win32, but avoids the lame creation of off-screen windows.
I tried setting the main form's visibility to false whenever it is automatically changed to true:
It works perfectly :)
In XAML set ShowInTaskbar="False":
Edit: That still shows it in Alt+Tab I guess, just not in the taskbar.
Why trying so much codes? Just set the
FormBorderStyle
propety toFixedToolWindow
. Hope it helps.see it:(from http://bytes.com/topic/c-sharp/answers/442047-hide-alt-tab-list#post1683880)
Don't show a form. Use invisibility.
More here: http://code.msdn.microsoft.com/TheNotifyIconExample
Update:
According to @donovan, modern days WPF supports this natively, through setting
ShowInTaskbar="False"
andVisibility="Hidden"
in the XAML. (I haven't tested this yet, but nevertheless decided to bump the comment visibility)Original answer:
There are two ways of hiding a window from the task switcher in Win32 API:
WS_EX_TOOLWINDOW
extended window style - that's the right approach.Unfortunately, WPF does not support as flexible control over the window style as Win32, thus a window with
WindowStyle=ToolWindow
ends up with the defaultWS_CAPTION
andWS_SYSMENU
styles, which causes it to have a caption and a close button. On the other hand, you can remove these two styles by settingWindowStyle=None
, however that will not set theWS_EX_TOOLWINDOW
extended style and the window will not be hidden from the task switcher.To have a WPF window with
WindowStyle=None
that is also hidden from the task switcher, one can either of two ways:WS_EX_TOOLWINDOW
extended style.I personally prefer the second approach. Then again, I do some advanced stuff like extending the glass in the client area and enabling WPF drawing in the caption anyway, so a little bit of interop is not a big problem.
Here's the sample code for the Win32 interop solution approach. First, the XAML part:
Nothing too fancy here, we just declare a window with
WindowStyle=None
andShowInTaskbar=False
. We also add a handler to the Loaded event where we will modify the extended window style. We can't do that work in the constructor, as there's no window handle at that point yet. The event handler itself is very simple:And the Win32 interop declarations. I've removed all unnecessary styles from the enums, just to keep the sample code here small. Also, unfortunately the
SetWindowLongPtr
entry point is not found in user32.dll on Windows XP, hence the trick with routing the call through theSetWindowLong
instead.