2017-03-07 85 views
-1

我有我自己的类扩展PictureBox。当我围绕它画一个圆时,我遇到了一个问题。问题出在“base.OnPaint(e)”上,它抛出一个ArgumentException。我把代码放在下面,然后是完整的类。base.OnPaint(e)ArgumentException

抛出异常的方法:

private void Parent_Paint(object sender, PaintEventArgs e) 
    { 
     if (clickPerformed) 
     { 
      using (Graphics g = e.Graphics) 
      { 
       using (Pen pen = new Pen(Color.Black, 2)) 
       { 
        float locationX = this.Location.X + this.Size.Width/2; 
        float locationY = this.Location.Y + this.Size.Height/2; 
        float radius = (this.Size.Height + this.Size.Width)/2; 

        float[] dashValues = { 5, 2, 15, 4 }; 
        pen.DashPattern = dashValues; 
        DrawCircle(g, pen, locationX, locationY, radius); // draw circle 
        clickPerformed = false; // process done so set it to false 
       } 
      } 
     } 
     base.OnPaint(e); 
    } 

的Unidad.cs类:

public class Unidad : PictureBox 
{ 
    //Constructor 
    public Unidad(string nombre, string tipo, int movimiento, int ha, int hp, int fuerza, int resistencia, int heridas, int iniciativa, int ataques, int liderazgo, int coste, string rutaImagen) 
    { 
     tipoUnidad = tipo; 
     movimientoUnidad = movimiento; 
     nombreUnidad = nombre; 
     costeUnidad = coste; 
     haUnidad = ha; 
     hpUnidad = hp; 
     fuerzaUnidad = fuerza; 
     resistenciaUnidad = resistencia; 
     iniciativaUnidad = iniciativa; 
     ataquesUnidad = ataques; 
     liderazgoUnidad = liderazgo; 
     rutaImagenUnidad = rutaImagen; 
    } 

    //Propiedades 
    public string nombreUnidad { get; set; } 
    public string tipoUnidad { get; set; } 
    public int movimientoUnidad { get; set; } 
    public int costeUnidad { get; set; } 
    public int haUnidad { get; set; } 
    public int hpUnidad { get; set; } 
    public int fuerzaUnidad { get; set; } 
    public int resistenciaUnidad { get; set; } 
    public int heridasUnidad { get; set; } 
    public int iniciativaUnidad { get; set; } 
    public int ataquesUnidad { get; set; } 
    public int liderazgoUnidad { get; set; } 
    public string rutaImagenUnidad { get; set; } 

    //Método para dibujar unidad 
    public void Colocar(Control control, Unidad unidad, Point p) 
    { 
     unidad.Location = p; 
     control.Controls.Add(unidad); 
    } 

    protected override void OnPaint(PaintEventArgs pe) 
    { 
     if (this.Parent != null) 
     { 
      this.Parent.Paint += Parent_Paint; // picturebox's paint means it added to parent so we need to trigger parent's paint event 
     } 
     base.OnPaint(pe); 

    } 
    bool clickPerformed = false; // to catch control has mouse down 
    private Point MouseDownLocation; 
    protected override void OnMouseDown(MouseEventArgs e) 
    { 
     base.OnMouseDown(e); 
     clickPerformed = true; // set mouse down 
     Control tempSender = this.Parent; // get sender 
     tempSender.Invalidate(); // invalidate to trigger paint event 
     MouseDownLocation = e.Location; 

    } 

    private void Parent_Paint(object sender, PaintEventArgs e) 
    { 
     if (clickPerformed) 
     { 
      using (Graphics g = e.Graphics) 
      { 
       using (Pen pen = new Pen(Color.Black, 2)) 
       { 
        float locationX = this.Location.X + this.Size.Width/2; 
        float locationY = this.Location.Y + this.Size.Height/2; 
        float radius = (this.Size.Height + this.Size.Width)/2; 

        float[] dashValues = { 5, 2, 15, 4 }; 
        pen.DashPattern = dashValues; 
        DrawCircle(g, pen, locationX, locationY, radius); // draw circle 
        clickPerformed = false; // process done so set it to false 
       } 
      } 
     } 
     base.OnPaint(e); 
    } 

    protected override void OnMouseUp(MouseEventArgs e) 
    { 
     this.Parent.Invalidate(); // mouse up circle should be erased, so invalidate again to trigger paint, but this time clickPerformed is false 
            // so it won't draw circle again 
     base.OnMouseDown(e); 
    } 
    public void DrawCircle(Graphics g, Pen pen, float centerX, float centerY, float radius) 
    { 
     g.DrawEllipse(pen, centerX - radius, centerY - radius, radius + radius, radius + radius); 
    } 

    protected override void OnMouseMove(MouseEventArgs e) 
    { 
     base.OnMouseMove(e); 
     if (clickPerformed) 
     { 
      Left = e.X + Left - MouseDownLocation.X; 
      Top = e.Y + Top - MouseDownLocation.Y; 
     } 
    } 


    //Método para dibujar la zona límite de movimiento de la unidad 
    public void DibujarLimites() 
    { 
     using (Graphics g = CreateGraphics()) 
     { 
      using (Pen pen = new Pen(Color.Red, 2)) 
      { 
       float[] dashValues = { 5, 2, 15, 4 }; 
       pen.DashPattern = dashValues; 
       DrawCircle(g, pen, 0, 0, 20); 
      } 
     } 
    } 
} 

当我删除了 “base.OnPaint(E)” 行,它给了我一个“ Program.cs中System.ArgumentException'在System.Drawing.dll中',在线:“Application.Run(new Form1());”

任何人都可以帮助我吗?谢谢!

+2

为什么你调用'base.OnPaint()'_after_你画自己?这会再次覆盖您的图纸。我猜这个异常是因为你在调用'base.OnPaint()''之前'在事件参数中'Dispose'' Graphics'实例。除去配置'Grahics'实例的'using'语句。 –

+0

'base.OnPaint()'通常用于'override OnPaint'。 –

+0

'使用(Graphics g = e.Graphics)'你没有创建它,所以你不应该处理它! – TaW

回答

0

我解决它的评论帮助。我只需要删除Graphics上的使用语句。

谢谢! :)

+0

可能你应该在你的问题文章中包含using语句。以便答案/解决方案变得更加明显。 –

相关问题