[英]How to get proper bounds for a C# Windows Forms Panel to draw a border
[英]How to draw border for panel which is continously resizing c#
使用面板作为选择指示器。 面板根据MouseMove事件上的光标位置更改其大小。 但是,当我按如下所示绘制边框时,以前的边框会在面板上留下标记,并且同一面板内显示的边框太多。 甚至在每次抽奖之前都尝试过refresh(),但这会使它出现故障和缓慢
private void panel1_Paint(object sender, PaintEventArgs e)
{
this.panel1.Refresh();
ControlPaint.DrawBorder(e.Graphics, this.panel1.ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Solid);
}
private void drawboard_MouseMove(object sender, MouseEventArgs e)
{
panel1.Width = e.Location.X - panel1.Left;
panel1.Height = e.Location.Y - panel1.Top;
}
首先,永远不要在控件绘画处理程序中调用影响控件绘画的方法,例如Invalidate
或Refresh
。
您可以在修改面板大小后通过调用Invalidate
或Refresh
来解决原始问题。 请注意,最好只调用一次来设置Size
属性,而不要分别设置Width
和Height
:
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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.