Friday, November 2, 2012

Winforms/C# - Overriding default minimize and maximize form events

Overriding the default system behavior is simple. Just need to capture machine events and modify them as we wish.


//overrides minimize and maximize default behavior
        private const int WM_SYSCOMMAND = 0x0112;
        private const int SC_MINIMIZE = 0xf020;
        private const int SC_MAXIMIZE = 0xf030;

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SYSCOMMAND)
            {
                if (m.WParam.ToInt32() == SC_MINIMIZE)
                {
                    m.Result = IntPtr.Zero;
                    //my code for the minimize event
                    return;
                }
                else if (m.WParam.ToInt32() == SC_MAXIMIZE)
                {
                    //my code for the maximize event
                    return;
                }
            }
            base.WndProc(ref m);


Cheers!

No comments:

Post a Comment