C#禁用WncProc上的调整大小自定义控件
对于form1,当我按住左下角调整大小时,它的大小限制为MinummSize(300300)。 但尽管设置MinimumSize=new Size(50,50),MyControl的宽度和高度仍然可以小于50。 你知道如何制作类似MyControl的表单吗?提前感谢C#禁用WncProc上的调整大小自定义控件,c#,resize,custom-controls,wndproc,C#,Resize,Custom Controls,Wndproc,对于form1,当我按住左下角调整大小时,它的大小限制为MinummSize(300300)。 但尽管设置MinimumSize=new Size(50,50),MyControl的宽度和高度仍然可以小于50。 你知道如何制作类似MyControl的表单吗?提前感谢 public partial class Form1 : Form { public Form1() { InitializeComponent(); FormBorderStyle
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);
}
}
ohm,我找到了WM_尺寸,这是在调整尺寸之前发生的
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);
}
}
公共结构
{
公共int左;
公共int Top;
公共权利;
公共int底部;
公共整数宽度{get=>Right-Left;set=>Right=value+Left;}
公共整数高度{get=>Bottom-Top;set=>Bottom=value+Top;}
公共静态_rectfrompointer(IntPtr ptr)
{
return(_RECT)Marshal.ptr结构(ptr,typeof(_RECT));
}
公共网络(IntPtr ptr)
{
Marshal.StructureToPtr(this,ptr,true);
}
}
公共类MyControl:Control
{
公共MyControl()
{
最小尺寸=新系统图纸尺寸(50,50);
}
受保护的覆盖无效WndProc(参考消息m)
{
如果(m.Msg==0x84)//WM\u nchitest
{
System.Drawing.Point pos=新的System.Drawing.Point(m.LParam.ToInt32());
pos=此点到客户端(pos);
如果(pos.X>=this.ClientSize.Width-6和&pos.Y>=this.ClientSize.Height-6)
{
m、 结果=(IntPtr)17;
返回;
}
}
如果(m.Msg==0x214)//WM\u
{
_RECT rec=_RECT.FromPointer(m.LParam);
if(记录宽度<最小尺寸宽度)
记录宽度=最小尺寸宽度;
if(记录高度<最小尺寸高度)
记录高度=最小尺寸高度;
记录ToPointer(m.LParam);
返回;
}
基准WndProc(参考m);
}
}