【发布时间】:2017-09-30 02:45:55
【问题描述】:
我目前正在尝试将可调整大小的面板添加到我的 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()吗?它是一种虚拟方法,您可以在像您这样的情况下使用它。
标签: c# winforms panel aspect-ratio resizable