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

18,880

Solution 1

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.

Solution 2

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().

Share:
18,880

Related videos on Youtube

claws
Author by

claws

Updated on June 04, 2022

Comments

  • claws
    claws almost 2 years

    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.

    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();
        }
    

    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?

  • claws
    claws over 14 years
    Actually I've the notifyIcon setup. So, I just added the this.ShowInTaskbar = false; but the problem is that window is showing for an instant and then hiding. I don't want this.
  • Matthew Scharley
    Matthew Scharley over 14 years
    Check out my solution then claws. This will fix this issue.
  • Matthew Scharley
    Matthew Scharley over 14 years
    As a further note, you need to be using a GUI project, despite this code appearing before any GUI pops up, otherwise you'll get a console window popping up, which is even worse than the original problem!
  • Matthew Scharley
    Matthew Scharley over 14 years
    @ChrisF: hacks on hacks... that'll still pop up a window, it's just that it'll start minimised and will be less likely to be noticed.
  • claws
    claws over 14 years
    Awesome!! This is exactly what I'm looking for. But how do I change the icon.Icon ? I want to put my own *.ico here. instead of extracted one.
  • Matthew Scharley
    Matthew Scharley over 14 years
    new System.Drawing.Icon("C:\Path\To\Icon.ico") from memory, or use a resource (recommended). See this other question for details on how to create/use resources: stackoverflow.com/questions/90697/…
  • Thomas Levesque
    Thomas Levesque about 13 years
    Wow... I've been programming in C# for almost 10 years, and it's the first time I see a solution to that problem that isn't a dirty hack. Well done ;)
  • Bitterblue
    Bitterblue over 10 years
    How would you define the default context menu item (e.g. to open form on simple icon click) ?
  • Matthew Scharley
    Matthew Scharley over 10 years
    @mini-me: It's late, but I believe all types of clicks (left, double, ..) are all handled via events on the NotifyIcon. Simply hook the relevant event and open your form that way.
  • Bitterblue
    Bitterblue over 10 years
    Actually I wanted "Show form" to be bold. Like my AntiVirus is showing me a bold item, that I also access with a double click on the icon. It's probably a different question about context menus etc.
  • Matthew Scharley
    Matthew Scharley over 10 years
    @mini-me: It's been a while since I looked at C#, but I believe that either the ContextMenu or the MenuItem's have a default property to make them display bold like that. Wiring up the default action though is still up to you though, I think. YMMV.
  • ndreisg
    ndreisg about 5 years
    There seems to be a problem with this solution: I am using a MSI installer in my project, when uninstalling the application should automatically be closed. But when the form was never visible (the ApplicationContext's MainForm property is not set), WM_CLOSE events can't be handled.
  • Matthew Scharley
    Matthew Scharley about 5 years
    @ndreisg I don't have any practical answers since I haven't done Windows development much in the 10 years since this answer was put forth. More accurately though, there's no window to receive WM_ events at all. Rather, your installer needs to send a different/better message to the application. In Unix land, the obvious solution here would be a process signal. I'm not sure what the analogue is in Windows land. Perhaps a named pipe or similar.
  • ndreisg
    ndreisg about 5 years
    I think I don't really have control over how Windows Installer is closing the process. I solved this by setting the MainForm again and hiding it on the forms Load event. Exactly like the code example in the question does but before hiding i set ShowInTaskbar = false;