N
N
Nem0_o2015-02-13 16:53:48
Programming
Nem0_o, 2015-02-13 16:53:48

How to make it so that after clicking the button, you can click the mouse in the PictureBox and a circle would be drawn there?

Windows Forms. In general, let's say that there is Button1 - add a circle, PictureBox, in which this circle is drawn and the MouseClick event, on which the circle should be drawn in the PictureBox.
How to make it so that after pressing the button (Add circle) you can click in the PictureBox'a and a circle would be drawn there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2015-02-13
@Nem0_o

Fixed size shapes can be drawn like this:

public partial class Form1 : Form
{

    private int BrushType = 1;

    public Form1()
    {
      InitializeComponent();

      // создаем PictureBox
      var pic = new PictureBox { Dock = DockStyle.Fill, BackColor = Color.White };
      pic.MouseClick += PictureBox_MouseClick;
      this.Controls.Add(pic);

      // создаем панель для кнопок
      var panel = new FlowLayoutPanel { Dock = DockStyle.Top, AutoSize = true };
      this.Controls.Add(panel);

      // кнопки
      var btn = new RadioButton 
      { 
        Text = "Круг", 
        Tag = 1, 
        Appearance = Appearance.Button, 
        Checked = true, 
        AutoSize = true 
      };
      btn.Click += Button_Click;
      panel.Controls.Add(btn);

      btn = new RadioButton
      {
        Text = "Не круг",
        Tag = 2,
        Appearance = Appearance.Button,
        AutoSize = true
      };
      btn.Click += Button_Click;
      panel.Controls.Add(btn);

      btn = new RadioButton
      {
        Text = "Квадратный круг",
        Tag = 3,
        Appearance = Appearance.Button,
        AutoSize = true
      };
      btn.Click += Button_Click;
      panel.Controls.Add(btn);
    }

    private void Button_Click(object sender, EventArgs e)
    {
      var btn = (RadioButton)sender;
      this.BrushType = (int)btn.Tag;
    }

    private void PictureBox_MouseClick(object sender, MouseEventArgs e)
    {
      // получаем ссылку на PictureBox
      var pic = (PictureBox)sender;
      // получаем Graphics из PictureBox
      var g = pic.CreateGraphics();

      if (this.BrushType == 2)
      {
        // рисуем не круглый квадрат
        g.DrawRectangle(Pens.Black, e.X, e.Y, 50, 50);
      }
      else if (this.BrushType == 3)
      {
        // рисуем квадратный круг
        Point[] points = new Point[6];
        int half = 50 / 2;
        int quart = 50 / 4;
        points[0] = new Point(e.X + quart, e.Y);
        points[1] = new Point(e.X + 50 - quart, e.Y);
        points[2] = new Point(e.X + 50, e.Y + half);
        points[3] = new Point(e.X + 50 - quart, e.Y + 50);
        points[4] = new Point(e.X + quart, e.Y + 50);
        points[5] = new Point(e.X, e.Y + half);
        g.DrawPolygon(Pens.Black, points);
      }
      else
      {
        // рисуем эллипс
        g.DrawEllipse(Pens.Black, e.X, e.Y, 50, 50);
      }
    }

}

RadioButton are used as buttons , because it's more convenient.
In order not to be erased, you can make a Bitmap :
var pic = (PictureBox)sender;
if (pic.Image == null) { pic.Image = new Bitmap(pic.Width, pic.Height); }
var bmp = new Bitmap(pic.Image);
var g = Graphics.FromImage(bmp);

And after the drawing is completed, pass the picture to the PictureBox :
Result:
c141d6f9ebe44712a5d9c5f9aa67dcb1.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question