繁体   English   中英

C#Winform如何更改窗体的非客户区大小?

[英]C# Winform How to change form nonclient area size?

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

点击这里查看demo.jpg

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

这是我的代码:

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

我最终尝试拦截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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM