我的意思是当用户启动我的应用程序(exe)时。我希望它直接在系统托盘中启动,而不显示窗口。像防病毒软件和下载管理器,它们在系统托盘中静默启动和运行。
我想要相同的效果,当用户单击 notifyIcon 的上下文菜单条的“显示”按钮时,只有应用程序应该显示 GUI。
我正在用这个,但它不工作
private void Form_Load(object sender, EventArgs e)
{
this.Hide();
}
可能我需要在其他一些没有 GUI 但有 notifyIcon 和 ContextMenuStrip 的类中使用 Main() 函数,其选项将实例化 GUI 窗口类。对吧?
最佳答案
我通常这样设置的方式是修改 Program.cs
如下所示:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (NotifyIcon icon = new NotifyIcon())
{
icon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);
icon.ContextMenu = new ContextMenu(new MenuItem[] {
new MenuItem("Show form", (s, e) => {new Form1().Show();}),
new MenuItem("Exit", (s, e) => { Application.Exit(); }),
});
icon.Visible = true;
Application.Run();
icon.Visible = false;
}
}
使用这个,你不需要担心隐藏而不是关闭表单以及所有其他可能导致的黑客攻击......你也可以制作一个单例表单而不是实例化一个新的 Form
每次单击显示表单选项时。这是构建的基础,而不是最终解决方案。
关于c# - 如何直接在系统托盘中启动应用程序? (.NET C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1617784/