C # How does winform minimize the main window,

Source: Internet
Author: User

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

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.