9

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Hali
  • 591
  • 2
  • 5
  • 20

7 Answers7

17

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);
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Have in mind that this way could stop working on future windows versions and of course won't work on Mono and maybe CE – Ignacio Soler Garcia Jan 05 '11 at 08:49
  • @SoMoS: There is absolutely no reason to believe that this would this stop working on a future Windows version. Unless the entire windowing framework is rewritten, this will work just fine. And in the event that happens, you will have way more compatibility problems than this. The .NET Framework P/Invokes most of this stuff under the covers anyway. It should work just fine on Windows CE; I don't know about Mono but the question didn't indicate a need for such support. – Cody Gray - on strike Jan 05 '11 at 09:33
  • 1
    I just wanted to note that going so low is a bit against the .Net philosophy because you're adding a dependence over the SO that you could avoid. And you can be sure that Linux (and Mono) does not support any PInvoke (and CE does not have a complete support). – Ignacio Soler Garcia Jan 05 '11 at 10:26
3

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

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
3

You can check it this way:

private void Form1_Resize(object sender, EventArgs e)
{
   if (this.WindowState == FormWindowState.Minimized)
   {
       ...
   }
   else if ....
   {
   }
}
Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • checking for `if (this.WindowState == FormWindowState.Maximized) { ... }` in the `Resize` event is what you are looking for. – Petrus Theron Jan 04 '11 at 15:06
  • 8
    That won't actually tell you if the state has *changed*, only what it is at the time of resizing. – Steven P Feb 27 '12 at 05:30
2

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.

Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
dystopiandev
  • 134
  • 1
  • 6
  • 1
    You don't consider the simple resize hier! So you need to store the last state somewhere an evaluate it as usr-local-ΕΨΗΕΛΩΝ offers! – Rekshino May 19 '17 at 09:24
0

Check:

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

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
Jan Heldal
  • 148
  • 6
-2

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?

fasadat
  • 1,025
  • 1
  • 12
  • 27