The winForm close button is used to run pallets in the background (similar to QQ pallets)

Source: Internet
Author: User

You can click "close" to bring up a dialog box to determine whether to exit. This is a good solution and is easy to implement. But people don't want to do this. If they want to have the effect similar to that of QQ running in the background of the tray area, they can't, but find a way to achieve it. Www.2cto.com actually has a lot of implementation methods on the Internet, but I tried to copy it directly and it cannot be run directly, or it can run without comments. I will post my code here today, and I hope you will give your comments. [csharp] using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. windows; namespace winform Form tray background run {public partial class Form1: Form {// here there is no NotifyIcon control dragged on the Form, the variable private policyicon = null; public Form is defined here. 1 () {InitializeComponent (); // call the initialization tray display function InitialTray ();} private void form=load (object sender, EventArgs e) {// write other code here} www.2cto.com // <summary> // click the event to close the form /// </summary> /// <param name = "sender"> </param> // <param name = "e"> </param> private void Form1_FormClosing (object sender, formClosingEventArgs e) {e. cancel = true; // It can be seen from this that the close here is not actually a "close", but a form hidden to implement a "false close" this. hide ();} pr Ivate void InitialTray () {// hide the main form this. hide (); // instantiate a yyicon object NotifyIcon = new notifyIcon (); // The content of NotifyIcon displayed by the tray icon bubble. balloonTipText = "running in the background"; // The yyicon is displayed on the tray icon. text = "form tray background running test program"; // Note: The following paths can be absolute paths and relative paths. However, you must note that the file must be one. ico format: policyicon. icon = new System. drawing. icon ("E:/ASP project/images/3.5 inch Floppy. ico "); // true indicates visible in the tray area, and false indicates invisible in the tray area. visible = true; // time when the bubble is displayed (unit: milliseconds) notifyIcon. showBalloonTip (2000); policyicon. mouseClick + = new System. windows. forms. mouseEventHandler (notifyIcon_MouseClick); // you can specify the level-2 menu. // MenuItem setting1 = new MenuItem ("level-2 menu 1 "); // MenuItem setting2 = new MenuItem ("Level 2 menu 2"); // MenuItem setting = new MenuItem ("Level 1 menu", new MenuItem [] {setting1, setting2 }); // The help option. Here, "there is no real name" is displayed on the menu, and clicking "no effect" is displayed, you can click the event MenuItem help = new MenuItem ("help") by referring to the "Exit menu" below; // about the option MenuItem about = new MenuItem ("about "); // exit menu item MenuItem exit = new MenuItem ("exit"); exit. click + = new EventHandler (exit_Click); //// this row associated with the tray control // The Difference Between the comment and the next row is that the parameters are different, setting: this parameter is used to implement the level-2 menu // MenuItem [] childen = new MenuItem [] {setting, help, about, exit }; menuItem [] childen = new MenuItem [] {help, about, exit}; policyicon. contextMenu = new ContextMenu (childen); // this is triggered when the form is closed. formClosing + = new System. windows. forms. formClosingEventHandler (this. form1_FormClosing );} /// <summary> /// click the mouse /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void policyicon_mouseclick (object sender, system. windows. forms. mouseEventArgs e) {// click if (e. button = MouseButtons. left) {// if the form is visible, after you Left-click the tray area icon, the form is invisible if (this. visible = true) {this. visible = false;} else {this. visible = true; this. activate ();}}} /// <summary> /// exit option /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void exit_Click (object sender, eventArgs e) {// exit the program System. environment. exit (0 );}}}

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.