Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put a program in the system tray at startup

Tags:

c#

.net

vb.net

I followed the commonly-linked tip for reducing an application to the system tray : http://www.developer.com/net/csharp/article.php/3336751 Now it works, but there is still a problem : my application is shown when it starts ; I want it to start directly in the systray. I tried to minimize and hide it in the Load event, but it does nothing.

Edit : I could, as a poster suggested, modify the shortcut properties, but I'd rather use code : I don't have complete control over every computer the soft is installed on.

I don't want to remove it completely from everywhere except the systray, I just want it to start minimized.

Any ideas ?

Thanks

like image 953
thomasb Avatar asked Nov 12 '08 11:11

thomasb


People also ask

How do I put a program in the system tray?

Select Start , select the arrow next to All apps, right-click the app, then select More > Pin to taskbar. If the app is already open on the desktop, press and hold (or right click) the app's taskbar icon, and then select Pin to taskbar.


2 Answers

In your main program you probably have a line of the form:

Application.Run(new Form1());

This will force the form to be shown. You will need to create the form but not pass it to Application.Run:

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

Note that the program will now not terminate until you call Application.ExitThread(). It's best to do this from a handler for the FormClosed event.

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    Application.ExitThread();
}
like image 106
Sunlight Avatar answered Sep 23 '22 01:09

Sunlight


this is how you do it

static class Program
{
    [STAThread]
    static void Main()
    {
        NotifyIcon icon = new NotifyIcon();
        icon.Icon = System.Drawing.SystemIcons.Application;
        icon.Click += delegate { MessageBox.Show("Bye!"); icon.Visible = false; Application.Exit(); };
        icon.Visible = true;
        Application.Run();
    }
}
like image 20
lubos hasko Avatar answered Sep 22 '22 01:09

lubos hasko