Window Minimize / Maximize Event (C# winforms)

If you are working in C# winforms and need to respond to window minimize and maximize events, it seems that winforms does not provide native event handlers. Sucha feat is still very much possible through hooking into the Windows API.

The exact message you need to catch is WM_SYSCOMMAND and look for the SC_* messages:

  • SC_CLOSE 0xF060
  • SC_MAXIMIZE 0xF030
  • SC_MINIMIZE 0xF020
  • SC_RESTORE 0xF120

There are indeed more messages. Check the above link for a full list. Now for some sample code:

protected override void WndProc(ref Message m)
    base.WndProc(ref m);
    if (m.Msg == 0x0112) // WM_SYSCOMMAND
        if (m.WParam == new IntPtr(0xF030) || m.WParam == new IntPtr(0xF020))
            // Event fired! Handle it here
comments powered by Disqus