WS_SYSMENU 阻止窗口调整大小

时间:2021-05-11 11:48:40

标签: c# winforms

当我使用这些样式(并将 FormBorderStyle 设置为 None)创建无边框窗口时,我可以在 WM_NCHITTEST 中调整窗口大小:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams p = base.CreateParams;
        p.Style = WS_MAXIMIZEBOX | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE; // This works
        p.ExStyle = WS_EX_RIGHTSCROLLBAR | WS_EX_LEFT | WS_EX_CONTROLPARENT | WS_EX_APPWINDOW;
        return p;
    }
}

但是一旦我在窗口样式中包含 WS_SYSMENU,调整大小就不再起作用。

protected override CreateParams CreateParams
{
    get
    {
        CreateParams p = base.CreateParams;
        p.Style = WS_MAXIMIZEBOX | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE | WS_SYSMENU; // This doesn't :(
        p.ExStyle = WS_EX_RIGHTSCROLLBAR | WS_EX_LEFT | WS_EX_CONTROLPARENT | WS_EX_APPWINDOW;
        return p;
    }
}

哦,这就是我的 WndProc 的样子:

const int _ = 4;
const int _t = 24;

Rectangle RectTop { get { return new Rectangle(0, 0, ClientSize.Width, _); } }
Rectangle RectLeft { get { return new Rectangle(0, 0, _, ClientSize.Height); } }
Rectangle RectBottom { get { return new Rectangle(0, ClientSize.Height - _, ClientSize.Width, _); } }
Rectangle RectRight { get { return new Rectangle(ClientSize.Width - _, 0, _, ClientSize.Height); } }

Rectangle RectTitleBar { get { return new Rectangle(_, _, Width - 2 * _, _t); } }
Rectangle RectIcon { get { return new Rectangle(_ + (_t / 2 - 8), _ + (_t / 2 - 8), 16, 16); } }

Rectangle RectTopLeft { get { return new Rectangle(0, 0, _, _); } }
Rectangle RectTopRight { get { return new Rectangle(ClientSize.Width - _, 0, _, _); } }
Rectangle RectBottomLeft { get { return new Rectangle(0, ClientSize.Height - _, _, _); } }
Rectangle RectBottomRight { get { return new Rectangle(ClientSize.Width - _, ClientSize.Height - _, _, _); } }

protected override void WndProc(ref Message message)
{
    base.WndProc(ref message);

    if (message.Msg == 0x84) // WM_NCHITTEST
    {
        var cursor = PointToClient(Cursor.Position);

        if (CustomFormBorderStyle == FormBorderStyle.Sizable || CustomFormBorderStyle == FormBorderStyle.SizableToolWindow)
        {
            if (RectTopLeft.Contains(cursor)) message.Result = (IntPtr)HTTOPLEFT;
            else if (RectTopRight.Contains(cursor)) message.Result = (IntPtr)HTTOPRIGHT;
            else if (RectBottomLeft.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMLEFT;
            else if (RectBottomRight.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMRIGHT;

            else if (RectTop.Contains(cursor)) message.Result = (IntPtr)HTTOP;
            else if (RectLeft.Contains(cursor)) message.Result = (IntPtr)HTLEFT;
            else if (RectRight.Contains(cursor)) message.Result = (IntPtr)HTRIGHT;
            else if (RectBottom.Contains(cursor)) message.Result = (IntPtr)HTBOTTOM;
        }

        if (RectTitleBar.Contains(cursor) && !RectIcon.Contains(cursor)) message.Result = (IntPtr)HTCAPTION;
    }
}

这是为什么?有没有办法让无边框窗口有系统菜单,同时可以调整大小。

0 个答案:

没有答案