WinForm完成最小化到體系托盤辦法實例詳解。本站提示廣大學習愛好者:(WinForm完成最小化到體系托盤辦法實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是WinForm完成最小化到體系托盤辦法實例詳解正文
本文實例講述了WinForm完成最小化到體系托盤辦法。分享給年夜家供年夜家參考。詳細剖析以下:
有個叫NotifyIcon的控件
1、建個WinForm項目,其它操作略過。
2、拉個NotifyIcon控件,將屬性Visable設置成False,在Text屬性上隨意填些文件。
3、完成Form的SizeChanged事宜,代碼以下:
if(this.WindowState == FormWindowState.Minimized) //斷定能否最小化
{
this.ShowInTaskbar = false; //不顯示在體系義務欄
notifyIcon.Visible = true; //托盤圖標可見
}
4、完成NotifyIcon控件的DoubleClick事宜,代碼以下:
if(this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = true; //顯示在體系義務欄
this.WindowState = FormWindowState.Normal; //復原窗體
notifyIcon.Visible = false; //托盤圖標隱蔽
}
例題:
private ContextMenu notifyiconMnu;
#region 最小化就任務欄
/// <summary>
/// 最小化就任務欄
/// </summary>
private void Initializenotifyicon()
{
//界說一個MenuItem數組,並把此數組同時賦值給ContextMenu對象
MenuItem[] mnuItms = new MenuItem[3];
mnuItms[0] = new MenuItem();
mnuItms[0].Text = "顯示窗口";
mnuItms[0].Click += new System.EventHandler(this.notifyIcon1_showfrom);
mnuItms[1] = new MenuItem("-");
mnuItms[2] = new MenuItem();
mnuItms[2].Text = "加入體系";
mnuItms[2].Click += new System.EventHandler(this.ExitSelect);
mnuItms[2].DefaultItem = true;
notifyiconMnu = new ContextMenu(mnuItms);
notifyIcon1.ContextMenu = notifyiconMnu;
//為托盤法式參加設定好的ContextMenu對象
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
}
}
public void notifyIcon1_showfrom(object sender, System.EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
}
}
public void ExitSelect(object sender, System.EventArgs e)
{
//隱蔽托盤法式中的圖標
notifyIcon1.Visible = false;
//封閉體系
this.Close();
this.Dispose(true);
}
#endregion
private void Form_main_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
//斷定能否最小化
{
notifyIcon1.Visible = true;
this.Hide();
this.ShowInTaskbar = false;
Initializenotifyicon();
}
}
願望本文所述對年夜家的C#法式設計有所贊助。