C # writes The Window appllication that is hidden as the taskbar icon when minimized.

Source: Internet
Author: User

1. Set WinForm Form Properties Showintask=false

2. Add NotifyIcon control NotifyIcon1, adding an icon to the property icon for the control NotifyIcon1.

3. Add a form Minimize event (you need to add an event reference first):

This. SizeChanged + = new System.EventHandler (this. form1_sizechanged);

private void Form1_sizechanged (object sender, EventArgs e)
{
if (this. WindowState = = formwindowstate.minimized)
{
This. Hide ();
This.notifyicon1.visible=true;
}
}

4. Add a click Icon event (you need to add an event reference first):

private void Notifyicon1_click (object sender, EventArgs e)
{
This. Visible = true;
This. WindowState = Formwindowstate.normal;
This.notifyIcon1.Visible = false;
}

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

Drag the main form into a ContextMenu control Nicontextmenu, click the control in the context menu, add a menu to the NotifyIcon1 contextmenu behavior, and select Nicontextmenu as the context menu.

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

C # writes The Window appllication that is hidden as the taskbar icon when minimized.

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.