This article describes the WinForm implementation to minimize the system tray method. Share to everyone for your reference. The specific analysis is as follows:
There's a control called NotifyIcon.
1, build a WinForm project, other operations skipped.
2, pull a NotifyIcon control, set the property visable to False, in the Text property on a random fill in some files.
3, the implementation of the form of the SizeChanged event, the code is as follows:
?
1 2 3 4 5 |
if (this. WindowState = = formwindowstate.minimized)//Determine whether to minimize {this. ShowInTaskbar = false; Do not appear in system taskbar notifyicon.visible = true; Tray icon Visible} |
4, the implementation of the NotifyIcon control DoubleClick event, the code is as follows:
?
1 2 3 4 5 6 |
if (this. WindowState = = formwindowstate.minimized) {this. ShowInTaskbar = true; displayed in the system taskbar this. WindowState = Formwindowstate.normal; Restore form notifyicon.visible = false; Tray icon Hide} |
Examples:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the same as the same as the same as the same as the same as the same as the other, at the same (). |
private ContextMenu notifyiconmnu; #region minimized to taskbar///<summary>///minimize to taskbar </summary> private void Initializenotifyicon () {//define a MenuItem array and assign the array to the ContextMenu object menuitem[] Mnuitms = new Menuitem[3]; Mnuitms[0] = new MenuItem (); Mnuitms[0]. Text = "Display window"; Mnuitms[0]. Click + + new System.EventHandler (This.notifyicon1_showfrom); MNUITMS[1] = new MenuItem ("-"); MNUITMS[2] = new MenuItem (); MNUITMS[2]. Text = "Exit system"; MNUITMS[2]. Click + + new System.EventHandler (this. Exitselect); MNUITMS[2]. Defaultitem = true; Notifyiconmnu = new ContextMenu (MNUITMS); Notifyicon1.contextmenu = Notifyiconmnu; Add a set of ContextMenu objects to the tray program private void Notifyicon1_doubleclick (object sender, EventArgs e) {if (this.WindowState = = formwindowstate.minimized) {this. Show (); This. ShowInTaskbar = true; This. WindowState = Formwindowstate.normal; Notifyicon1.visible = false; } public void Notifyicon1_showfrom (object sender, System.EventArgs e) {if (this. WindowState = = formwindowstate.minimized) {this. Show (); This. ShowInTaskbar = true; This. WindowState = Formwindowstate.normal; Notifyicon1.visible = false; } public void Exitselect (object sender, System.EventArgs e) {//Hide the icon in the tray program notifyicon1.visible = false;//Shut down system this. Close (); This. Dispose (TRUE); #endregion private void Form_main_sizechanged (object sender, EventArgs e) {if (this. WindowState = = formwindowstate.minimized)//Determine whether to minimize {notifyicon1.visible = true; Hide (); This. ShowInTaskbar = false; Initializenotifyicon (); } } |
I hope this article will help you with your C # programming.