首先設置程序最小化到任務欄右下角
先給窗口添加一個notifyIcon控件
為notifyIcon控件設置ICO圖標(不設置圖標將無法在任務欄顯示)
給notifyIcon控件添加點擊事件
然后是最小化到任務欄右下角
if (this.WindowState == FormWindowState.Normal && this.Visible == true) { this.notifyIcon1.Visible = true;//在通知區顯示Form的Icon this.WindowState = FormWindowState.Minimized; this.Visible = false; this.ShowInTaskbar = false;//使Form不在任務欄上顯示 this.Hide(); }
接下來判斷點擊的是鼠標的哪個按鍵
/// <summary> /// 添加雙擊托盤圖標事件(雙擊顯示窗口) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left)//判斷鼠標的按鍵 { if (this.WindowState == FormWindowState.Normal) { this.WindowState = FormWindowState.Minimized; this.Hide(); } else if (this.WindowState == FormWindowState.Minimized) { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); } } else if (e.Button == MouseButtons.Right) { if (MessageBox.Show("是否需要關閉程序?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)//出錯提示 { //鼠標查詢進程 _MouseStop = false; if (Mouse_Thread != null) { Mouse_Thread.Abort(); Mouse_Thread = null; } //關閉窗口 DialogResult = DialogResult.No; Dispose(); Close(); } } }
如果點擊的是左鍵,就判斷窗口是不是最小化的,是就還原窗口。不是就隱藏窗口
if (e.Button == MouseButtons.Left)//判斷鼠標的按鍵 { if (this.WindowState == FormWindowState.Normal) { this.WindowState = FormWindowState.Minimized; this.Hide(); } else if (this.WindowState == FormWindowState.Minimized) { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); } }
如果點擊的是右鍵,則彈出提示框,提示用戶
else if (e.Button == MouseButtons.Right) { if (MessageBox.Show("是否需要關閉程序?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)//出錯提示 { //鼠標查詢進程 _MouseStop = false; if (Mouse_Thread != null) { Mouse_Thread.Abort(); Mouse_Thread = null; } //關閉窗口 DialogResult = DialogResult.No; Dispose(); Close(); } }
作者:逐夢
出處:http://www.cnblogs.com/huanjun/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。