C # implements the WinForm window to minimize the system tray code, and to determine the left and right mouse events

Source: Internet
Author: User

1. Set WinForm Form Properties Showintask=false
2. Add NotifyIcon control NotifyIcon1, adding an icon to the property icon for the control NotifyIcon1.
3. Add a form Minimize event (you need to add an event reference first):

 this . SizeChanged + = new  System.EventHandler (this  . form1_sizechanged);  //  The above line is the main form InitializeComponent () The reference that you need to add in the method  private  void  form1_ SizeChanged (object   sender, EventArgs e) {  if  (this . WindowState == formwindowstate.minimized) {  . Hide ();  true  

4. Add a click Icon event (you need to add an event reference first):
private void Notifyicon1_click (object sender, EventArgs e)
{
This. Visible = true;
This. WindowState = Formwindowstate.normal;
This.notifyIcon1.Visible = false;
}
5. You can add a right-click menu to NotifyIcon:
Drag a ContextMenuStrip control into the main form, add a menu in Nicontextmenu, and NotifyIcon1 Nicontextmenu as the context menu in the ContextMenu behavior.

6. Judging the left and right mouse events

Click on the NotifyIcon control, enter the code

      NotifyIcon1 Mouse Event Click (for example, double-click to select Double-click event)        private void Notifyicon1_mouseclick (object sender, MouseEventArgs e)        {            if (E.button = = MouseButtons.Left && E.clicks = = 1)            {//Left                         }            else if (E.button = = Mousebuttons.right && E.clicks = = 1)            {//Right                         }        }

C # implements the WinForm window to minimize the system tray code, and to determine the left and right mouse events

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.