6

Possible Duplicate:
What's the proper way to minimize to tray a C# WinForms app?

How can I create a program that runs in the background, and can be accessed via the Windows' "Notification Area" (Where the date and time are in the lower right hand corner)?

In other words, I want to be able to create a program that runs and can toggle between having a display window and not having a display window.

Community
  • 1
  • 1
sooprise
  • 22,657
  • 67
  • 188
  • 276
  • Checkout this answer http://stackoverflow.com/questions/46918/whats-the-proper-way-to-minimize-to-tray-a-c-winforms-app/47743#47743 in a related question. – Jorge Ferreira May 25 '10 at 22:18

1 Answers1

6
  1. Drag and drop a NotifyIcon and a ContextMenuStrip.
  2. Set de NotifyIcon's context menu to the one you added
  3. Add 2 menuitems (e.g. Restore, Exit)
  4. Set the Form event resize and do the following check

    private void MyForm_Resize(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized) this.Hide();
        else this.Show();
    }
    
    // you could also restore the window with a
    // double click on the notify icon
    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        this.Show();
        this.WindowState = FormWindowState.Normal;
    }
    

For a example can download this project

Don't worry about the right click event, the NotifyIcon will automatically detect it and show the ContextMenu

BrunoLM
  • 97,872
  • 84
  • 296
  • 452