WinForm window minimized to tray and right-click Icon Display Menu

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.