主要功能:
C#讓窗體最小化至任務欄,同時在系統托盤區的圖標點擊左鍵能顯示窗體,並使窗體閃爍。
首先:
創建窗體應用程序,並添加控件NotifyIcon及ContextMenuStrip控件
NotifyIcon:點擊notifyIcon1屬性,為控件屬性Icon添加圖標
contextMenuStrip1屬性,進入Items添加或右鍵"編輯項".添加4個toolStripMenuItem,設置其Text為"顯示窗體"、"隱藏窗體"、"開始閃爍"、"退出"
代碼:
點擊窗體"關閉"按鈕時,取消關閉操作且窗體隱藏,任務欄圖標仍然顯示:
//窗體關閉前發生事件 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; //取消關閉操作 表現為不關閉窗體 this.Hide(); //隱藏窗體 } }
1.左鍵點擊圖標時,顯示窗體.
2.當鼠標右鍵點擊圖標時,顯示"顯示窗體"\"隱藏窗體"\"閃爍"\"退出"菜單欄,並有相對應的功能
//"顯示窗體"單擊事件 private void 顯示ToolStripMenuItem_Click(object sender, EventArgs e) { this.Show(); //窗體顯示 this.WindowState = FormWindowState.Normal; //窗體狀態默認大小 this.Activate(); } //"隱藏窗體"單擊事件 private void 隱藏ToolStripMenuItem_Click(object sender, EventArgs e) { this.Hide(); //隱藏窗體 } //"退出"單擊事件 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { //點擊"是(YES)"退出程序 if (MessageBox.Show("確定要退出程序?", "安全提示", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes) { notifyIcon1.Visible = false; //設置圖標不可見 this.Close(); //關閉窗體 this.Dispose(); //釋放資源 Application.Exit(); //關閉應用程序窗體 } }
鼠標左鍵圖標顯示窗體功能
//鼠標左鍵圖標事件 private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) { //點擊鼠標"左鍵"發生 if (e.Button == MouseButtons.Left) { this.Visible = true; //窗體可見 this.WindowState = FormWindowState.Normal; //窗體默認大小 this.notifyIcon1.Visible = true; //設置圖標可見 } }
圖標閃爍
閃爍的效果是加上一個空白的圖標,正常圖標與空白圖標的切換進而實現閃爍的效果。
property是vs的一個資源管理功能,可以存儲系統圖標及一些常量
private Icon timg = Properties.Resources.timg; private Icon blank = Properties.Resources.blank;//透明的圖標 private bool _status = true; private bool _isBlink = false;
右鍵菜單控制圖標是不是顯示
private void toolStripMenuItem1_Click(object sender, EventArgs e) { if (_isBlink == false) { _isBlink = true; timer1.Enabled = true; timer1.Start(); } else { _isBlink = false; timer1.Stop(); //氣泡提示 notifyIcon1.ShowBalloonTip(5000, "提示", "關閉閃爍效果!", ToolTipIcon.Info); } }
定時器中修改圖標的狀態,定時反轉圖標
private void timer1_Tick(object sender, EventArgs e) { if (_status) notifyIcon1.Icon = timg; else notifyIcon1.Icon = blank; _status = !_status; }
完整代碼:
public partial class Form1 : Form { private Icon timg = Properties.Resources.timg; private Icon blank = Properties.Resources.blank;//透明的圖標 private bool _status = true; private bool _isBlink = false; public Form1() { InitializeComponent(); } //窗體關閉前發生事件 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; //取消關閉操作 表現為不關閉窗體 this.Hide(); //隱藏窗體 } } //"顯示窗體"單擊事件 private void 顯示ToolStripMenuItem_Click(object sender, EventArgs e) { this.Show(); //窗體顯示 this.WindowState = FormWindowState.Normal; //窗體狀態默認大小 this.Activate(); } //"隱藏窗體"單擊事件 private void 隱藏ToolStripMenuItem_Click(object sender, EventArgs e) { this.Hide(); //隱藏窗體 } //"退出"單擊事件 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { //點擊"是(YES)"退出程序 if (MessageBox.Show("確定要退出程序?", "安全提示", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes) { notifyIcon1.Visible = false; //設置圖標不可見 this.Close(); //關閉窗體 this.Dispose(); //釋放資源 Application.Exit(); //關閉應用程序窗體 } } //鼠標左鍵圖標事件 private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) { //點擊鼠標"左鍵"發生 if (e.Button == MouseButtons.Left) { this.Visible = true; //窗體可見 this.WindowState = FormWindowState.Normal; //窗體默認大小 this.notifyIcon1.Visible = true; //設置圖標可見 } } private void toolStripMenuItem1_Click(object sender, EventArgs e) { if (_isBlink == false) { _isBlink = true; timer1.Enabled = true; timer1.Start(); } else { _isBlink = false; timer1.Stop(); //氣泡提示 notifyIcon1.ShowBalloonTip(5000, "提示", "關閉閃爍效果!", ToolTipIcon.Info); } } private void timer1_Tick(object sender, EventArgs e) { if (_status) notifyIcon1.Icon = timg; else notifyIcon1.Icon = blank; _status = !_status; } }