如何为面板绘制边框,不断调整c#

时间:2016-03-08 06:58:22

标签: c# border panel

使用面板作为选择指示器。面板根据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;        
}

1 个答案:

答案 0 :(得分:1)

首先,你不应该在控件绘制处理程序中调用控制绘制来影响InvalidateRefresh等方法。

您可以在修改面板大小后调用InvalidateRefresh来解决原始问题。请注意,最好分别使用一个调用设置Size属性,而不是WidthHeight

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));
}