WinForm 捕获最小化事件

[Dotnet]C# WinForm 捕获最小化事件

2009/02/10 09:22

转自:

虽然Form类没有提供Minimize的事件,但还是可以通过重载Deactive来实现
当Form失去焦点后,测试WindowState取得Form状态,若为Minimized既是最小化事件。
本例为最小化后隐藏窗口:

private void Form1_Deactivate(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                this.Visible = false;
        }

还有种方法更加直接,重载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);
}

Resize   事件   
private   void   Form1_Resize(object   sender,   System.EventArgs   e)   
{   
if   (this.WindowState   ==   FormWindowState.Minimized)   
{   
              //   Write   code   here   --   By   TheAres   
}   
}
原文地址:https://www.cnblogs.com/hdl217/p/1725251.html