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 pro开发者_JAVA技巧gram that runs and can toggle between having a display window and not having a display window.
- Drag and drop a
NotifyIcon
and aContextMenuStrip
. - Set de NotifyIcon's context menu to the one you added
- Add 2 menuitems (e.g. Restore, Exit)
Set the Form event
resize
and do the following checkprivate 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
精彩评论