一般來說,雖然Form類沒有提供Minimize的事件,但還是可以通過重載Deactive來實現WinForm捕獲最小化事件。
實現方法為:當Form失去焦點后,測試WindowState取得Form狀態,若為Minimized既是最小化事件。
本例為最小化后隱藏窗口:
還有種方法更加直接,重載WndProc:
實現代碼如下:
const int WM_SYSCOMMAND = 0x112;const int SC_CLOSE = 0xF060;const int SC_MINIMIZE = 0xF020;const int SC_MAXIMIZE = 0xF030;protected override void WndProc(ref Message m){ if (m.Msg == WM_SYSCOMMAND) { if (m.WParam.ToInt32() == SC_MINIMIZE) { this.Visible = false; return; } } base.WndProc(ref m);}
private void Form1_Deactivate(object sender, EventArgs e){ if (this.WindowState == FormWindowState.Minimized) this.Visible = false;}
希望本文所述實例對大家C#程序設計有所幫助。
新聞熱點
疑難解答