WinForm實(shí)現(xiàn)最小化到系統(tǒng)托盤方法實(shí)例詳解
本文實(shí)例講述了WinForm實(shí)現(xiàn)最小化到系統(tǒng)托盤方法。分享給大家供大家參考。具體分析如下:
有個(gè)叫NotifyIcon的控件
1、建個(gè)WinForm項(xiàng)目,其它操作略過(guò)。
2、拉個(gè)NotifyIcon控件,將屬性Visable設(shè)置成False,在Text屬性上隨便填些文件。
3、實(shí)現(xiàn)Form的SizeChanged事件,代碼如下:
if(this.WindowState == FormWindowState.Minimized) //判斷是否最小化
{
this.ShowInTaskbar = false; //不顯示在系統(tǒng)任務(wù)欄
notifyIcon.Visible = true; //托盤圖標(biāo)可見(jiàn)
}
4、實(shí)現(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()
{
//定義一個(gè)MenuItem數(shù)組,并把此數(shù)組同時(shí)賦值給ContextMenu對(duì)象
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對(duì)象
}
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();
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C# 實(shí)現(xiàn)SDL2進(jìn)行視頻播放窗口截圖和字幕添加
這篇文章主要介紹了C# 實(shí)現(xiàn)SDL2進(jìn)行視頻播放窗口截圖和字幕添加,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
C#實(shí)現(xiàn)TCP客戶端和服務(wù)器的基本功能
本文將介紹如何使用C#實(shí)現(xiàn)TCP客戶端和服務(wù)器的基本功能,客戶端與服務(wù)器可以相互發(fā)送消息,文章通過(guò)代碼講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-12-12
C#中Hashtable和Dictionary的區(qū)別
Hashtable 和 Dictionary 都是 C# 中用于存儲(chǔ)鍵值對(duì)的數(shù)據(jù)結(jié)構(gòu),本文主要介紹了C#中Hashtable和Dictionary的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
基于AForge實(shí)現(xiàn)C#攝像頭視頻錄制功能
這篇文章主要為大家詳細(xì)介紹了基于AForge實(shí)現(xiàn)C#攝像頭視頻錄制功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
C#實(shí)現(xiàn)的pdf生成圖片文字水印類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的pdf生成圖片文字水印類,結(jié)合完整實(shí)例形式分析了C#針對(duì)pdf文件的創(chuàng)建、添加文字、水印等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
TextBox獲取輸入焦點(diǎn)時(shí)自動(dòng)全選的實(shí)現(xiàn)方法
TextBox獲取輸入焦點(diǎn)時(shí)自動(dòng)全選的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-03-03

