简体   繁体   English

如何在没有可见窗口的情况下启动.net Windows窗体应用程序?

[英]How do I launch .net windows forms application with no visible windows?

I have a .net windows forms application that needs to open directly to the notify icon (system tray) with no visible windows. 我有一个.net Windows窗体应用程序,需要直接打开通知图标(系统托盘),没有可见的窗口。 I realize that I can do this in the onshown event or something like it. 我意识到我可以在onshown事件或类似事件中执行此操作。 But if I do that I get a flash of the window. 但如果我这样做,我会得到一个窗口的闪光。 How can I avoid that flash? 我怎么能避免那个闪光灯? I have tried modifying my Program.cs file to look like this: 我已经尝试修改我的Program.cs文件,如下所示:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

MainForm frm = new MainForm();
frm.Visible = false;
Application.Run(frm);

However this doesn't work either because Application.Run() makes the form visible. 但是这不起作用,因为Application.Run()使表单可见。 I am pretty sure there is an easy answer that I am missing. 我很确定我会错过一个简单的答案。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

There's an overload for Application.Run() that takes in no parameters, and thus doesn't immediately show a form on application launch. Application.Run()有一个重载,它不接受任何参数,因此在应用程序启动时不会立即显示表单。 Of course, you'll have to manage what causes the application to terminate yourself since there's no initial or 'main' form for it to monitor. 当然,您必须管理导致应用程序自行终止的原因,因为它没有初始或“主要”形式供其监控。 So for example it'd be your notification icon, which I'm sure you'll be able to handle. 所以例如它是你的通知图标,我相信你能够处理它。

如果您在启动应用程序时不需要主表单,则可以在此处链接到描述如何创建NotifyIcon的文章。

You can try setting WindowState on frm to Minimized along with ShowInTaskbar to false. 您可以尝试将frm上的WindowState设置为Minimized,同时将ShowInTaskbar设置为false。 Also, I'm no expert, but I think you should handle the visibility logic in the MainForm constructor. 另外,我不是专家,但我认为你应该处理MainForm构造函数中的可见性逻辑。

Here's a code snippet from the initialization method of a form I have that does exactly that. 这是我所拥有的表单的初始化方法的代码片段。 The app runs in the tray and the window shows when the user double clicks the notify icon. 应用程序在托盘中运行,当用户双击通知图标时,窗口会显示。 I have methods that handle resizing, etc. that ensure the form will only be closed through a menu option. 我有处理调整大小等的方法,确保只能通过菜单选项关闭表单。

public MainForm()
{
  ...code
  Resize += MainForm_Resize;
  notifyIcon.DoubleClick += NotifyIconDoubleClick;
  WindowState = FormWindowState.Minimized;
  Hide();
}
private void MainForm_Resize(object sender, EventArgs e)
{
  if (FormWindowState.Minimized == WindowState)
     Hide();
}

private void NotifyIconDoubleClick(object sender, EventArgs e)
{
   Show();
   try
   {
      WindowState = FormWindowState.Normal;
      ...more code for other stuff
    }catch(yadda yadda)
      ...code
    }
 }

Maybe a bit hackish, but you can create a borderless Form ( FormBorderStyle.None ) and set it's TransparencyKey to it's BackColor , disable ShowInTaskbar , then give that form to Application.Run(). 也许有点hackish,但你可以创建一个无边框Form( FormBorderStyle.None )并将它的TransparencyKey设置为它的BackColor ,禁用ShowInTaskbar ,然后将该表单提供给Application.Run()。 Voilà. 瞧。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM