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.
Here's what does the trick, regardless of the style of the window your are trying to hide from Alt+↹Tab.
Place the following into the constructor of your form:
Essentially, you make your form a child of an invisible window which has the correct style and ShowInTaskbar setting to keep out of the Alt-Tab list. You must also set your own form's ShowInTaskbar property to false. Best of all, it simply doesn't matter what style your main form has, and all tweaking to accomplish the hiding is just a few lines in the constructor code.
Form1 Properties:
FormBorderStyle: Sizable
WindowState: Minimized
ShowInTaskbar: False
if you want the form to be borderless, then you need to add the following statements to the form’s constructor:
AND you must add the following method to your derived Form class:
more details
I've found a solution, but it's not pretty. So far this is the only thing I've tried that actually works:
Found it here.
A more general, reusable solution would be nice. I suppose you could create a single window 'w' and reuse it for all windows in your app that need to be hidden from the Alt+↹Tab.
Update: Ok so what I did was move the above code, minus the
this.Owner = w
bit (and movingw.Hide()
immediately afterw.Show()
, which works fine) into my application's constructor, creating a public staticWindow
calledOwnerWindow
. Whenever I want a window to exhibit this behavior, I simply setthis.Owner = App.OwnerWindow
. Works great, and only involves creating one extra (and invisible) window. You can even setthis.Owner = null
if you want the window to reappear in the Alt+↹Tab dialog.Thanks to Ivan Onuchin over on MSDN forums for the solution.
Update 2: You should also set
ShowInTaskBar=false
onw
to prevent it from flashing briefly in the taskbar when shown.Personally as far as I know this is not possible without hooking into windows in some fashion, I'm not even sure how that would be done or if it is possible.
Depending on your needs, developing your application context as a NotifyIcon (system tray) application will allow it to be running without showing in ALT + TAB. HOWEVER, if you open a form, that form will still follow the standard functionality.
I can dig up my blog article about creating an application that is ONLY a NotifyIcon by default if you want.
Inside your form class, add this:
It's as easy as that; works a charm!