C#完成明滅托盤圖標後果的辦法。本站提示廣大學習愛好者:(C#完成明滅托盤圖標後果的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成明滅托盤圖標後果的辦法正文
本文實例講述了C#完成明滅托盤圖標後果的辦法。分享給年夜家供年夜家參考,詳細以下:
在用戶正在登錄QQ或許應用Firemail郵件體系主動收取郵件的時刻,托盤圖標會明滅提醒用戶正在運轉的義務。
明滅圖標可使用准時切換托盤圖標的方法完成,托盤圖標可以從ImageList控件中獲得。在ImageList控件外面添加三個icon,第一個icon表現窗體啟動今後的托盤圖標。第二個和第三個圖標分離表現當特定的義務產生的時刻,准時切換的圖標。
(1)設置托盤的圖標可以從ImageList控件中的Image對象轉換為Icon對象
/// <summary> /// 設置托盤顯示的圖標 /// </summary> /// <param name="index">圖象列表中圖片的索引</param> private void setIconImg(int index) { Image img = this.imgIcon.Images[index]; Bitmap b = new Bitmap(img); Icon icon = Icon.FromHandle(b.GetHicon()); this.niMain.Icon = icon; }
(2)准時器事宜完成圖標的切換
/// <summary> /// 准時器切換圖標顯示 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tmrIcon_Tick(object sender, EventArgs e) { if (iconFlag) { this.setIconImg(1); iconFlag = !iconFlag; } else { this.setIconImg(2); iconFlag = !iconFlag; } }
(3)窗體的最小化按鈕和封閉按鈕完成隱蔽窗體的功效,窗體封閉時,會履行FormClosing 事宜,釋放與此窗體聯系關系的一切資本。是以須要撤消封閉事宜,完成窗體的隱蔽和托盤的顯示功效。
/// <summary> /// 封閉窗體時隱蔽窗體顯示托盤圖標 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; this.Hide(); this.niMain.Visible = true; }
(4)托盤聯系關系菜單有四個功效:
① “顯示” :顯示主窗體、隱蔽托盤圖標。
② “運轉”:圖標明滅後果,模仿義務正在運轉。
③ “停滯”:恢復初始化的圖標,模仿義務停滯。
④ “加入”:提醒用戶並加入運用體系。
完全的代碼以下:
namespace NotifyIconDemo { public partial class FrmMain : Form { //切換圖片的標識 private bool iconFlag = false; //體系能否運轉 private bool isRun = false; /// <summary> /// /// </summary> public FrmMain() { InitializeComponent(); //設置icon顯示的圖片 this.setIconImg(0); } /// <summary> /// 設置托盤顯示的圖標 /// </summary> /// <param name="index">圖象列表中圖片的索引</param> private void setIconImg(int index) { Image img = this.imgIcon.Images[index]; Bitmap b = new Bitmap(img); Icon icon = Icon.FromHandle(b.GetHicon()); this.niMain.Icon = icon; } /// <summary> /// 顯示主窗體 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsmiMain_Click(object sender, EventArgs e) { //顯示主窗體 this.Visible = true; this.WindowState = System.Windows.Forms.FormWindowState.Normal; //隱蔽托盤圖標 this.niMain.Visible = false; this.Show(); } /// <summary> /// 加入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsmiExit_Click(object sender, EventArgs e) { //設置托盤的提醒信息 this.niMain.BalloonTipText = "勝利加入Demo!"; this.niMain.BalloonTipTitle = "加入"; this.niMain.ShowBalloonTip(1000*3); //延遲加入 Thread.Sleep(1000 * 2); //釋放托盤圖標資本 this.niMain.Dispose(); //終止線程 Application.ExitThread(); } /// <summary> /// 最小化時隱蔽窗體,顯示托盤圖標 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmMain_SizeChanged(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Hide(); this.niMain.Visible = true; } } /// <summary> /// 封閉窗體時隱蔽窗體顯示托盤圖標 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; this.Hide(); //this.ShowInTaskbar = false;//撤消窗體在義務欄的顯示 this.niMain.Visible = true; } /// <summary> /// 加載窗體時隱蔽窗體 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmMain_Load(object sender, EventArgs e) { this.Hide(); } /// <summary> /// 准時器切換圖標顯示 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tmrIcon_Tick(object sender, EventArgs e) { if (!this.isRun) { return; } if (iconFlag) { this.setIconImg(1); iconFlag = !iconFlag; } else { this.setIconImg(2); iconFlag = !iconFlag; } } /// <summary> /// 點擊運轉菜單 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsmiRun_Click(object sender, EventArgs e) { this.tsmiRun.Enabled = false; this.tsmiStop.Enabled = true; //設置運轉狀況 this.isRun = true; } /// <summary> /// 點擊停滯菜單 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsmiStop_Click(object sender, EventArgs e) { this.tsmiRun.Enabled = true; this.tsmiStop.Enabled = false; //設置為停滯狀況 this.isRun = false; //恢復圖標顯示 this.setIconImg(0); } } }
願望本文所述對年夜家C#法式設計有所贊助。