I need help with a problem that I don’t know how to solve. I have my own class that extends from the PictureBox, I can correctly show my rectangle, and I can move it without any problems.
But when I try to draw a circle around the rectangle, I have a problem. I want to draw a circle when users make a mouseclickevent. He draws a circle, but on a rectangle. I don’t know if you understand me ... I put my code below, as well as the results that I have and the results that I want to have.
My code is:
OnClick Event:
bool unidadPulsada = false;
private Point MouseDownLocation;
protected override void OnMouseDown(MouseEventArgs e)
{
unidadPulsada = true;
base.OnMouseDown(e);
MouseDownLocation = e.Location;
DibujarLimites();
}
Method that sets pictureBox:
public void Colocar(Control control, Unidad unidad, Point p)
{
unidad.Location = p;
control.Controls.Add(unidad);
}
Methods that draw:
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);
}
}
}
public void DrawCircle(Graphics g, Pen pen, float centerX, float centerY, float radius)
{
g.DrawEllipse(pen, centerX - radius, centerY - radius,
radius + radius, radius + radius);
}
, :
pictureBox . pictureBox , .

, :
.

EDIT Youri:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using WHF.Properties;
namespace WHF
{
public class Unidad : PictureBox
{
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;
}
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; }
public void Colocar(Control control, Unidad unidad, Point p)
{
unidad.Location = p;
control.Controls.Add(unidad);
}
bool unidadPulsada = false;
private Point MouseDownLocation;
protected override void OnMouseDown(MouseEventArgs e)
{
unidadPulsada = true;
base.OnMouseDown(e);
MouseDownLocation = e.Location;
float x = Location.X + e.X;
float y = Location.Y + e.Y;
Graphics graphics = CreateGraphics();
PointF center = new PointF(x, y);
float radius = 100;
PointF rectOrigin = new PointF(center.X - radius, center.Y - radius);
RectangleF r = new RectangleF(rectOrigin, new SizeF(radius * 2F, radius * 2F));
using (Pen p = new Pen(Color.Red, 4))
{
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
graphics.DrawEllipse(p, r);
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (unidadPulsada)
{
Left = e.X + Left - MouseDownLocation.X;
Top = e.Y + Top - MouseDownLocation.Y;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
unidadPulsada = false;
base.OnMouseDown(e);
LimpiarLimites();
}
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);
}
}
}
public void LimpiarLimites()
{
}
public void DrawCircle(Graphics g, Pen pen, float centerX, float centerY, float radius)
{
g.DrawEllipse(pen, centerX - radius, centerY - radius, radius + radius, radius + radius);
}
public void FillCircle(Graphics g, Brush brush, float centerX, float centerY, float radius)
{
g.FillEllipse(brush, centerX - radius, centerY - radius, radius + radius, radius + radius);
}
}
}