C # minimize winform windows to system pallets

Source: Internet
Author: User

C # Hide the window appllication as the taskbar icon when writing the minimal code.

1. Set the winform attribute showintask to false.

2. Add the policyicon control policyicon1 to add an icon for the property icon of the control policyicon1.

3. Add a form minimization event (you must first add an event reference ):

This. sizechanged + = new system. eventhandler (this. form1_sizechanged );

// The above line is the reference to be added in the main form initializecomponent () method

Private void form1_sizechanged (Object sender, eventargs E)
{
If (this. windowstate = formwindowstate. Minimized)
{
This. Hide ();
This. policyicon1.visible = true;
}
}

4. Add a click icon event (you must first add an event reference ):

Private void policyicon#click (Object sender, eventargs E)
{
This. Visible = true;
This. windowstate = formwindowstate. normal;
This. policyicon1.visible = false;
}

5. You can right-click policyicon to add a menu:

Drag a contextmenu control nicontextmenu into the main form, click the control, and add a menu in the context menu. In the contextmenu action of notifyicon1, select nicontextmenu as the context menu.

This. policyicon1 = new system. Windows. Forms. policyicon (this. components );
This. nicontextmenu = new system. Windows. Forms. contextmenu ();
This. menuitem_hide = new system. Windows. Forms. menuitem ();
This. menuitem_show = new system. Windows. Forms. menuitem ();
This. menuitem_aubot = new system. Windows. Forms. menuitem ();
This. menuitem_exit = new system. Windows. Forms. menuitem ();

This. policyicon1.contextmenu = This. nicontextmenu;
This. policyicon1.icon = (system. Drawing. Icon) (resources. GetObject ("policyicon. Icon ")));
This. policyicon1.text = "";
This. policyicon1.visible = true;
This. policyicon1.doubleclick + = new system. eventhandler (this. policyicon1_doubleclick );
This. policyicon1.click + = new system. eventhandler (this. policyicon#click );

This. nicontextmenu. menuitems. addrange (

New system. Windows. Forms. menuitem []
{
This. menuitem_hide,
This. menuitem_show,
This. menuitem_aubot,
This. menuitem_exit
}
);

//
// Menuitem_hide
//
This. menuitem_hide.index = 0;
This. menuitem_hide.text = "hide ";
This. menuitem_hide.click + = new system. eventhandler (this. menuitem_hide_click );
//
// Menuitem_show
//
This. menuitem_show.index = 1;
This. menuitem_show.text = "show ";
This. menuitem_show.click + = new system. eventhandler (this. menuitem_show_click );
//
// Menuitem_aubot
//
This. menuitem_aubot.index = 2;
This. menuitem_aubot.text = "about ";
This. menuitem_aubot.click + = new system. eventhandler (this. menuitem_aubot_click );
//
// Menuitem_exit
//
This. menuitem_exit.index = 3;
This. menuitem_exit.text = "quit ";
This. menuitem_exit.click + = new system. eventhandler (this. menuitem_exit_click );

Protected override void onclosing (canceleventargs E)
{
This. showintaskbar = false;
This. windowstate = formwindowstate. minimized;
E. Cancel = true;
}
Protected override void onclosing (canceleventargs E)
{
// This. showintaskbar = false;
This. windowstate = formwindowstate. minimized;
E. Cancel = true;
}

Private void closectiserver ()
{
Timer. Enabled = false;
Dj160api. disablecard ();
This. policyicon. Visible = false;
This. Close ();
This. Dispose ();
Application. Exit ();
}

Private void hidectiserver ()
{
This. Hide ();
}

Private void showctiserver ()
{
This. Show ();
This. windowstate = formwindowstate. normal;
This. Activate ();

}
Private void ctimaniform_closing (Object sender, system. componentmodel. canceleventargs E)
{
This. closectiserver ();
}

Private void menuitem_show_click (Object sender, system. eventargs E)
{
This. showctiserver ();
}

Private void menuitem_aubot_click (Object sender, system. eventargs E)
{

}

Private void menuitem_exit_click (Object sender, system. eventargs E)
{
This. closectiserver ();
}

Private void menuitem_hide_click (Object sender, system. eventargs E)
{
This. hidectiserver ();
}

Private void ctimaniform_sizechanged (Object sender, system. eventargs E)
{
If (this. windowstate = formwindowstate. Minimized)
{
This. hidectiserver ();
}
}

Private void policyicon#doubleclick (Object sender, system. eventargs E)
{
This. showctiserver ();
}

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.