How to make a program not show up in Alt-Tab or on the taskbar
Asked Answered
P

3

11

I have a program that needs to sit in the background and when a user connects to a RDP session it will do some environment setup then launch a program. When the program is closed it will do some housekeeping and logoff the session.

The current way I am doing it is I have the terminal server launch this application. This is built as a windows forms application to keep the console window from showing up:

public static void Main()
{
    //(Snip...) Do some setup work

    Process proc = new Process();
    //(Snip...) Setup the process
    proc.Start();
    proc.WaitForExit();

    //(Snip...) Do some housecleaning

    NativeMethods.ExitWindowsEx(0, 0);
}

I really like this because there is no item in the taskbar and there is nothing showing up in alt-tab. However to do this I gave up access to functions like void WndProc(ref Message m) So Now I can't listen to windows messages (Like WTS_REMOTE_DISCONNECT or WTS_SESSION_LOGOFF) and do not have a handle to use for for bool WTSRegisterSessionNotification(IntPtr hWnd, int dwFlags); I would like my code to be more robust so it will do the housecleaning if the user logs off or disconnects from the session before he closes the program.

Any reccomendations on how I can have my cake and eat it too?

Pyle answered 7/5, 2010 at 22:5 Comment(0)
A
11

You can create a hidden window that you use to handle the messages.

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
  class Program
  {
    [STAThread]
    static void Main(string[] args)
    {
      Application.Run(new MessageWindow());        
    }
  }

  class MessageWindow : Form
  {
    public MessageWindow()
    {
      this.ShowInTaskbar = false;
      this.WindowState = FormWindowState.Minimized;
      // added by MusiGenesis 5/7/10:
      this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
    }

    protected override void WndProc(ref Message m)
    {
      base.WndProc(ref m);
    }
  }
}  
Ardene answered 7/5, 2010 at 22:15 Comment(3)
Tried that, it shows up in Alt-Tab. That is my backup solution but I would really like one that does not show up in the alt-tab menu.Pyle
If you have this set as a Windows application it should not show up in Alt-Tab.Ardene
I added a line that will make this work (and keep the window out of the Alt-Tab list).Interlay
I
7

See this question: Best way to hide a window from the Alt-Tab program switcher?

I tried all of the solutions, but no matter what I do the window still shows up in the Alt-Tab list (I'm running Vista).

In Windows Mobile, you set a form's Text property to blank to keep it out of the running programs list (the WinMo equivalent of the alt-tab list). Perhaps this will work for you, but I doubt it.

Update: OK, this does work after all. If you create and show a form with its FormBorderStyle set to FixedToolWindow and its ShowInTaskbar set to false it will not appear in the Alt-Tab list.

Interlay answered 7/5, 2010 at 22:47 Comment(0)
P
7

Paste this, into your code:

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

Simple as that. Works perfectly on win7 64bit and whats more important - it doesnt require, to change form border style (i've created a widget-like application so setting style to fixedToolWindow was not an option, with this solution its still borderless and invisible for alt-tab).

Pearson answered 19/6, 2013 at 21:33 Comment(1)
I did it and now the form looks as if I set FormBorderStyle to FixedToolWindowSterilization

© 2022 - 2024 — McMap. All rights reserved.