winform program minimized tray display
1. Create a new solution, the solution name and path of the custom
2. Create a new form in solution below, from the toolbox on the left, drag the NotifyIcon past form, the role of the control is: During the run the program displays an icon (what we call in the notification area on the right side of the Windows taskbar tray shown), as in FIG.
3. We can set the relevant property values of the control, directly on the code
// resizes the form of time-triggered events Private void the Form1_Resize ( Object SENDER, EventArgs E) { IF ( the this .WindowState == FormWindowState.Minimized) // when the form is provided to minimize { notifyIcon1.Visible = to true ; // the control seen the this .ShowInTaskbar = to false ; // display the window in the taskbar } the else { notifyIcon1.Visible = false ; // otherwise the control is not visible } } // triggered when double-click the control events Private void notifyIcon1_MouseDoubleClick ( Object SENDER, MouseEventArgs E) { the this .Visible = to true ; // the this .WindowState = FormWindowState.Normal; // window display properly the this .ShowInTaskbar = to true ; // display the window in the taskbar }
Another point, the NotifyIcon control our default setting is invisible
You can also set your favorite as a tray icon displays the icon
Then add an additional:
Add a right-click menu in NotifyIcon control
1. dragged into a context menu in the toolbar on the left contextMenuStrip
2. Add two sub-menu item, display and exit
3. Double-click Display to enter and exit, following directly attached to the code, the code is not explained, there is a comment.
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e) { the this .Visible = to true ; // seen the this .WindowState = FormWindowState.Normal; // normal display the this .ShowInTaskbar = to false ; // display the window in the taskbar } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { the this .Close (); // Exit the program }
4. The next step is the most critical, is to contextMenuStrip and NotifyIcon linking operation, as shown below:
ok, here to talk during the entire tray display has been completed, the above functions pro-test too, if wrong place above or have not understood the message can welcome correction.