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