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