1. 程式人生 > >C#最小化到托盤+雙擊托盤恢復

C#最小化到托盤+雙擊托盤恢復

1.新增notifyIcon控制元件,並新增Icon,否則托盤沒有圖示(托盤右鍵選單也可直接在屬性裡新增);

2.響應Form的Resize或SizeChanged訊息:

        // Hide to system tray
        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.ShowInTaskbar = false;
                this.notifyIcon.Visible = true;
            }
        }

3.雙擊托盤圖示恢復需要響應notifyIcon的DoubleClick訊息:

        // Show from system tray
        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                notifyIcon.Visible = false;
                this.ShowInTaskbar = true;
            }
        }