且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何启动直接在系统托盘中的应用程序? (.NET C#)

更新时间:2023-10-17 20:28:22

我的方式通常设置这样的事情是修改的Program.cs 看起来有点像以下内容:

The way I usually setup something like this is to modify the Program.cs to look something like the following:

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

使用这个,你不必担心隐藏的而不是封闭的形式,并且可以导致黑客的所有其余...你可以做一个单身的形式,而不是过多的实例化一个新的表格单击显示窗体选项每次。这是事关建设,而不是最终解决方案。

Using this, you don't need to worry about hiding instead of closing forms and all the rest of the hacks that can lead to... You can make a singleton form too instead of instantiating a new Form every time you click the show form option. This is something to build off of, not the end solution.