C # System Application form minimized to taskbar and common operations

Source: Internet
Author: User
now a lot of software such as 360 anti-virus software, cool dog music all have the minimize to the taskbar and leave an icon in the system tray area, my project also needs this feature. So this article mainly describes how to use C # to minimize the form to the taskbar, while the system tray icon click the left button to display the form, The right key enables the associated control to display 3 commonly used functions. Mainly involved in the control is NotifyIcon and ContextMenuStrip, such articles are more, hope that the author in his own unique perspective to give everyone help. one. Interface Operation

1. Create a Windows Forms Application project, add NotifyIcon from the Toolbox (displays an icon in the notification area to the right of the Windows taskbar during runtime). Right-click the NotifyIcon1 property to add an icon to the control's property icon, The Text property is "CSDN".

2. Add ContextMenuStrip (Display the shortcut menu when the user right-clicks the associated control). Right mouse button ContextMenuStrip1 property, enter items Add or right "edit Item". Add 3 ToolStripMenuItem, Set its text to show form, hide form, exit. The following illustration shows:


3. Associated system tray icon and right-click menu. Set the ContextMenuStrip property of the NotifyIcon1 to associate two controls with the CONTEXTMENUSTRIP1. Run the program, the lower right corner of the taskbar's system tray icon Click on the right button to display the following image:


two. Form setup

form setup is mainly when the form clicks the "Exit" button, the taskbar still displays an icon and the program does not exit. Set the Form1 MaximizeBox (whether the form can be maximized) property is set to false so that it cannot be maximized. and add formclosing to Form1 (occurs before the form is closed and a reason for closing is made when the user closes the form). The following figure shows.

add code as follows, the main implementation of the function is when the user clicks the form "close" button or through the Alt+f4 shortcut close, cancel the shutdown operation and the form is hidden, the taskbar icon is still displayed:

Event
private void Form1_formclosing (object sender, FormClosingEventArgs e)
{//form closes prior to closing the form
    because click Close button or Alt+f4
    if (E.closereason = = closereason.userclosing)
    {
        e.cancel = true;           Canceling the shutdown behavior does not close the form this
        . Hide ();               Hide Form
    }
}

Where: The FormClosingEventArgs class provides data for the FormClosing event, whose property cancel Gets or sets whether the value of the event should be canceled, Closereason gets a value that indicates why the form was closed. ( as described in the MSDN FormClosingEventArgs Class )
Note: The added event occurs before the form_closing-form closes, not the form_closed form has been closed. It does not have a E.cancel property and prompts for an error. System.Windows.Forms.FormClosedEventArgs "does not contain the definition of cancel.
three. System Tray function

Common forms to minimize the functionality of the taskbar (System tray) icon:
1. When the left mouse button click on the icon, display the form.
2. When the right mouse button clicks on the icon, display the "show form" \ "hidden form" \ "Exit" menu bar, and have the corresponding function.
The specific action is: Click "Show form" \"Hide form" \ "Exit" To add "click" Event in its property bar. Add the following code:

//"Show form" click event private void Toolstripmenuitem1_click (object sender, EventArgs e) {this.                                Show (); Form displays this.  WindowState = Formwindowstate.normal; Form state default Size this.                            Activate (); Activate form to give focus}//"Hide form" click event private void Toolstripmenuitem2_click (object sender, EventArgs e) {this.                      Hide (); Hide form}//exit Click event private void Toolstripmenuitem3_click (object sender, EventArgs e) {//Click Yes to exit program if (Mess Agebox.show ("OK to exit the program?", "Security tips," System.Windows.Forms.MessageBoxButtons.YesNo, System.windo Ws.    Forms.MessageBoxIcon.Warning = = System.Windows.Forms.DialogResult.Yes) {notifyicon1.visible = false; The set icon is not visible this.                  Close (); Close form this.                Dispose ();            Releasing resources application.exit (); Close Application Form}} 
where the form's state formwindowstate has minimized (minimized), maximized (maximized), Normal (default size). Some programs set up the SizeChanged event when the user clicks "Minimize" The button form is minimized to the taskbar (System tray) when it changes size. But I think it's better to have the minimized icon when you open the program, and add formclosing events more in line with the user. Click "Exit" to run the results as shown in the following figure:

Finally, add the left mouse button icon to display the form function. Right-notifyIcon1 property, adding MouseClick (occurs when the mouse clicks a component) event. Add code as follows:

Left mouse button icon event
private void Notifyicon1_mouseclick (object sender, MouseEventArgs e)
{
    //Click the left mouse button to occur
    if (E . Button = = MouseButtons.Left)
    {this
        . Visible = true;                        form is visible this
        . WindowState = Formwindowstate.normal;  form default size
        this.notifyIcon1.Visible = true;            Set icon visible
    }
}
Four. Complete code

The source code is as follows:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Using System.Windows.Forms; namespace Winformmin {public partial class Form1:form {public Form1 () {Initialize
        Component ();
            Event private void Form1_formclosing (object sender, FormClosingEventArgs e) {Before the form closes  The form closes because clicking the Close button or alt+f4 if (E.closereason = = closereason.userclosing) {E.cancel           = true; Canceling the shutdown behavior does not close the form this.               Hide (); Hide form}//Show form click event private void Toolstripmenuitem1_click (object sender, EventArg S e) {this.                                Show (); Form displays this.  WindowState = Formwindowstate.normal; Form state default Size this.                       Activate ();     Activate form give focus}//Hide form Click event private void Toolstripmenuitem2_click (object sender, EventArgs e) {this.                      Hide ();
            Hide form}//exit Click event private void Toolstripmenuitem3_click (object sender, EventArgs e) { Click "Yes" to exit the program if (MessageBox.Show ("OK to exit program?", "Security Prompt", System.Windows.For Ms. Messageboxbuttons.yesno, System.Windows.Forms.MessageBoxIcon.Warning) = = System.wi Ndows.   Forms.DialogResult.Yes) {notifyicon1.visible = false; The set icon is not visible this.                  Close (); Close form this.                Dispose ();            Releasing resources application.exit (); Close Application Form}//left mouse button icon event private void Notifyicon1_mouseclick (object sender, Mouseeve Ntargs e) {//Click the left mouse button to take place if (E.button = = MoUsebuttons.left) {this.                        Visible = true; form is visible this.  WindowState = Formwindowstate.normal;            form default Size this.notifyIcon1.Visible = TRUE; Set icon Visible}}}
Five. Summary The article is mainly combined with its own project completion, mainly to minimize the form to the taskbar (system tray) while including some common operations. The following two articles involve clicking the "Minimize" button to achieve the minimum to the taskbar, slightly different from the article, If you want to do this you can read. Thanks to the two-bit article author. http://blog.sina.com.cn/s/blog_45eaa01a01013u36.html
http://blog.csdn.net/furturerock/article/details/5687793Finally, hope that the article for everyone to help, if the article has errors or deficiencies, please Haihan!
(By:eastmount 2014-1-21 6 o'clock in the afternoon http://blog.csdn.net/eastmount)

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.