Search code examples
c#winformsresizewndproc

Custom WndProc doesn't stop resizing


I made a form that handles WM_CREATE, WM_ACTIVATE, WM_NCCALCSIZE and WM_NCHITTEST. It also overrides the paint method.

The problem is when I resize the form it doesn't stop resizing. I tried to compare the messages with a working window but spy++ keeps crashing. Here is my WndProc code:

protected override void WndProc(ref Message m)
{
    IntPtr result = IntPtr.Zero;

    bool callDWP = !Win32Interop.DwmDefWindowProc(m.HWnd, m.Msg, m.WParam, m.LParam, out result);

    switch (m.Msg)
    {
        case Win32Messages.WM_CREATE:
        {
            int style = Win32Interop.GetWindowLong(m.HWnd, Win32Constants.GWL_STYLE);
            int styleEx = Win32Interop.GetWindowLong(m.HWnd, Win32Constants.GWL_EXSTYLE);
            Win32Interop.AdjustWindowRectEx(out RECT rc, style, false, styleEx);

        }
        break;

        case Win32Messages.WM_ACTIVATE:
        {
            MARGINS margins = new MARGINS
            {
                cxLeftWidth = Math.Abs(BorderLeft),
                cxRightWidth = Math.Abs(BorderRight),
                cyBottomHeight = Math.Abs(BorderBottom),
                cyTopHeight = Math.Abs(BorderTop)
            };

            int hr = Win32Interop.DwmExtendFrameIntoClientArea(m.HWnd, ref margins);

            result = IntPtr.Zero;
        }
        break;

        case Win32Messages.WM_NCCALCSIZE:
        {
            if (m.WParam != IntPtr.Zero)
            {
                result = IntPtr.Zero;
                callDWP = false;
            }
        }
        break;

        case Win32Messages.WM_NCHITTEST:
        {
            {
                int ht = DoHitTest(m);
                Console.WriteLine(ht);
                if (callDWP)
                {
                    callDWP = (ht == Win32Constants.HTNOWHERE);
                    result = new IntPtr(ht);
                }

            }

            break;
        }
        default:
        {
            base.WndProc(ref m);
            break;
        }
    }
    m.Result = result;
    if (callDWP)
    {
        base.WndProc(ref m);
    }
}

Solution

  • Don't call base.WndProc(ref m); in default