开发者

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

开发者 https://www.devze.com 2022-12-29 19:40 出处:网络
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 house

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 housecleanin开发者_如何学编程g 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?


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消