如何使程序不显示在 Alt-Tab 或任务栏上

发布于 2024-09-01 06:47:36 字数 1002 浏览 11 评论 0原文

我有一个程序需要位于后台,当用户连接到 RDP 会话时,它将进行一些环境设置,然后启动程序。当程序关闭时,它将执行一些内务处理并注销会话。

我目前的做法是让终端服务器启动此应用程序。这是作为 Windows 窗体应用程序构建的,以防止控制台窗口显示:

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

我真的很喜欢这个,因为任务栏中没有任何项目,并且 alt-tab 中也没有显示任何内容。然而,为了做到这一点,我放弃了对诸如 之类的函数的访问void WndProc(ref Message m) 所以现在我无法收听 Windows 消息(例如 WTS_REMOTE_DISCONNECTWTS_SESSION_LOGOFF)并执行以下操作没有用于 bool WTSRegisterSessionNotification( IntPtr hWnd, int dwFlags); 我希望我的代码更加健壮,这样如果用户在关闭程序之前注销或从会话断开连接,它将进行清理工作。

关于如何鱼与熊掌兼得,有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满意归宿 2024-09-08 06:47:36

您可以创建一个隐藏窗口来处理消息。

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

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);
    }
  }
}  
烟酉 2024-09-08 06:47:36

请参阅此问题:隐藏的最佳方法Alt-Tab 程序切换器中的窗口?

我尝试了所有解决方案,但无论我做什么,该窗口仍然显示在 Alt-Tab 列表中(我运行的是 Vista)。

在Windows Mobile 中,您将窗体的Text 属性设置为空白,以将其排除在正在运行的程序列表之外(WinMo 相当于alt-tab 列表)。也许这对你有用,但我对此表示怀疑。

更新:好吧,这毕竟确实有效。如果您创建并显示一个表单,其 FormBorderStyle 设置为 FixedToolWindow 且其 ShowInTaskbar 设置为 false 它将 < em>未出现在 Alt-Tab 列表中。

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.

蓬勃野心 2024-09-08 06:47:36

将其粘贴到您的代码中:

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

就这么简单。在 win7 64 位上完美运行,更重要的是 - 它不需要更改表单边框样式(我创建了一个类似小部件的应用程序,因此无法将样式设置为fixedToolWindow,此解决方案仍然是无边框且对 alt 不可见 -选项卡)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文