微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带无边框样式的可调整大小的表单和带停靠栏填充的子项

如何解决带无边框样式的可调整大小的表单和带停靠栏填充的子项

我的目标是创建像普通 Win10 窗口一样可调整大小但没有标题栏的窗口(使用 WindowsForms),因此我可以自己绘制它(如 UWP 应用程序)。

解决方法

通过修改这段代码,我终于找到了一个非常好的工作anwser: https://stackoverflow.com/a/29788300/15213858

我现在拥有的:

public class MyForm : Form
{
    //Window Messages
    public const uint WM_NCPAINT = 0x85;
    public const uint WM_NCCALCSIZE = 0x83;
    public const uint WM_NCHITTEST = 0x84;

    //RECT Structure
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int left,top,right,bottom;
    }

    //WINDOWPOS Structure
    [StructLayout(LayoutKind.Sequential)]
    public struct WINDOWPOS
    {
        public IntPtr hwnd;
        public IntPtr hwndinsertafter;
        public int x,y,cx,cy;
        public int flags;
    }

    //NCCALCSIZE_PARAMS Structure
    [StructLayout(LayoutKind.Sequential)]
    public struct NCCALCSIZE_PARAMS
    {
        public RECT rgrc0,rgrc1,rgrc2;
        public WINDOWPOS lppos;
    }

    //Window Procedure Hook
    protected override void WndProc(ref Message m)
    {
        //Don't style window in designer...
        if (DesignMode)
            base.WndProc(ref m);

        //Handle Message
        switch ((uint)m.Msg)
        {
            case WM_NCCALCSIZE: WmNCCalcSize(ref m); break;
            default: base.WndProc(ref m); break;
        }
    }

    //WM_NCCALCSIZE
    private void WmNCCalcSize(ref Message m)
    {

        //Check WPARAM
        if (m.WParam != IntPtr.Zero)    //TRUE
        {
            //When TRUE,LPARAM Points to a NCCALCSIZE_PARAMS structure
            var nccsp = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam,typeof(NCCALCSIZE_PARAMS));

            //We're adjusting the size of the client area here. Right now,the client area is the whole form.
            //Adding to the Top,Bottom,Left,and Right will size the client area.
            nccsp.rgrc0.top += 0;      //30-pixel top border
            nccsp.rgrc0.bottom -= 8;    //4-pixel bottom (resize) border
            nccsp.rgrc0.left += 8;      //4-pixel left (resize) border
            nccsp.rgrc0.right -= 8;     //4-pixel right (resize) border

            //Set the structure back into memory
            Marshal.StructureToPtr(nccsp,m.LParam,true);
        }
        else    //FALSE
        {
            //When FALSE,LPARAM Points to a RECT structure
            var clnRect = (RECT)Marshal.PtrToStructure(m.LParam,typeof(RECT));

            //Like before,we're adjusting the rectangle...
            //Adding to the Top,and Right will size the client area.
            clnRect.top += 0;      //30-pixel top border
            clnRect.bottom -= 8;    //4-pixel bottom (resize) border
            clnRect.left += 8;      //4-pixel left (resize) border
            clnRect.right -= 8;     //4-pixel right (resize) border

            //Set the structure back into memory
            Marshal.StructureToPtr(clnRect,true);
        }

        //Return Zero
        m.Result = IntPtr.Zero;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?