2017-03-06 11 views
4

문제를 해결하는 방법을 모르겠다는 도움이 필요합니다. PictureBox에서 확장 한 클래스가 있습니다. 사각형을 올바르게 표시 할 수 있으며 문제없이 이동할 수 있습니다.원을 그릴 때 원의 크기와 위치를 조절하는 방법

그러나 사각형을 원으로 그리려고 할 때 문제가 생깁니다. 사용자가 마우스 커서를 틀면 서클을 그립니다. 원을 그리지 만, 직사각형에. 나는 당신이 나를 이해하는지 모르겠다. 나는 아래에 나의 코드와 내가 가지고있는 결과와 내가 갖고 싶은 결과를 넣었다.

내 코드 :

의 OnClick 이벤트 : 내가 가진 결과를

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); 
    } 

: 그릴

public void Colocar(Control control, Unidad unidad, Point p) 
    { 
     unidad.Location = p; 
     control.Controls.Add(unidad); 
    } 

방법 : PictureBox를 설정

//Métodos para mover la unidad 
    bool unidadPulsada = false; 
    private Point MouseDownLocation; 
    protected override void OnMouseDown(MouseEventArgs e) 
    { 
     unidadPulsada = true; 
     base.OnMouseDown(e); 
     MouseDownLocation = e.Location; 
     DibujarLimites(); 
    } 

방법 : 왼쪽 사각형은 원 그리기가있는 pictureBox입니다. 오른쪽 직사각형은 원이없는 pictureBox, 원래 직사각형입니다. enter image description here

내가 갖고 싶은 결과 : 원은 사각형을 그립니다. 의 Youri에 대한

enter image description here

편집 :

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 
{ 
    //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); 
    } 


    //Métodos para mover la unidad 
    bool unidadPulsada = false; 
    private Point MouseDownLocation; 
    protected override void OnMouseDown(MouseEventArgs e) 
    { 
     unidadPulsada = true; 
     base.OnMouseDown(e); 
     MouseDownLocation = e.Location; 
     //DibujarLimites(); 

     float x = Location.X + e.X; 
     float y = Location.Y + e.Y; 

     Graphics graphics = CreateGraphics(); 

     PointF center = new PointF(x, y);//this.ClientSize.Width/2F, this.ClientSize.Height/2F); 
     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(); 
    } 

    //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); 
      } 
     } 
    } 

    //Método para limpiar el dibujo de la zona límite 
    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); 
    } 
} 
} 
+0

참조 [이] (http://stackoverflow.com/questions/42274719/c-sharp-draw-circle-around-label/42275309#42275309). – Berkay

+0

원을 'PicureBox' 또는 그 주위에 그려야합니까? –

+0

주위에 pictureBox와 함께 움직이지 않습니다. 나는 그것이 pictureBox의 움직임의 한계 영역을보고 싶습니다. – Imrik

답변

1

괜찮 예입니다, 당신이 그랬던 것처럼, 난 내 자신의 그림 상자를 만든 같은 방식으로,

class MyPBox : PictureBox 
    { 
    public MyPBox() 
    { 
     this.BackColor = Color.Red; // for see better 
     this.Location = new Point(50, 50); // set location at form 

    } 
    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 
    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 

    } 

    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); 
    } 

} 

결과를 (내가 적용하려 picturebox btw :)를 클릭하십시오); 희망이 도움이

enter image description here

,

+0

완벽 해 보인다! 나는 시험해 볼 것이다, 감사!! :) – Imrik

+0

Parent_Paint 메서드에서 "base.OnPaint (e);"줄에서 System.ArgumentException을 throw합니다. 너 나 좀 도와 줄 수있어? 감사! – Imrik

+0

그냥 제거하고 다시 시도하십시오. @Imrik – Berkay

0

은 PictureBox를 당신의 onclick 이벤트에 넣고 당신은

float x = Location.X + e.X; 
float y = Location.Y + e.Y; 

Form form = (Form)this.FindForm(); 
Graphics graphics = form.CreateGraphics(); 

PointF center = new PointF(x, y);//this.ClientSize.Width/2F, this.ClientSize.Height/2F); 
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.Blue, 4)) 
{ 
    p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; 
    graphics.DrawEllipse(p, r); 
} 
+0

아니요, 편집 된 질문에서 더 잘 설명합니다. 사용자가 마우스 커서를 돌릴 때 원을 그려야하고이 이벤트는 pictureBox를 확장하는 myUnidad.cs 클래스에 있습니다. 그래서 당신의 대답으로 해결할 수없는 것은 pictureBox에 원을 그리기 때문입니다. – Imrik

+0

내 편집을보세요. 당신을 위해 트릭을해야합니다. @Imrik –

+0

시도해 보았지만 아무 것도 볼 수 없습니다. 제 말은, 그것이 원을 그리지 않는다고 생각합니다. 그렇지 않으면 볼 수 없습니다. 너 나 좀 도와 줄 수있어? – Imrik

0

0,0 왼쪽 상단 코너 괜찮을 것 picturebox의. 당신이 원하는 것은 여기

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, this.height/2, this.width/2, 20); 
     } 
    } 
} 
+0

아니요, 편집 된 질문에서 더 잘 설명합니다. 사용자가 마우스 커서를 돌릴 때 원을 그려야하고이 이벤트는 pictureBox를 확장하는 myUnidad.cs 클래스에 있습니다. 그래서 당신의 대답으로 해결할 수없는 것은 pictureBox에 원을 그리기 때문입니다. – Imrik