LoginSignup
0
0

More than 1 year has passed since last update.

WindowsFomのpicureBox上での範囲指定

Last updated at Posted at 2021-10-16

概要

  • マウスを使ってpictureBox上で範囲指定するプログラムです。
  • 指定した範囲を線で表示します。
  • マウスの3つのイベントを利用してます。
    SelectArea_06.jpg

作業手順

①新規プロジェクト作成

  • Windows フォームアプリケーション(.Net Framework)を選択
  • 名前:SelectArea
    SelectArea_01.jpg

②pictureBoxを作成

・Name:_Picture
・BackColorを設定:pictureBoxの範囲を見える化
SelectArea_02.jpg
SelectArea_04.jpg

③Form1.cssに下記のように記述

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

namespace SelectArea
{
    public partial class Form1 : Form
    {
        Rectangle _Rect;
        Point _PosStart;
        bool _IsSelect = false;

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// マウスボタンが押された時のイベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // 範囲選択開始
                _IsSelect = true;

                // 開始位置をセットする
                _PosStart.X = e.X;
                _PosStart.Y = e.Y;
            }
        }

        /// <summary>
        /// マウスポインターが移動した時のイベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && _IsSelect)
            {
                // 四角形を描く
                DrawRect(e.X, e.Y);
            }
        }

        /// <summary>
        /// マウスボタンが離された時のイベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnMouseUp(object sender, MouseEventArgs e)
        {
            if ((e.Button == MouseButtons.Left) && (_IsSelect == true))
            {
                //四角形を描く
                DrawRect(e.X, e.Y);

                // 範囲選択終了
                _IsSelect = false;
            }
        }

        /// <summary>
        /// 四角形の描画
        /// </summary>
        /// <param name="clickX"></param>
        /// <param name="clickY"></param>
        void DrawRect(int clickX, int clickY)
        {
            // PictureBoxの範囲内に収める
            if (clickX < 0) clickX = 0;
            else if (clickX >= _PictureBox.Width) clickX = _PictureBox.Width - 1;
            if (clickY < 0) clickY = 0;
            else if (clickY >= _PictureBox.Height) clickY = _PictureBox.Height - 1;

            // サイズをセットする
            _Rect.Width = Math.Abs(clickX - _PosStart.X);
            _Rect.Height = Math.Abs(clickY - _PosStart.Y);

            // 開始位置より小さい場合は座標を入れ替える
            _Rect.X = (clickX >= _PosStart.X) ? _PosStart.X : clickX;
            _Rect.Y = (clickY >= _PosStart.Y) ? _PosStart.Y : clickY;

            // 青の破線に設定する
            Pen pen = new Pen(Color.Blue, 1);
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;

            // 描画領域をリフレッシュする
            _PictureBox.Refresh();

            // 選択範囲を描画する
            Graphics g = _PictureBox.CreateGraphics();
            g.DrawRectangle(pen, _Rect);
            g.Dispose();
        }
    }
}

④マウス関連のイベントを登録

SelectArea_05.jpg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0