Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# base.OnPaint(e)参数异常_C#_Class_Drawing_Base_Argumentexception - Fatal编程技术网

C# base.OnPaint(e)参数异常

C# base.OnPaint(e)参数异常,c#,class,drawing,base,argumentexception,C#,Class,Drawing,Base,Argumentexception,我有自己的类扩展了PictureBox。当我在它周围画一个圆圈时,我有一个问题。问题出在“base.OnPaint(e)”上,它抛出了一个ArgumentException。我把代码放在下面,然后是完整的类 引发异常的方法: private void Parent_Paint(object sender, PaintEventArgs e) { if (clickPerformed) { using (Graphics g = e.

我有自己的类扩展了PictureBox。当我在它周围画一个圆圈时,我有一个问题。问题出在“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的第行“Application.Run(new Form1());”上给出了“System.Drawing.dll中的System.ArgumentException”


有人能帮我吗?谢谢

我用注释帮助解决它。我只需要删除图形上的using语句


谢谢!:)

为什么在绘制自己之后调用
base.OnPaint()
?这将再次对图形进行过度绘制。我猜发生异常是因为在调用
base.OnPaint()
之前,在事件参数中处理
Graphics
实例。使用处置
Grahics
实例的
语句删除
base.OnPaint()
通常用于
override OnPaint
using(Graphics g=e.Graphics)
您没有创建它,因此不能处置它!它解决了问题!谢谢大家,我只需要删除using语句,它就可以工作了。但是现在我有另一个问题,当我点击我的一个图片框时,它正确地画出了圆圈,但它在表单的左上角又画了一个图片框。你知道为什么要这么做吗?在此之前,我可以点击图片框,它会显示一个消息框,但它不会画任何“额外”的东西。也许你应该在你的问题帖子中包含使用语句。因此答案/解决方案变得更加明显。