C#Winform如何更改表单非客户区域大小?

问题描述 投票:1回答:1

我已经实现了对非客户区域大小的更改,但遇到了问题。

错误:每当我最大化窗口并再次恢复它时,它的宽度和高度都会增加。

这是我的代码:

private void WmNcCalcSize(ref Message m)
{
    if (m.WParam != IntPtr.Zero)
    {
        Win32.NCCALCSIZE_PARAMS rcsize = (Win32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(Win32.NCCALCSIZE_PARAMS));
        AdjustClientRect(ref rcsize.rcNewWindow);
        Marshal.StructureToPtr(rcsize, m.LParam, false);
    }
    else
    {
        Win32.RECT rcsize = (Win32.RECT)Marshal.PtrToStructure(m.LParam, typeof(Win32.RECT));
        AdjustClientRect(ref rcsize);
        Marshal.StructureToPtr(rcsize, m.LParam, false);
    }

    m.Result = (IntPtr)1;
}

private void AdjustClientRect(ref Win32.RECT rcClient)
{
    if (this.WindowState == FormWindowState.Maximized)
    {
        rcClient.top += this.CaptionHeight + Math.Abs(this.Top) - 1;
        rcClient.left = 0;

        rcClient.right = Screen.PrimaryScreen.Bounds.Right;
        rcClient.bottom -= 1;
    }
    else
    {
        rcClient.top += this.CaptionHeight;
        rcClient.left += 1;
        rcClient.right -= 1;
        rcClient.bottom -= 1;
    }
}
c# windows winforms winapi
1个回答
0
投票

我最终试图拦截WM_WINDOWPOSCHANGED消息来控制它:

    private void WmWindowPosChanged(ref Message m)
    {
        base.WndProc(ref m);

        curWindowState = this.WindowState;

        if (curWindowState == FormWindowState.Maximized || curWindowState == FormWindowState.Minimized
            && preWindowState == FormWindowState.Normal)
        {
            restoreRectangle = this.RestoreBounds;
        }

        if (curWindowState == FormWindowState.Normal
            && preWindowState == FormWindowState.Maximized || curWindowState == FormWindowState.Minimized)
        {
            if (restoreRectangle != Rectangle.Empty)
                this.Size = restoreRectangle.Size;

            restoreRectangle = Rectangle.Empty;
        }

        preWindowState = curWindowState;
    }
© www.soinside.com 2019 - 2024. All rights reserved.