tags:

views:

383

answers:

3

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?

+2  A: 

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

ChrisF
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.
claws
Check out my solution then claws. This will fix this issue.
Matthew Scharley
Have you tried setting the `WindowState` to `Minimized`?
ChrisF
@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.
Matthew Scharley
A: 

Have you created a Windows Application in C#? You need to drag a NotifyIcon control onto the form, the control will be placed below the form because it has no visual representation on the form itself.

Then you set its properties such as the Icon...

Try this one first...

J Angwenyi
+9  A: 

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.

Matthew Scharley
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
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.
claws
`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: http://stackoverflow.com/questions/90697/how-to-create-and-use-resources-in-c-net-vs-2008
Matthew Scharley