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 클래스 : 나는 "base.OnPaint (예)"라인을 제거하면
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);
}
}
}
}
, 그것은 나를주는 " System.ArgumentException 'System.Drawing.dll있는 "줄에 Program.cs :"Application.Run (새 Form1()); "
아무도 도와 줄 수 있습니까? 감사!
왜 당신이'base.OnPaint()'_ _를 그린 것인가? 이것은 도면을 다시 덮어 씁니다. 그리고 당신은'base.OnPaint()'를 호출하기 전에 이벤트 인자에'Graphics' 인스턴스를'Dispose'하기 때문에 예외가 발생했다고 생각합니다. Grahics 인스턴스를 처리하는'using' 문을 제거하십시오. –
'base.OnPaint()'는 보통'override OnPaint'에서 사용됩니다. –
'(그래픽 g = e.Graphics를 사용하여)'당신이 그것을 만들지 않았으므로 당신은 그것을 처분하지 않을 것입니다! – TaW