我正在尝试向我的C#winforms项目添加可调整大小的面板。限制可调整大小的面板(比例和最小/最大大小)
我目前使用此代码来获得我想要的东西:
using System;
using System.Drawing;
using System.Windows.Forms;
class ResizablePanel : Panel
{
private const int grab = 16;
public ResizablePanel()
{
this.ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var rc = new Rectangle(this.ClientSize.Width - grab, this.ClientSize.Height - grab, grab, grab);
ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x84)
{
var pos = this.PointToClient(new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16));
if (pos.X >= this.ClientSize.Width - grab && pos.Y >= this.ClientSize.Height - grab)
m.Result = new IntPtr(17);
}
}
}
其工作正常,但现在我想限制一些东西。
我不希望面板小于420x236。 我试图设置MinimumSize,但忽略了当我尝试调整大小。
我想保持宽高比为16:9。
我如何得到上面的代码?有没有办法做到这一点?
你有没有试过SetBoundsCore()?这是一种虚拟方法,您可以在像您这样的情况下使用它。 – Bahrom