C # WinForm start minimized to the right notification bar of the taskbar and interoperate

Source: Internet
Author: User

C # WinForm start minimized to the right notification bar of the taskbar and interoperate

One. Main functions:
(1), program start automatically hidden to the right of the taskbar notification bar display. (synonymous with system tray)
(2), double-click the system tray icon display, hidden window;
(3), right-click the system tray icon provides three menu options, "Exit", "Hide", "display";

Two. Related controls:
1, build a WinForm program-iconform, change the form property ShowInTaskbar to False, so the program will not be displayed in the taskbar.
2. Select the form attribute WindowState as minimized to automatically minimize the hidden
3, in the "common control" in the toolbar, drag into the NotifyIcon control-notifyicon1, this is the program to run the taskbar to the right of the notification area icon display control, for the control NotifyIcon property icon to add an icon, or from the code to join.
4. In the toolbar, in the "Menus and toolbars", drag into the CONTEXTMENUSTRIP-CONTEXTMENUSTRIP1, this control is right-click the context menu.
5, right-NotifyIcon1 Select the property, ContextMenuStrip its property to ContextMenuStrip1, this time NotifyIcon1 and CONTEXTMENUSTRIP1 two controls are associated.
6. Right-click ContextMenuStrip1, select Properties, go to items, then click "Add", add three menu options here: Exitmenuitem, Hidemenuitem, showMenuItem, Change its Text property to: Exit, hide, and display, respectively.


Three. Main code:
1. Double-click Iconform to add the Load event and then

Right-click the form, select Properties, go to the Events page, double-click the Load,sizechanged event, add code to the form

private void Form1_Load (object sender, EventArgs e)        {         //1. Change the form property ShowInTaskbar to False so that the program will not appear in the taskbar.         //2. Select the Form property WindowState as minimized so that it automatically minimizes the hidden.            string startup = Application.executablepath;       Get the program path               int pp = startup. LastIndexOf ("\ \");            startup = Startup. Substring (0, pp);            string icon = startup + "\\testIcon.ico";         3. You must set the icon for NotifyIcon1, otherwise it cannot be displayed in the notification bar. or set            Notifyicon1.icon = new icon (icon) in its properties;        private void Form1_sizechanged (object sender, EventArgs e)        {            if (this. WindowState = = formwindowstate.minimized)            {this                . Hide (); or this. Visible = false;                This.notifyIcon1.Visible = true;            }        }

Double-click the menu item on the form to add the relevant code

private void Exitmenuitem_click (object sender, EventArgs e)        {            if (MessageBox.Show ("Are you sure you want to quit the program?") "," Confirm ", Messageboxbuttons.okcancel, messageboxicon.question, messageboxdefaultbutton.button2) = = DialogResult.OK)            {                notifyicon1.visible = false;                This. Close ();                This. Dispose ();                Application.exit ();            }        }        private void Hidemenuitem_click (object sender, EventArgs e)        {this            . Hide ();        }        private void Showmenuitem_click (object sender, EventArgs e)        {this            . Show ();            This. WindowState = Formwindowstate.normal;            This. Activate ();        }

Three go to form design mode, right click NotifyIcon1, select Properties, double click on DoubleClick, add related code

private void Notifyicon1_doubleclick (object sender, EventArgs e)        {            if (this. WindowState = = Formwindowstate.normal)            {this                . WindowState = formwindowstate.minimized;                This. Hide ();            }            else if (this. WindowState = = formwindowstate.minimized)            {this                . Show ();                This. WindowState = Formwindowstate.normal;                This. Activate ();            }        }

Four. The complete code is as follows:

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.text;using system.windows.forms;namespace iconform{public partial class Form1:form {public F        Orm1 () {InitializeComponent (); }//description, the program is automatically hidden to the right of the taskbar in the notification bar,//1 right click to select Exit, hide, display//2 Double-click can hide and show toggle//A right click on the form, select Properties, go to the Events page, double-click the Load,sizechanged event, add to the form Code private void Form1_Load (object sender, EventArgs e) {//1. Change the form property ShowInTaskbar to False so that the program will not be in the taskbar         shown in.            2. Select the Form property WindowState as minimized to automatically minimize the hidden.       string startup = Application.executablepath; Get the program path int pp = startup.            LastIndexOf ("\ \"); startup = Startup.            Substring (0, pp);         string icon = startup + "\\testIcon.ico"; 3. You must set the icon for NotifyIcon1, otherwise it cannot be displayed in the notification bar.        or set Notifyicon1.icon = new icon (icon) in its properties;        private void Form1_sizechanged (object sender, EventArgs e){if (this. WindowState = = formwindowstate.minimized) {this. Hide (); or this.                Visible = false;            This.notifyIcon1.Visible = true; }//two double-click the menu item on the form to add the relevant code private void Exitmenuitem_click (object sender, EventArgs e) {if ( MessageBox.Show ("Are you sure you want to quit the program?")            "," Confirm ", Messageboxbuttons.okcancel, messageboxicon.question, messageboxdefaultbutton.button2) = = DialogResult.OK)                {notifyicon1.visible = false; This.                Close (); This.                Dispose ();            Application.exit (); }} private void Hidemenuitem_click (object sender, EventArgs e) {this.        Hide (); private void Showmenuitem_click (object sender, EventArgs e) {this.            Show (); This.            WindowState = Formwindowstate.normal; This.        Activate (); }//three go to form design mode, right-click NotifyIcon1, select Properties, double-click on DoubleClick, add related code private VOID Notifyicon1_doubleclick (object sender, EventArgs e) {if (this. WindowState = = Formwindowstate.normal) {this.                WindowState = formwindowstate.minimized; This.            Hide (); } else if (this. WindowState = = formwindowstate.minimized) {this.                Show (); This.                WindowState = Formwindowstate.normal; This.            Activate (); }        }    }}

  

C # WinForm start minimized to the right notification bar of the taskbar and interoperate

Related Article

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.