cscの作法 その193

# 概要
cscの作法、調べてみた。
練習問題やってみた。

# 練習問題
マウスイベントを使え。

# 参考にしたページ

https://kats-eye.net/info/2020/02/18/c-mous-dd-01/

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/b71e0d34-f37b-6b96-a7ed-9a912c2c16ba.png)


# サンプルコード

```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace ListViewTest
{
	public partial class Form1 : Form {
		bool draw_flag = false;
		Point mus_dn = new Point();
		PictureBox pictureBox1;
		PictureBox pictureBox2;
		PictureBox pictureBox3;
		private Button button1;
		private Button button2;
		private Button button3;
		public Form1() {
			this.Text = "listView";
			this.Size = new Size(500, 400);
			this.Load += new EventHandler(this.Form1_Load);
			this.button1 = new Button();
			this.button2 = new Button();
			this.button3 = new Button();
			this.button1.Location = new System.Drawing.Point(330, 30);
			this.button1.TabIndex = 0;
			this.button1.Text = "1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			this.button2.Location = new System.Drawing.Point(330, 80);
			this.button2.TabIndex = 0;
			this.button2.Text = "2";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			this.button3.Location = new System.Drawing.Point(330, 130);
			this.button3.TabIndex = 0;
			this.button3.Text = "3";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			pictureBox1 = new PictureBox();
			pictureBox1.Width = 300;
			pictureBox1.Height = 300;
			pictureBox2 = new PictureBox();
			pictureBox3 = new PictureBox();
			Controls.AddRange(new Control[] {
				button1,
				button2,
				button3,
				pictureBox1,
				pictureBox2,
				pictureBox3
			});
		}
		private void Form1_Load(object sender, EventArgs e) {
			pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
			pictureBox1.ImageLocation = @"sakana.png";
			pictureBox2.Parent = pictureBox1;
			pictureBox2.BackColor = Color.Transparent;
			pictureBox2.Location = new Point(pictureBox2.Location.X - pictureBox1.Location.X, pictureBox2.Location.Y - pictureBox1.Location.Y);
			pictureBox2.Size = pictureBox1.Size;
			pictureBox3.Parent = pictureBox2;
			pictureBox3.BackColor = Color.Transparent;
			pictureBox3.Location = new Point(pictureBox3.Location.X - pictureBox1.Location.X, pictureBox3.Location.Y - pictureBox1.Location.Y);
			pictureBox3.Size = pictureBox2.Size;
			pictureBox3.MouseDown += new MouseEventHandler(pictureBox3_MouseDown);
			pictureBox3.MouseUp += new MouseEventHandler(pictureBox3_MouseUp);
			pictureBox3.MouseMove += new MouseEventHandler(pictureBox3_MouseMove);

		}
		private void button1_Click(object sender, EventArgs e) {
			pictureBox2.Image = null;
		}
		private void button2_Click(object sender, EventArgs e) {
			pictureBox2.Image = null;
			int pic_w = pictureBox2.Width;
			int pic_h = pictureBox2.Height;
			int pic_margin = 5;
			int rect_w = pic_w - 2 * pic_margin - 1;
			int rect_h = pic_h - 2 * pic_margin - 1;
			int crcl_x = pic_w / 2 - rect_h / 2;
			Bitmap cvs = new Bitmap(pic_w, pic_h);
			Graphics g = Graphics.FromImage(cvs);
			Pen p1 = new Pen(Color.Blue, 2);
			Pen p2 = new Pen(Color.FromArgb(128, 255, 0, 0), 10);
			SolidBrush b1 = new SolidBrush(Color.FromArgb(128, 0, 255, 0));
			g.DrawRectangle(p1, pic_margin, pic_margin, rect_w, rect_h);
			g.DrawEllipse(p2, pic_margin, pic_margin, rect_w, rect_h);
			g.FillEllipse(b1, crcl_x, pic_margin, rect_h, rect_h);
			p1.Dispose();
			p2.Dispose();
			b1.Dispose();
			g.Dispose();
			pictureBox2.Image = cvs;
		}
		private void button3_Click(object sender, EventArgs e) {
			pictureBox2.Image = null;
			int pic_w = pictureBox2.Width;
			int pic_h = pictureBox2.Height;
			Bitmap cvs = new Bitmap(pic_w, pic_h);
			Graphics g = Graphics.FromImage(cvs);
			Point p01 = new Point(60, 40);
			Point p02 = new Point(120, 300);
			Point p03 = new Point(240, 60);
			Point p04 = new Point(400, 280);
			Point p05 = new Point(440, 200);
			Point p06 = new Point(380, 120);
			Point[] c_points = { p01, p02, p03, p04, p05, p06 };
			Pen p_b = new Pen(Color.FromArgb(80, 0, 0, 255), 10);
			Pen p_r = new Pen(Color.FromArgb(80, 255, 0, 0), 10);
			Pen p_g = new Pen(Color.FromArgb(80, 0, 255, 0), 10);
			g.DrawCurve(p_b , c_points , 0.5F);
			g.DrawCurve(p_r, c_points, 1);
			g.DrawClosedCurve(p_g, c_points, 1, FillMode.Alternate);
			p_b.Dispose();
			p_r.Dispose();
			p_g.Dispose();
			g.Dispose();
			pictureBox2.Image = cvs;
		}
		private void pictureBox3_MouseDown(object sender, MouseEventArgs e) {
			draw_flag = true;
			mus_dn.X = e.X;
			mus_dn.Y = e.Y;
		}
		private void pictureBox3_MouseMove(object sender, MouseEventArgs e) {
			if (!draw_flag)
			{
				return;
			};
			Bitmap tmp_cvs = new Bitmap(pictureBox3.Width, pictureBox3.Height);
			Graphics tmp_g = Graphics.FromImage(tmp_cvs);
			Point tmp_mv = new Point();
			Pen tmp_p = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
			tmp_p.DashStyle = DashStyle.Dot;
			if (e.X - mus_dn.X >= 0)
			{
				tmp_mv.X = mus_dn.X;
			}
			else
			{
				tmp_mv.X = e.X;
			}
			if (e.Y - mus_dn.Y >= 0)
			{
				tmp_mv.Y = mus_dn.Y;
			}
			else
			{
				tmp_mv.Y = e.Y;
			}
			int tmp_wdt = Math.Abs(e.X - mus_dn.X);
			int tmp_hgt = Math.Abs(e.Y - mus_dn.Y);
			tmp_g.DrawRectangle(tmp_p, tmp_mv.X, tmp_mv.Y, tmp_wdt, tmp_hgt);
			pictureBox3.Image = tmp_cvs;
		}
		private void pictureBox3_MouseUp(object sender, MouseEventArgs e) {
			if (!draw_flag)
			{
				return;
			};
			pictureBox3.Image=null;
			Bitmap tmp_cvs = new Bitmap(pictureBox2.Width, pictureBox2.Height);
			Graphics tmp_g = Graphics.FromImage(tmp_cvs);
			Image img = pictureBox2.Image ;
			Point tmp_mv = new Point();
			Pen tmp_p = new Pen(Color.FromArgb(80, 255, 0, 0), 10);
			if (e.X - mus_dn.X >= 0)
			{
				tmp_mv.X = mus_dn.X;
			}
			else
			{
				tmp_mv.X = e.X;
			}
			if (e.Y - mus_dn.Y >= 0)
			{
				tmp_mv.Y = mus_dn.Y;
			}
			else
			{
				tmp_mv.Y = e.Y;
			}
			int tmp_wdt = Math.Abs(e.X - mus_dn.X);
			int tmp_hgt = Math.Abs(e.Y - mus_dn.Y);
			if (img != null)
			{
				tmp_g.DrawImage(img, 0, 0, img.Width, img.Height);
			}
			tmp_g.DrawEllipse(tmp_p, tmp_mv.X, tmp_mv.Y, tmp_wdt, tmp_hgt);
			pictureBox2.Image = tmp_cvs;
			draw_flag = false;
		}
		[STAThread]
		static void Main() {
			Application.Run(new Form1());
		}
	}
}





```

以上。