C # minimize the form of a system application to the taskbar and Common Operations

Source: Internet
Author: User

Nowadays, many software such as 360 anti-virus software and cool dog music have been minimized to the taskbar and marked in the system tray area. This function is also required for my projects. therefore, this article mainly describes how to use C # To minimize the form to the taskbar. At the same time, you can click the icon in the system tray area to display the form, right-click to display the three common functions associated with the control. the main controls involved are policyicon and ContextMenuStrip. There are many such articles. I hope the author can help you with his own unique perspective. i. interface operations

1. create a "Windows Forms Application" project and add the NotifyIcon from the "toolbox" (the icon is displayed in the notification area on the right of the Windows taskbar during runtime ). right-click policyicon1 and add an Icon to the control Icon. The Text attribute is "CSDN ".

2. add ContextMenuStrip (the shortcut menu is displayed when you right-click the associated control ). right-click contextMenuStrip1 and choose Items add or right-click Edit item ". add three toolStripMenuItem and set its Text to "display form", "hide form", and "exit ". as shown in:


3. Associate the system tray icon with the right-click menu. Set ContextMenuStrip attribute of notifyIcon1 to contextMenuStrip1 to associate two controls. Run the program, and right-click the system tray icon in the lower right corner of the taskbar, as shown in:


Ii. Form settings

When the form button is clicked, the taskbar still displays the icon and the program does not exit. set the MaximizeBox (whether the form can be maximized) attribute of Form1 to False, so that it cannot be maximized.Add a FormClosing event to Form1 (the event occurs before the form is closed and the cause is determined), as shown in.

The Code is as follows. The main function is to cancel the close operation and hide the form when you click the "close" button of the form or use Alt + F4 to close the form quickly. The taskbar icon is still displayed:

// Before the form is closed, the event private void form=formclosing (object sender, FormClosingEventArgs e) {// The reason for closing the form is to click the close button or Alt + F4 if (e. closeReason = CloseReason. userClosing) {e. cancel = true; // Cancel the close operation because the form is not closed. this. hide (); // Hide the form }}

Where: The FormClosingEventArgs class provides data for the FormClosing event, and its attribute Cancel gets or sets whether to Cancel the event value. CloseReason gets a value, which indicates the reason for closing the form .(For details, see the MSDN FormClosingEventArgs class.)
Note: The added event occurs before the Form_Closing-form is closed, rather than when the Form_Closed form is closed. it does not have e. "System. windows. forms. formClosedEventArgs "does not contain the Cancel definition.

Iii. System Tray Functions

The common function of minimizing forms to the taskbar (system tray) icon is as follows:
1. When the left mouse clicks the icon, the form is displayed.
2. When you right-click the icon, the "display form" \ "hide form" \ "exit" menu bar is displayed, and corresponding functions are available.
The specific operation is: click "display form "\"Hide form" \ "exit" add "Click" event in its attribute bar. Add the following code:

// "Display form" Click Event private void toolstripmenuitemdetail click (object sender, EventArgs e) {this. show (); // display this in the form. windowState = FormWindowState. normal; // The default size of the form status. this. activate (); // Activate the form to focus} // "hide form" Click Event private void toolStripMenuItem2_Click (object sender, EventArgs e) {this. hide (); // Hide the form} // "exit" Click Event private void toolStripMenuItem3_Click (object sender, EventArgs e) {// click "YES) "Exit the program if (MessageBox. show ("are you sure you want to exit Order? "," Security prompt ", System. windows. forms. messageBoxButtons. yesNo, System. windows. forms. messageBoxIcon. warning) = System. windows. forms. dialogResult. yes) {policyicon1.visible = false; // The setting icon is not visible this. close (); // Close the form this. dispose (); // release the resource Application. exit (); // close the application form }}
The FormWindowState of the form has the following options: Minimized (minimum), Maximized (maximum), and Normal (default size ). some programs set the sizechanged event. When the user clicks the "minimize" button, the size of the form is minimized to the taskbar (system tray ). however, I think it is better to have a minimal icon when opening a program, and adding a FormClosing event is more suitable for users. click "exit". The running result is shown in:

Finally, add the left mouse button icon to display the form function. Right-click the policyicon1 attribute and add the MouseClick event (which occurs when the mouse clicks the component). Add the following code:

// The left mouse button icon event private void policyiconw.mouseclick (object sender, MouseEventArgs e) {// click the left mouse button to create if (e. button = MouseButtons. left) {this. visible = true; // this is Visible to the form. windowState = FormWindowState. normal; // The default size of the form. this. policyicon1.visible = true; // sets the icon to be visible }}
Iv. 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 () {InitializeComponent () ;}// private void Form1_FormClosing (object sender, FormClosingEventArgs e) {// The reason for closing the form is to click" Close the "button or Alt + F4 if (e. closeReason = CloseReason. userClosing) {e. cancel = true; // Cancel the close operation because the form is not closed. this. hide (); // Hide form} // "display form" Click Event private void toolstripmenuitemdetail click (object sender, EventArgs e) {this. show (); // display this in the form. windowState = FormWindowState. normal; // The default size of the form status. this. activate (); // Activate the form to 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 ("are you sure you want to exit the program? "," Security prompt ", System. windows. forms. messageBoxButtons. yesNo, System. windows. forms. messageBoxIcon. warning) = System. windows. forms. dialogResult. yes) {policyicon1.visible = false; // The setting icon is not visible this. close (); // Close the form this. dispose (); // release the resource Application. exit (); // close the application form} // left mouse button icon event private void policyicon#mouseclick (object sender, MouseEventArgs e) {// click the left mouse button to create if (e. button = MouseButtons. left) {this. visible = true; // this is Visible to the form. windowState = FormWindowState. normal; // The default size of the form. this. policyicon1.visible = true; // sets the icon to be visible }}}}
V. Summary This article is mainly based on your own projects, mainly to minimize the form to the taskbar (system tray) and some common operations. at the same time, the following two articles involve clicking the "minimize" button to minimize the number of articles to the taskbar. They are slightly different from this article. If you want to do this, you can read it. thank you for the two authors. Http://blog.sina.com.cn/s/blog_45eaa01a01013u36.html
Http://blog.csdn.net/furturerock/article/details/5687793Finally, I hope this article will be helpful to you. If there are errors or deficiencies in this article, please try again!
(By: Eastmount six o'clock P.M. http://blog.csdn.net/eastmount)

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.