2017-03-06 34 views
4

我需要幫助解決一個我不知道如何解決的問題。我有我自己的課程,從PictureBox延伸,我可以正確顯示我的矩形,我可以移動它沒有問題。如何在繪製時控制圓的大小和位置

但是,當我嘗試繪製一個圓形的矩形周圍,我有一個問題。我想在用戶製作mouseclickevent時繪製圓。它繪製一個圓圈,但在矩形上。我不知道你是否理解我......我把我的代碼放在下面,還有我的結果和我想要的結果。

我的代碼:

OnClick事件:

public void Colocar(Control control, Unidad unidad, Point p) 
    { 
     unidad.Location = p; 
     control.Controls.Add(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); 
      } 
     } 
    } 

    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

//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。右邊的矩形是沒有圓圈的原始矩形的圖片框。 enter image description here

我想得到的結果: 將圓圈畫在矩形的周圍。

的尤里·

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。你可以幫我嗎?謝謝! – 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

不,我在編輯的問題上解釋得更好。我想在用戶製作mouseclickevent時繪製一個圓圈,並且此事件位於擴展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

找到它不,我在編輯的問題上解釋得更好。我想在用戶製作mouseclickevent時繪製一個圓圈,並且此事件位於擴展pictureBox的myUnidad.cs類中。所以我不能用你的答案解決,因爲它將圓圈繪製到pictureBox中,不是嗎? – Imrik