托盤程式的制作:
1.把NotifyIcon控件拉一個到窗體上,并設定NotifyIcon的Icon(很重要!否則運作後看不到效果)
2.窗體關閉時,将程式最小化到系統托盤上
?
1 2 3 4 5 6 7 8 | |
3.放一個上下文菜單,添加幾個基本項,"顯示主窗體","退出" ,将這個菜單挂到NotifyIcon上
?
1 2 3 4 5 6 7 8 9 10 11 | |
4.左鍵單擊托盤圖示時,顯示主窗體,右擊時當然是彈出上面設定的菜單
?
1 2 3 4 5 6 7 8 9 | |
防止這個程式同時運作多個
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
希望本文所述對大家C#程式設計有所幫助。
C#程式在托盤背景運作
源代碼如下:
請在工具欄把notifyIcon控件拖入窗體再使用:
C#代碼
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuIXY0N3Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmLzRHdnNWbvw1LcpDc0RHaiojIsJye.png)
- //隐藏窗體
- private bool windowCreate = true;
- private void toolStripMenuItem1_Click(object sender, EventArgs e) //這是菜單選項的一個item點選事件
- {
- if (windowCreate)
- {
- base.Visible = false;
- windowCreate = false;
- }
- this.Hide();
- base.OnActivated(e);
- }
- //顯示回窗體(notifyIcon控件輕按兩下事件,注:請選擇一個ico圖示,這樣隐藏後在右下角顯示有相關圖示)
- private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- if (this.Visible == true)
- {
- this.Hide();
- this.ShowInTaskbar = false;
- }
- else
- {
- this.Visible = true;
- this.ShowInTaskbar = true;
- this.WindowState = FormWindowState.Normal;
- //this.Show();
- this.BringToFront();
- windowCreate = true;
- }
- }
若想直接打開程式後,自動背景運作的話,以下代碼實作:
load函數中
this.Hide();
this.ShowInTaskbar = false;