如何检测窗体何时被最小化?

时间:2022-10-01 07:15:40

I know that I can get the current state by WindowState, but I want to know if there's any event that will fire up when the user tries to minimize the form.

我知道我可以通过WindowState获取当前状态,但我想知道当用户尝试最小化表单时是否会发生任何事件。

4 个解决方案

#1


You can use the Resize event and check the Forms.WindowState Property in the event.

您可以使用Resize事件并检查事件中的Forms.WindowState属性。

private void Form1_Resize ( object sender , EventArgs e )
{
    if ( WindowState == FormWindowState.Minimized )
    {
        // Do some stuff
    }
}

#2


To get in before the form has been minimised you'll have to hook into the WndProc procedure:

要在表单最小化之前进入,您必须挂钩到WndProc过程:

    private const int WM_SYSCOMMAND = 0x0112;
    private const int SC_MINIMIZE = 0xF020; 

    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    protected override void WndProc(ref Message m)
    {
        switch(m.Msg)
        {
            case WM_SYSCOMMAND:
                int command = m.WParam.ToInt32() & 0xfff0;
                if (command == SC_MINIMIZE)
                {
                    // Do your action
                }
                // If you don't want to do the default action then break
                break;
        }
        base.WndProc(ref m);
    }

To react after the form has been minimised hook into the Resize event as the other answers point out (included here for completeness):

在表单被最小化后做出反应,挂钩进入Resize事件,其他答案指出(包含在这里是为了完整性):

private void Form1_Resize (object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)
    {
        // Do your action
    }
}

#3


I don't know of a specific event, but the Resize event fires when the form is minimized, you can check for FormWindowState.Minimized in that event

我不知道具体事件,但是当窗体最小化时会触发Resize事件,您可以在该事件中检查FormWindowState.Minimized

#4


For people who search for WPF windows minimizing event :

对于搜索最小化事件的WPF窗口的人:

It's a bit different. For the callback use WindowState :

这有点不同。对于回调使用WindowState:

private void Form1_Resize(object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)
    {
        // Do some stuff
    }
}

The event to use is StateChanged (instead Resize):

要使用的事件是StateChanged(而不是Resize):

public Main()
{
    InitializeComponent();
    this.StateChanged += Form1_Resize;
}

#1


You can use the Resize event and check the Forms.WindowState Property in the event.

您可以使用Resize事件并检查事件中的Forms.WindowState属性。

private void Form1_Resize ( object sender , EventArgs e )
{
    if ( WindowState == FormWindowState.Minimized )
    {
        // Do some stuff
    }
}

#2


To get in before the form has been minimised you'll have to hook into the WndProc procedure:

要在表单最小化之前进入,您必须挂钩到WndProc过程:

    private const int WM_SYSCOMMAND = 0x0112;
    private const int SC_MINIMIZE = 0xF020; 

    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    protected override void WndProc(ref Message m)
    {
        switch(m.Msg)
        {
            case WM_SYSCOMMAND:
                int command = m.WParam.ToInt32() & 0xfff0;
                if (command == SC_MINIMIZE)
                {
                    // Do your action
                }
                // If you don't want to do the default action then break
                break;
        }
        base.WndProc(ref m);
    }

To react after the form has been minimised hook into the Resize event as the other answers point out (included here for completeness):

在表单被最小化后做出反应,挂钩进入Resize事件,其他答案指出(包含在这里是为了完整性):

private void Form1_Resize (object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)
    {
        // Do your action
    }
}

#3


I don't know of a specific event, but the Resize event fires when the form is minimized, you can check for FormWindowState.Minimized in that event

我不知道具体事件,但是当窗体最小化时会触发Resize事件,您可以在该事件中检查FormWindowState.Minimized

#4


For people who search for WPF windows minimizing event :

对于搜索最小化事件的WPF窗口的人:

It's a bit different. For the callback use WindowState :

这有点不同。对于回调使用WindowState:

private void Form1_Resize(object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)
    {
        // Do some stuff
    }
}

The event to use is StateChanged (instead Resize):

要使用的事件是StateChanged(而不是Resize):

public Main()
{
    InitializeComponent();
    this.StateChanged += Form1_Resize;
}