如何使程序不显示在 Alt-Tab 或任务栏上
我有一个程序需要位于后台,当用户连接到 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_DISCONNECT
或 WTS_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建一个隐藏窗口来处理消息。
You can create a hidden window that you use to handle the messages.
请参阅此问题:隐藏的最佳方法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 toFixedToolWindow
and itsShowInTaskbar
set tofalse
it will not appear in the Alt-Tab list.将其粘贴到您的代码中:
就这么简单。在 win7 64 位上完美运行,更重要的是 - 它不需要更改表单边框样式(我创建了一个类似小部件的应用程序,因此无法将样式设置为fixedToolWindow,此解决方案仍然是无边框且对 alt 不可见 -选项卡)。
Paste this, into your code:
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).