WinForm minimized to tray, tray Right-click menu display

Source: Internet
Author: User

Add the NotifyIcon control and add icon, otherwise the tray has no icon (the Tray Right-click menu can also be added directly in the attribute);

The main code:

 Public Partial classForm1:form {#region        //Create a NotifyIcon objectNotifyIcon NotifyIcon =NewNotifyIcon (); //Create a tray icon objectIcon ico =NewIcon ("U9.ico"); //Creating a tray Menu ObjectContextMenu Notifycontextmenu =NewContextMenu (); #endregion         PublicForm1 () {InitializeComponent (); }        Private voidForm1_Load (Objectsender, EventArgs e) {            //set the text on the top of the tray icon with the mouse             This. Notifyicon1.text ="Code farming Network test"; }        /// <summary>        ///Minimized to Pallet/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidForm1_sizechanged (Objectsender, EventArgs e) {            //determine if the Minimize button is selected            if(WindowState = =formwindowstate.minimized) {//tray display icon equals tray icon Object//Note that NotifyIcon1 is the name of the control, not the name of the objectNotifyicon1.icon =ico; //Hide taskbar area icons                 This. ShowInTaskbar =false; //icon displayed in tray areaNotifyIcon. Visible =true; }        }        /// <summary>        ///Tray Click event/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidNotifyicon1_mouseclick (Objectsender, MouseEventArgs e) {            Switch(E.button) { CaseSystem.Windows.Forms.MouseButtons.Left://Left click//Determine if the pallet has been minimized                    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 HiddenNotifyIcon. Visible =false; }                     Break;  CaseSystem.Windows.Forms.MouseButtons.Right://MessageBox.Show ("right button");                     Break; }        }    }

WinForm minimized to tray, tray Right-click menu display

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.