简体   繁体   中英

Is there an event raised in C# when a window is restored?

Is there any event that is raised when a window is restored in C#/.NET?

I noticed that there is an event that is raised when a window is activated, but I can't find a corresponding event for a window being restored, such as from a maximized or minimized state.

If you don't like using the form's WindowState property and don't want to have to keep around a flag indicating the form's previous state, you can achieve the same result at a slightly lower level.

You'll need to override your form's window procedure ( WndProc ) and listen for a WM_SYSCOMMAND message indicating SC_RESTORE . For example:

protected override void WndProc(ref Message m)
{
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_RESTORE = 0xF120;

    if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_RESTORE)
    {
        // Do whatever processing you need here, or raise an event
        // ...
        MessageBox.Show("Window was restored");
    }

    base.WndProc(ref m);
}

You can check it this way:

private void Form1_Resize(object sender, EventArgs e)
{
   if (this.WindowState == FormWindowState.Minimized)
   {
       ...
   }
   else if ....
   {
   }
}

Pretty unsure. You'd have to handle the SizeChanged event and detect if WindowState changed from Minimized to Normal or Maximized to Normal . Source

I know this question is quite old but it works this way:

public Form1()
{
    InitializeComponent();
    this.SizeChanged += new EventHandler(Form1_WindowStateTrigger);
}

private void Form1_WindowStateTrigger(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Minimized)
    {
        MessageBox.Show("FORM1 MINIMIZED!");
    }

    if (this.WindowState == FormWindowState.Normal)
    {
        MessageBox.Show("FORM1 RESTORED!");
    }

    if (this.WindowState == FormWindowState.Maximized)
    {
        MessageBox.Show("FORM1 MAXIMIZED!");
    }
}

Using the SizeChanged event, coupled with a check of WindowState does the trick here.

Check:

private void Form1_Activated(Object o, EventArgs e)
{
    if (this.WindowState == FormWindowState.Minimized) {
        ...
    }
}

The answer from Redhart is good. This is the same but slightly simpler:

public Form1()
{
    InitializeComponent();
    this.SizeChanged += Form1_SizeChanged;
}

#region Event-Handlers
void Form1_SizeChanged(object sender, EventArgs e)
{
    var state = this.WindowState;
    switch (state)
    {
        case FormWindowState.Maximized:
            ClearMyView();
            DisplayMyStuff();
            break;
        case FormWindowState.Minimized:
            break;
        case FormWindowState.Normal:
            ClearMyView();
            DisplayMyStuff();
            break;
        default:
            break;
    }
}
#endregion Event-Handlers

It's easy enough to add:

public partial class Form1 : Form {
    private FormWindowState mLastState;
    public Form1() {
      InitializeComponent();
      mLastState = this.WindowState;
    }
    protected override void OnClientSizeChanged(EventArgs e) {
      if (this.WindowState != mLastState) {
        mLastState = this.WindowState;
        OnWindowStateChanged(e);
      }
      base.OnClientSizeChanged(e);
    }
    protected void OnWindowStateChanged(EventArgs e) {
      // Do your stuff
    }

go to this link winforms-windowstate-changed-how-to-detect-this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM