WinForm set the task bar tray program

Programming interface is shown below:

 

 

1. In the program initialization settings to load the program icon, the specific code as follows:

private void Form1_Load(object sender, EventArgs e)
        {
            //this.ShowInTaskbar = false;
            //this.WindowState = FormWindowState.Minimized;
            string startUp = Application.ExecutablePath;
            int pp = startUp.LastIndexOf("\\");
            startUp = startUp.Substring(0,pp);
            string icon = startUp + "\\HHQC.ico";
            notifyIcon1.Icon = new Icon(icon);
}

 2. When the form is minimized, the taskbar notification icon display applications, code as follows:

private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if(this.WindowState == FormWindowState.Minimized){
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
        }

3. Set the task bar icon click event (double-click to display the main interface, right-click to display the menu), code as follows:

#region taskbar related operations
         // Double-task tray icon - displays the main screen 
        Private  void notifyIcon1_MouseDoubleClick ( Object SENDER, The MouseEventArgs E) 
        { 
            IF ( the this .WindowState == FormWindowState.Minimized) 
            { 
                the this the .Show ();
                 the this .WindowState = FormWindowState.Normal;
                 the this .Activate (); 
            } 
            the else 
            { 
                the this .WindowState = FormWindowState.Minimized;
                 the this .hide (); 
            } 
        }
        // Taskbar exit menu bar 
        Private  void toolStripMenu_Exit_Click ( Object SENDER, EventArgs E) 
        { 
            // this.Close (); 
            Application.Exit (); 
        } 
        // taskbar context menu bar - open the main interface 
        Private  void ToolStripMenu_Open_Click ( Object SENDER, EventArgs E) 
        { 
            IF ( the this .Visible) 
            { 
                the this .hide ();
                 the this .WindowState = FormWindowState.Minimized; 
            } 
            the else 
            { 
                the this the .Show ();
                this.WindowState = FormWindowState.Normal;
            }
        }
#endregion

4. The user form is not set top right corner exit the program when the close button only minimizes the system tray, the following code:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
                notifyIcon1.Visible = true;
                this.Hide();
                return;
            }
        }

5. taskbar icon context menu to exit the program or opt-out procedure in the program's main interface window menu

 private void toolStripMenu_Exit_Click(object sender, EventArgs e)
        {
            //this.Close();
            Application.Exit();
        }

 

Guess you like

Origin www.cnblogs.com/jian-xiao-stephen/p/11096190.html