自定义 WndProc 不会停止调整大小

Custom WndProc doesn't stop resizing

我制作了一个处理 WM_CREATE、WM_ACTIVATE、WM_NCCALCSIZE 和 WM_NCHITTEST 的表单。它还覆盖了 paint 方法。

问题是当我调整表单大小时它不会停止调整。我试图将消息与正常工作的 window 进行比较,但 spy++ 一直崩溃。 这是我的 WndProc 代码:

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);
    }
}

默认不调用base.WndProc(ref m);