【C#】【Form】PictureBox [6] ~ PictureBox 内でMouseMoveイベントにより線を描画する ~

■ はじめに

https://blogs.yahoo.co.jp/dk521123/32877749.html
で「例4:マウスを追いかける」を行ったが、
VS2017/Windows10で実行したらうまくいかなかったので作り直す

■ サンプル

例1:シンプルなサンプル

using System;
using System.Drawing;
using System.Windows.Forms;

namespace SampleForm
{
  public partial class Form1 : Form
  {
    private Point startPoint = Point.Empty;
    private Point nowPoint = Point.Empty;

    public Form1()
    {
      InitializeComponent();
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
      if (e.Button != MouseButtons.Left)
      {
        // 右クリックは無視
        return;
      }

      this.startPoint = e.Location;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
      if (this.startPoint.IsEmpty)
      {
        return;
      }

      this.nowPoint = e.Location;

      var pictureBox = sender as PictureBox;
      pictureBox.Invalidate();
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
      var graphics = e.Graphics;
      graphics.DrawLines(Pens.Red, new Point[] { startPoint, nowPoint });
    }
  }
}

例2:マウスを追いかける(改)

using System;
using System.Drawing;
using System.Windows.Forms;

namespace SampleForm
{
  public partial class Form1 : Form
  {
    private Point startPoint = Point.Empty;
    private Point endPoint = Point.Empty;
    private Point nowPoint = Point.Empty;

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      this.pictureBox1.InitialImage = this.pictureBox1.Image;
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
      if (e.Button != MouseButtons.Left)
      {
        // 右クリックは無視
        return;
      }

      var targetPoint = e.Location;

      if (!this.startPoint.IsEmpty && !this.endPoint.IsEmpty)
      {
        // Clear
        this.Clear();
      }
      else if (!this.startPoint.IsEmpty && this.endPoint.IsEmpty)
      {
        this.endPoint = targetPoint;
      }
      else
      {
        this.startPoint = targetPoint;
        this.endPoint = Point.Empty;
      }

      var pictureBox = sender as PictureBox;
      pictureBox.Invalidate();
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
      if (this.startPoint.IsEmpty)
      {
        return;
      }

      if (this.endPoint.IsEmpty)
      {
        this.nowPoint = e.Location;
      }
      else
      {
        this.nowPoint = Point.Empty;
      }

      var pictureBox = sender as PictureBox;
      pictureBox.Invalidate();
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
      var graphics = e.Graphics;

      if (!this.startPoint.IsEmpty)
      {
        this.FillCircle(graphics, Brushes.White, this.startPoint.X, this.startPoint.Y, 10);
        this.FillCircle(graphics, Brushes.SkyBlue, this.startPoint.X, this.startPoint.Y, 8);
      }

      if (!this.endPoint.IsEmpty)
      {
        this.FillCircle(graphics, Brushes.White, this.endPoint.X, this.endPoint.Y, 10);
        this.FillCircle(graphics, Brushes.LightGreen, this.endPoint.X, this.endPoint.Y, 8);

        graphics.DrawLine(new Pen(Color.LightGreen, 3), this.startPoint, this.endPoint);

        return;
      }

      if (!this.nowPoint.IsEmpty)
      {
        graphics.DrawLines(Pens.Red, new Point[] { startPoint, nowPoint });
      }
    }

    private void Clear()
    {
      this.startPoint = Point.Empty;
      this.endPoint = Point.Empty;
      this.nowPoint = Point.Empty;

      this.pictureBox1.Image = this.pictureBox1.InitialImage;
    }

    /// <summary>
    /// x,yを中心とした半径radiusの円を書く
    /// </summary>
    /// <param name="graphics">graphicsオブジェクト</param>
    /// <param name="brush">brushオブジェクト</param>
    /// <param name="x">x座標</param>
    /// <param name="y">y座標</param>
    /// <param name="radius">半径</param>
    /// <see cref="">https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q11173767351"/>
    private void FillCircle(Graphics graphics, Brush brush, float x, float y, float radius)
    {
      graphics.FillEllipse(brush, x - radius, y - radius, radius * 2, radius * 2);
    }
  }
}


関連記事

Windows Form ~ 目次 ~

https://blogs.yahoo.co.jp/dk521123/8054245.html

Graphics ~ さまざまな描画 ~

https://blogs.yahoo.co.jp/dk521123/32877749.html

C#】図形 ~ 矩形 / Rectangle ~

https://blogs.yahoo.co.jp/dk521123/37888274.html

PictureBox

PictureBox [1] ~ 画像を表示する ~
https://blogs.yahoo.co.jp/dk521123/23504075.html
PictureBox [2] ~ PictureBox を マウスで移動する ~
https://blogs.yahoo.co.jp/dk521123/37861699.html
PictureBox [3] ~ マウスホイール で画像の拡大・縮小する ~
https://blogs.yahoo.co.jp/dk521123/37866101.html
PictureBox [4] ~ PictureBox 内に文字列を描画する ~
https://blogs.yahoo.co.jp/dk521123/37890831.html
PictureBox [5] ~ PictureBox 内に画像を描画する ~
https://blogs.yahoo.co.jp/dk521123/37890873.html
PictureBox [7] ~ 画像をコピーする ~
https://blogs.yahoo.co.jp/dk521123/37857445.html