public partial class Form1 : Form { private int Seconds; public Form1() { InitializeComponent();
// 任務欄是否顯示 this.ShowInTaskbar = false; timer1.Interval = 1000; timer1.Enabled = false; // 定時間隔:25分鍾 Seconds = 60 * 60; ShowTime(); } private void button1_Click(object sender, EventArgs e) {
// 倒計時開始 timer1.Start(); } private void ShowTime() { // 顯示剩余時間 label1.Text = string.Format("{0:d2}:{1:d2}", Seconds / 60, Seconds % 60); } private void timer1_Tick(object sender, EventArgs e) { Seconds--; if (Seconds < 0) { // 停止倒計時 timer1.Stop(); if (WindowState == FormWindowState.Minimized) { //還原窗體顯示 this.Show(); WindowState = FormWindowState.Normal; //激活窗體並給予它焦點 this.Activate(); //任務欄區顯示圖標 //this.ShowInTaskbar = true; //托盤區圖標隱藏 notifyIcon1.Visible = false; } return; } ShowTime(); } private void label1_Click(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { Seconds = 60 * 60; ShowTime(); } private void Form1_Load(object sender, EventArgs e) { } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
// 托盤圖標的雙擊事件 this.Show(); //還原窗體顯示 this.WindowState = FormWindowState.Normal; this.Activate(); //任務欄區顯示圖標 //this.ShowInTaskbar = true; //托盤區圖標隱藏 notifyIcon1.Visible = false; } private void Form1_Deactivat(object sender, EventArgs e) {
//借助失去焦點事件判斷窗口最小化 if (this.WindowState == FormWindowState.Minimized) { this.notifyIcon1.Visible = true; this.Hide(); this.ShowInTaskbar = false; } } }

