WinForm實現(xiàn)最小化到系統(tǒng)托盤方法實例詳解
更新時間:2015年05月25日 10:43:52 作者:陌香
這篇文章主要介紹了WinForm實現(xiàn)最小化到系統(tǒng)托盤方法,實例分析了C#中實現(xiàn)WinForm最小化到系統(tǒng)托盤所需的相關(guān)控件與使用技巧,需要的朋友可以參考下
本文實例講述了WinForm實現(xiàn)最小化到系統(tǒng)托盤方法。分享給大家供大家參考。具體分析如下:
有個叫NotifyIcon的控件
1、建個WinForm項目,其它操作略過。
2、拉個NotifyIcon控件,將屬性Visable設(shè)置成False,在Text屬性上隨便填些文件。
3、實現(xiàn)Form的SizeChanged事件,代碼如下:
if(this.WindowState == FormWindowState.Minimized) //判斷是否最小化 { this.ShowInTaskbar = false; //不顯示在系統(tǒng)任務(wù)欄 notifyIcon.Visible = true; //托盤圖標(biāo)可見 }
4、實現(xiàn)NotifyIcon控件的DoubleClick事件,代碼如下:
if(this.WindowState == FormWindowState.Minimized) { this.ShowInTaskbar = true; //顯示在系統(tǒng)任務(wù)欄 this.WindowState = FormWindowState.Normal; //還原窗體 notifyIcon.Visible = false; //托盤圖標(biāo)隱藏 }
例題:
private ContextMenu notifyiconMnu; #region 最小化到任務(wù)欄 /// <summary> /// 最小化到任務(wù)欄 /// </summary> private void Initializenotifyicon() { //定義一個MenuItem數(shù)組,并把此數(shù)組同時賦值給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 = "退出系統(tǒng)"; mnuItms[2].Click += new System.EventHandler(this.ExitSelect); mnuItms[2].DefaultItem = true; notifyiconMnu = new ContextMenu(mnuItms); notifyIcon1.ContextMenu = notifyiconMnu; //為托盤程序加入設(shè)定好的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) { //隱藏托盤程序中的圖標(biāo) notifyIcon1.Visible = false; //關(guān)閉系統(tǒng) 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#程序設(shè)計有所幫助。
相關(guān)文章
Winform學(xué)生信息管理系統(tǒng)各子窗體剖析(3)
這篇文章主要針對Winform學(xué)生信息管理系統(tǒng)各子窗體進行剖析,感興趣的小伙伴們可以參考一下2016-05-05