Form minimization means that the entire form is reduced to the taskbar, but the form is displayed on the taskbar as the form's title bar, and if you want the form to appear in the lower-right corner of the taskbar as an icon, you need to add a NotifyIcon control to the form.
One, add NotifyIcon control
1 If you add a NotifyIcon control to the form and specify the Icon and Text properties, such as:
2 in the processing code that requires the form to be minimized to the pallet, add the following code:
if (this.) windowstate==formwindowstate.normal&&this. visible==true) { this.notifyicon1.visible=true; the icon of the form is displayed in the notification area . windowstate=formwindowstate.minimized; this. Visible=false; this. Showintaskbar=false; // make form not appear on taskbar }
3 You can specify double-click events for the Add NotifyIcon control, double-click Restore, and the code is as follows:
/// <summary> ///Add double-click Tray icon Event (double-click the Display window)/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param>PrivatevoidNotifyicon1_mousedoubleclick (Objectsender, MouseEventArgs e) { if(WindowState = =formwindowstate.minimized) {//Restore form DisplayWindowState =Formwindowstate.normal; //activate the form and give it focus This. Activate (); //taskbar area Display icon This. ShowInTaskbar =true; //Tray area icon HiddenNotifyicon1.visible =false; } }
4 Close the form to ask whether to exit directly or minimize to the pallet
/// <summary> ///determine whether to minimize and then display the pallet/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param>PrivatevoidF_main_sizechanged (Objectsender, EventArgs e) { //determine if the Minimize button is selectedif(WindowState = =formwindowstate.minimized) {//Hide taskbar area icons This. ShowInTaskbar =false; //icon displayed in tray areaNotifyicon1.visible =true; } } /// <summary> ///confirm whether to exit/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param>PrivatevoidF_main_formclosing (Objectsender, FormClosingEventArgs e) { if(MessageBox.Show ("do you want to confirm exiting the program? ","Exit", Messageboxbuttons.okcancel, messageboxicon.question) = =DialogResult.OK) {//Close all the threads This. Dispose (); This. Close (); } Else{E.cancel=true; } }
Second, add ContextMenuStrip control
1 Add a ContextMenuStrip control to the form, then add the Control menu item, and finally bind to the NotifyIcon control, as follows:
2 Bind the menu to the NotifyIcon control, such as:
3 Specify the Click event for the menu item for the ContextMenuStrip control, as follows:
/// <summary> ///Tray Right-click to display the main interface/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param>PrivatevoidShow Toolstripmenuitem_click (Objectsender, EventArgs e) {WindowState=Formwindowstate.normal; } /// <summary> ///Tray Right-click Exit program/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param>PrivatevoidExit Toolstripmenuitem_click (Objectsender, EventArgs e) { if(MessageBox.Show ("do you want to confirm exiting the program? ","Exit", Messageboxbuttons.okcancel, messageboxicon.question) = =DialogResult.OK) {//Close all the threads This. Dispose (); This. Close (); } }
Third, for
The NotifyIcon control adds a click event, controls the display of the menu and restores the window, with the following code:
Private void Myicon_mouseclick (object sender, MouseEventArgs e) { if (E.button = = mousebuttons.right) { mymenu.show (); } if (E.button = = mousebuttons.left) { thistrue; this. WindowState = formwindowstate.normal; } }
WinForm window minimized to tray and right-click Icon Display Menu