2017-03-07 4 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)" の行を削除すると、それは「私に与えますSystem.ArgumentException 'System.Drawing.dll内の "Program.cs、行:" Application.Run(新しいForm1()); "

誰でも手伝ってもらえますか?ありがとう!

+2

なぜあなたはあなた自身を描いた 'base.OnPaint()' _after_を呼び出しますか?これで図面が上書きされます。そして、 'base.OnPaint()'を呼び出す前にイベント引数に 'Graphics'インスタンスを' Dispose'するので例外が発生したと思います。 'Grahics'インスタンスを破棄する' using'ステートメントを削除してください。 –

+0

'base.OnPaint()'は通常 'override OnPaint'で使われます。 –

+0

'(グラフィックスg = e.Graphics)を使用しています。 'あなたはそれを作成していないので、あなたはそれを処分してはいけません! – TaW

答えて

0

コメントのヘルプで解決します。 Graphics上でusingステートメントを削除するだけです。

ありがとうございます! :)

+0

質問文にusingステートメントを含める必要があるかもしれません。答え/解決策がもう少し明らかになります。 –

関連する問題