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 ();
}