如何启动直接在系统托盘中的应用程序? (.NET C#) [英] How to start the application directly in system tray? (.NET C#)

查看:258
本文介绍了如何启动直接在系统托盘中的应用程序? (.NET C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户开始我的应用程序(EXE),我的意思。我希望它能够直接在系统盘启动,而不显示窗口。如防病毒软件和放大器;下载管理器,它启动和在系统托盘中自动运行。

I mean when the user starts my application(exe). I want it to start directly in system tray, without showing the window. Like antivirus softwares & download managers, which start and run in the system tray silently.

我想同样的效果,当用户点击显示NotifyIcon类的的ContextMenuStrip的按钮则只有应用程序应该显示界面。

I want the same effect and when user click the "show" button of the notifyIcon's contextmenustrip then only application should show GUI.

我用这一点,但它不工作

I'm using this, but its not working

    private void Form_Load(object sender, EventArgs e)
    {
        this.Hide();
    }

可能是我需要有main()函数中,没有GUI,但有NotifyIcon的&放一些其他类;的ContextMenuStrip的选项将实例化GUI窗口类。对吧?

May be I need to have Main() function in some other class which has no GUI but has notifyIcon & ContextMenuStrip whose option will instantiate the GUI window class. Right?

推荐答案

我的方式通常设置这样的事情是修改的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.

这篇关于如何启动直接在系统托盘中的应用程序? (.NET C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆