2016-03-08 45 views
0

将面板用作选择指示符。面板根据MouseMove事件上的光标位置更改其大小。但是,当我如下所示绘制边框时,以前的边框会在面板上留下它们的标记,并且会在同一面板中显示太多边框。甚至试图刷新()每次在绘制之前,但是这使得它出问题,慢如何为连续调整大小的面板绘制边框c#

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;   
} 
+0

尝试在面板调整大小之前使用背景颜色绘制边框,然后调整面板大小并让面板使用实际颜色绘制边框。事实上,你根本不需要边框来绘制矩形。 –

+0

面板是实际的选择工具指示器,被选中的对象是按钮,在画板上简单地画一个矩形给出一个不相互作用的感觉 – phpnet

回答

1

首先,你应该调用影响像InvalidateRefresh控制的油漆处理程序中的方法控制油漆。

修改面板的大小后,您可以通过调用InvalidateRefresh来解决原始问题。请注意,这是更好地与单独一个电话,而不是WidthHeight设置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); 
    } 
} 

让您panel1SelectionBox,取出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)); 
} 
+0

感谢您的解释! – phpnet