How to start the application directly in system tray? (.NET C#)

后端 未结 3 522
终归单人心
终归单人心 2021-01-04 18:19

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, wh

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-04 19:04

    You need to set up the notify icon as well.

    Either manually or via the toolbar (drag a notifyIcon onto your form) create the notifyIcon:

    this.notifyIcon = new System.Windows.Forms.NotifyIcon(components);
    

    Then add this code to Form_Load():

    // Notify icon set up
    notifyIcon.Visible = true;
    notifyIcon.Text = "Tooltip message here";
    this.ShowInTaskbar = false;
    this.Hide();
    

    Though this will, as has been pointed out, briefly show the form before hiding it.

    From the accepted answer of this question, the solution appears to be to change:

    Application.Run(new Form1());
    

    to:

    Form1 f = new Form1();
    Application.Run();        
    

    in Main().

提交回复
热议问题