C#禁用WncProc上的调整大小自定义控件

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

使用form1时,当我按住左下角进行调整时,将其限制为MinumumSize(300,300)。但是,尽管设置MinimumSize = new Size(50,50),但MyControl的Width和Height仍可以小于50。你知道如何使MyControl像Form吗?预先感谢!

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        FormBorderStyle = FormBorderStyle.None;
        MinimumSize = new Size(300, 300);
    }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x84)         //WM_NCHITTEST
        {
            Point pos = new Point(m.LParam.ToInt32());
            pos = this.PointToClient(pos);
            if (pos.X >= this.ClientSize.Width - 6 && pos.Y >= this.ClientSize.Height - 6)
            {
                m.Result = (IntPtr)17;
                return;
            }
        }
        base.WndProc(ref m);
    }
}
public class MyControl : Control
{
    public MyControl()
    {
        MinimumSize = new Size(50, 50);
    }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x84)         //WM_NCHITTEST
        {
            Point pos = new Point(m.LParam.ToInt32());
            pos = this.PointToClient(pos);
            if (pos.X >= this.ClientSize.Width - 6 && pos.Y >= this.ClientSize.Height - 6)
            {
                m.Result = (IntPtr)17;
                return;
            }
            return;
        }
        base.WndProc(ref m);
    }
}
c# resize custom-controls wndproc
1个回答
0
投票

欧姆,我发现WM_SIZING <0x214>,该事件在调整大小之前发生!

public struct _RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
    public int Width { get => Right - Left; set => Right = value + Left; }
    public int Height { get => Bottom - Top; set => Bottom = value + Top; }
    public static _RECT FromPointer(IntPtr ptr)
    {
        return (_RECT)Marshal.PtrToStructure(ptr, typeof(_RECT));
    }
    public void ToPointer(IntPtr ptr)
    {
        Marshal.StructureToPtr(this, ptr, true);
    }
}
public class MyControl : Control
{
    public MyControl()
    {
        MinimumSize = new System.Drawing.Size(50, 50);
    }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x84)         //WM_NCHITTEST
        {
            System.Drawing.Point pos = new System.Drawing.Point(m.LParam.ToInt32());
            pos = this.PointToClient(pos);
            if (pos.X >= this.ClientSize.Width - 6 && pos.Y >= this.ClientSize.Height - 6)
            {
                m.Result = (IntPtr)17;
                return;
            }
        }
        else if (m.Msg == 0x214)       //WM_SIZING
        {
            _RECT rec = _RECT.FromPointer(m.LParam);
            if (rec.Width < MinimumSize.Width)
                rec.Width = MinimumSize.Width;
            if (rec.Height < MinimumSize.Height)
                rec.Height = MinimumSize.Height;
            rec.ToPointer(m.LParam);
            return;
        }
        base.WndProc(ref m);
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.