C # WinForm window minimized to system tray _c# Tutorial

Source: Internet
Author: User
1. Set WinForm Form Properties Showintask=false
2. Add the NotifyIcon control NotifyIcon1, adding an icon icon for the property icon of the control NotifyIcon1.
3. Add form Minimize event (you need to add an event reference first):

Copy Code code as follows:

This. SizeChanged + = new System.EventHandler (this. form1_sizechanged);
The above line is the reference that needs 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.notifyicon1.visible=true;
}
}

4. Add Click Icon Event (first need to add Event reference):

private void Notifyicon1_click (object sender, EventArgs e)
{
This. Visible = true;
This. WindowState = Formwindowstate.normal;
This.notifyIcon1.Visible = false;
}
5. You can add the right button menu to NotifyIcon:
Drag a ContextMenu control nicontextmenu the main form, click the Control, add a menu to the context menu, and select Nicontextmenu as the context menu in the ContextMenu behavior of the notifyIcon1.
Copy Code code as follows:

This.notifyicon1 = new System.Windows.Forms.NotifyIcon (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.notifyIcon1.ContextMenu = this. Nicontextmenu;
This.notifyIcon1.Icon = ((System.Drawing.Icon) (resources. GetObject ("Notifyicon.icon"));
This.notifyIcon1.Text = "";
This.notifyIcon1.Visible = true;
This.notifyIcon1.DoubleClick + = new System.EventHandler (This.notifyicon1_doubleclick);
This.notifyIcon1.Click + = new System.EventHandler (This.notifyicon1_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 = "hidden";
This.menuItem_Hide.Click + = new System.EventHandler (This.menuitem_hide_click);
//
Menuitem_show
//
This.menuItem_Show.Index = 1;
This.menuItem_Show.Text = "Display";
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 = "Exit";
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. Notifyicon.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 Notifyicon1_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.