【发布时间】:2016-10-09 21:38:28
【问题描述】:
我想向PictureBox、Label、Panel 添加一些扩展,例如移动、调整大小、...,如下所示:
public class LiveControl: PictureBox
{
private Point cur = new Point(0, 0);
public LiveControl()
{
ResizeRedraw = true;
MouseDown += (s, e) => { cur = new Point(e.X, e.Y); };
MouseMove += (s, e) => {
if (e.Button == MouseButtons.Left)
{
Control x = (Control)s;
x.SuspendLayout();
x.Location = new Point(x.Left + e.X - cur.X, x.Top + e.Y - cur.Y);
x.ResumeLayout();
}
};
}
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);
}
}
private const int grab = 16;
}
有没有我把它写成一个类并为所有它们继承它,或者我应该像我为PictureBox写的那样写3个单独的类?
【问题讨论】:
-
我通常会编写一个控制器类,控件可以注册到该控制器类以获取这些服务。见here for an example of a Drag&Drop controller!
-
@HansPassant 我没明白你的意思
-
我从别处复制了其中的一部分,我不需要浪费时间写已经写的东西,我也不打算要求代码都是我的,这是举个例子,代码要大得多。
-
您可能有兴趣
FrameControl。