C # Close the program and display it in the lower right corner of the taskbar,

Source: Internet
Author: User

C # Close the program and display it in the lower right corner of the taskbar,

Preface

Generally, minimizing From will narrow the Form to the taskbar, but it is displayed in the Form of the Form title bar.

To narrow down the program to the taskbar in the lower right corner, you also need to add a policyicon control to the Form. The Code is as follows:

if (this.WindowState == FormWindowState.Normal && this.Visible == true)
             {
                 this.notifyIcon1.Visible = true; // Show the Icon of the Form in the notification area
                 this.WindowState = FormWindowState.Minimized;
                 this.Visible = false;
                 this.ShowInTaskbar = false; // Make Form not displayed on the taskbar
             }

Implementation Method

Minimize the WinForm window to the system tray, right-click the tray icon, and the exit menu is displayed.

1. Add the policyicon control myicon to the Form, add an Icon to the control's property icon, and Text is the tip displayed when the mouse is on the Icon.

2. Set the ShowInTaskbar attribute of Form in fromaskformclosing.

3. Set the ShowInTaskbar and WindowState attributes of Form in the myicon_MouseClick event.

4. Add the ContextMenuStrip control myMenu, right-click the tray icon to bring up the menu, and set the ContextMenuStrip attribute of myIcon to myMenu. Add an item to myMenu (exit ).

In the MouseClick event, select right-click and pop up ContextMenuStrip.

The Code is as follows:

private void Form1_FormClosing (object sender, FormClosingEventArgs e)
         {
             if (e.CloseReason == CloseReason.UserClosing) // Occurs when the user clicks the X button or (Alt + F4)
             {
                 e.Cancel = true;
                 this.ShowInTaskbar = false;
                 this.myIcon.Icon = this.Icon;
                 this.Hide ();
             }
         }

         private void myIcon_MouseClick (object sender, MouseEventArgs e)
         {
             if (e.Button == MouseButtons.Right)
             {
                 myMenu.Show ();
             }

             if (e.Button == MouseButtons.Left)
             {
                 this.Visible = true;
                 this.WindowState = FormWindowState.Normal;
             }
         }

         private void toolMenuCancel_Click (object sender, EventArgs e)
         {
             Application.Exit ();
         } 


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.