C # How does winform minimize the main window,
1. If you do not want the program to be displayed in the taskbar, set ShowInTaskbar of the form attribute to false;
2. If you want to minimize program startup, set the WindowState attribute of the form to Minimized. (Minimal, Normal, and Maximized)
3. Pull a policyicon control and add an Icon for the property icon of the policyicon control.
4. You can add a ContextMenuStrip right-click the NotifyIcon and choose menu_Notify.
5. In this example, the form maximization button is disabled. (Set the MaximizeBox attribute of the form to false)
6. Main Code:
# Region private method: Hide and hide the form (Exit)
Private void ExitMainForm ()
{
If (MessageBox. Show ("are you sure you want to exit the test data receiving program? "," Confirm to exit ", MessageBoxButtons. OKCancel, MessageBoxIcon. Question, MessageBoxDefaultButton. Button2) = DialogResult. OK)
{
This. policyicon. Visible = false;
This. Close ();
This. Dispose ();
Application. Exit ();
}
}
Private void HideMainForm ()
{
This. Hide ();
}
Private void ShowMainForm ()
{
This. Show ();
This. WindowState = FormWindowState. Normal;
This. Activate ();
}
# Endregion # Right-click the region menu and choose "hide" to exit.
Private void menuItem_Show_Click (object sender, EventArgs e)
{
ShowMainForm ();
}
Private void menuItem_Hide_Click (object sender, EventArgs e)
{
HideMainForm ();
}
Private void menuItem_Exit_Click (object sender, EventArgs e)
{
ExitMainForm ();
}
# Endregion # display the form when double-clicking the tray icon
Private void policyicon_doubleclick (object sender, EventArgs e)
{
If (this. WindowState = FormWindowState. Normal)
{
This. WindowState = FormWindowState. Minimized;
HideMainForm ();
}
Else if (this. WindowState = FormWindowState. Minimized)
{
ShowMainForm ();
}
}
# Endregion
# When the region point is minimized, it is minimized to the tray.
Private void frmMain_SizeChanged (object sender, EventArgs e)
{
If (this. WindowState = FormWindowState. Minimized)
{
HideMainForm ();
}
}
# Endregion
# Minimize the number of region forms to the tray when the form is closed
Private void frmMain_FormClosing (object sender, FormClosingEventArgs e)
{
E. Cancel = true;
HideMainForm ();
}
# Endregion