首先,你应该调用影响像Invalidate
或Refresh
控制的油漆处理程序中的方法控制油漆。
修改面板的大小后,您可以通过调用Invalidate
或Refresh
来解决原始问题。请注意,这是更好地与单独一个电话,而不是Width
和Height
设置Size
属性:
private void panel1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, this.panel1.ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Solid);
}
private void drawboard_MouseMove(object sender, MouseEventArgs e)
{
var size = new Size(Math.Max(e.Location.X - panel1.Left, 0),
Math.Max(e.Location.Y - panel1.Top, 0));
if (panel1.Size != size)
{
panel1.Size = size;
panel1.Invalidate();
}
}
一个更好的选择是选择面板的ResizeRedraw属性设置为true
。由于它属于protected
属性,因此您需要创建并使用您自己的Panel
子类。作为奖励,你还可以设置DoubleBuffered属性true
,避免闪烁,而且里面移动绘图代码:
class SelectionBox : Panel
{
public SelectionBox()
{
ResizeRedraw = true;
DoubleBuffered = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Solid);
}
}
让您panel1
是SelectionBox
,取出Paint
事件处理程序,然后将鼠标移动处理程序可能很简单
private void drawboard_MouseMove(object sender, MouseEventArgs e)
{
panel1.Size = new Size(Math.Max(e.Location.X - panel1.Left, 0),
Math.Max(e.Location.Y - panel1.Top, 0));
}
尝试在面板调整大小之前使用背景颜色绘制边框,然后调整面板大小并让面板使用实际颜色绘制边框。事实上,你根本不需要边框来绘制矩形。 –
面板是实际的选择工具指示器,被选中的对象是按钮,在画板上简单地画一个矩形给出一个不相互作用的感觉 – phpnet