2017-03-07 36 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語句。以便答案/解決方案變得更加明顯。 –

相關問題