Best way to hide a window from the Alt-Tab program

2019-01-02 17:45发布

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.

13条回答
泛滥B
2楼-- · 2019-01-02 17:50

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:

// Keep this program out of the Alt-Tab menu

ShowInTaskbar = false;

Form form1 = new Form ( );

form1.FormBorderStyle = FormBorderStyle.FixedToolWindow;
form1.ShowInTaskbar = false;

Owner = form1;

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.

查看更多
浅入江南
3楼-- · 2019-01-02 17:53

Form1 Properties:
FormBorderStyle: Sizable
WindowState: Minimized
ShowInTaskbar: False

private void Form1_Load(object sender, EventArgs e)
{
   // Making the window invisible forces it to not show up in the ALT+TAB
   this.Visible = false;
}>
查看更多
无色无味的生活
4楼-- · 2019-01-02 17:53

if you want the form to be borderless, then you need to add the following statements to the form’s constructor:

this.FormBorderStyle = FormBorderStyle.None;
this.ShowInTaskbar = false;

AND you must add the following method to your derived Form class:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        // turn on WS_EX_TOOLWINDOW style bit
        cp.ExStyle |= 0x80;
        return cp;
    }
}

more details

查看更多
闭嘴吧你
5楼-- · 2019-01-02 17:54

I've found a solution, but it's not pretty. So far this is the only thing I've tried that actually works:

Window w = new Window(); // Create helper window
w.Top = -100; // Location of new window is outside of visible part of screen
w.Left = -100;
w.Width = 1; // size of window is enough small to avoid its appearance at the beginning
w.Height = 1;
w.WindowStyle = WindowStyle.ToolWindow; // Set window style as ToolWindow to avoid its icon in AltTab 
w.Show(); // We need to show window before set is as owner to our main window
this.Owner = w; // Okey, this will result to disappear icon for main window.
w.Hide(); // Hide helper window just in case

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 moving w.Hide() immediately after w.Show(), which works fine) into my application's constructor, creating a public static Window called OwnerWindow. Whenever I want a window to exhibit this behavior, I simply set this.Owner = App.OwnerWindow. Works great, and only involves creating one extra (and invisible) window. You can even set this.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 on w to prevent it from flashing briefly in the taskbar when shown.

查看更多
有味是清欢
6楼-- · 2019-01-02 17:54

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.

查看更多
柔情千种
7楼-- · 2019-01-02 17:58

Inside your form class, add this:

protected override CreateParams CreateParams
{
    get
    {
        var Params = base.CreateParams;
        Params.ExStyle |= 0x80;
        return Params;
    }
}

It's as easy as that; works a charm!

查看更多
登录 后发表回答