首先,添加界面響應最小化的按鈕事件:(ps:直接上網搜的圖片,添加的事件)
1 private void pictureBox2_Click(object sender, EventArgs e) 2 { 3 this.WindowState = FormWindowState.Minimized;//最小化 4 5 }
重要部分:添加notifyIcon控件
1 private void notifyIcon_Click(object sender, EventArgs e)
2 { 3 4 5 if (this.WindowState != FormWindowState.Minimized) 6 { 7 this.WindowState = FormWindowState.Minimized; 8 } 9 else 10 { 11 this.WindowState = FormWindowState.Maximized; 12 } 13 14 }
在From_Load中添加事件內容:
1 private void Login_Load(object sender, EventArgs e) 2 { 3 //初始化圖片屬性 4 notifyIcon.Icon = new Icon("test.ico"); 5 //初始化是不可見的 6 notifyIcon.Visible = false; 7 //為notifyIcon添加Click事件 8 notifyIcon.Click += new System.EventHandler(this.notifyIcon_Click); 9 //為當前窗體添加窗體形狀改變響應函數 10 this.SizeChanged += new System.EventHandler(this.Login_SizeChanged); 11 }
使用:Resize事件進行狀態的判斷:
1 private void Login_Resize(object sender, EventArgs e) 2 { 3 //如果當前狀態的狀態為最小化,則顯示狀態欄的程序托盤 4 if (this.WindowState == FormWindowState.Minimized) 5 { 6 7 //不在Window任務欄中顯示 8 this.ShowInTaskbar = true; 9 //使圖標在狀態欄中顯示 10 this.notifyIcon.Visible = true; 11 } 12 }