cscの作法 その192

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

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

# 参考にしたページ

https://qiita.com/guijiu/items/1954cf2fa73a907eabd5

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/a61675ab-32e3-b45c-a1d7-b38a1026c749.png)

# サンプルコード

```
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Project1
{
	class Class1 : Form {
		Image[] images = new Image[] {
			Image.FromFile(@"sakana.png"),
			Image.FromFile(@"kusa.png")
		};
		Image currentImage = null;
		Image backImage = Image.FromFile(@"ki.png");
		Label lb0,
			lb1,
			lb2,
			lb3;
		Panel panel1,
			panel2,
			panel3;
		Button button1;
		private List<Point> ls;
		public Class1() {
			Text = "Panelをいくつか配置し、グラフィックス、コントロール、イメージを表示する";
			Width = 640;
			Height = 640;
			ls = new List<Point>();
			panel1 = new Panel();
			panel1.BorderStyle = BorderStyle.FixedSingle;
			panel1.Location = new Point(300, 300);
			panel1.Size = new Size(100, 100);
			panel1.BackColor = Color.Gray;
			this.Controls.Add(panel1);
			panel2 = new Panel();
			panel2.BorderStyle = BorderStyle.FixedSingle;
			panel2.Location = new Point(100, 200);
			panel2.Size = new Size(200, 100);
			this.Controls.Add(panel2);
			panel3 = new Panel();
			panel3.BorderStyle = BorderStyle.FixedSingle;
			panel3.Location = new Point(0, 400);
			panel3.Size = new Size(300, 200);
			this.Controls.Add(panel3);
			button1 = new Button();
			button1.Text = "ボディーカラー変更\nこの赤いボタンを押してください。";
			button1.AutoSize = true;
			button1.Dock = DockStyle.Bottom;
			button1.BackColor = Color.Red;
			panel2.Controls.Add(button1);
			lb0 = new Label();
			lb0.ForeColor = Color.Blue;
			lb0.BackColor = Color.Yellow;
			lb0.Text = "thisの領域";
			this.Controls.Add(lb0);
			lb1 = new Label();
			lb1.Text = "panel1の領域";
			panel1.Controls.Add(lb1);
			lb2 = new Label();
			lb2.Text = "panel2の領域";
			panel2.Controls.Add(lb2);
			lb3 = new Label();
			lb3.AutoSize = true;
			lb3.Text = "panel3の領域。\nこの領域でクリックしてください";
			panel3.Controls.Add(lb3);
			currentImage = images[0];
			button1.Click += new EventHandler(Button1_Click);
			this.Paint += new PaintEventHandler(Back_Paint);
			panel1.Paint += new PaintEventHandler(Panel1_Paint);
			panel3.MouseDown += new MouseEventHandler(Panel3_MouseDown);
			panel3.Paint += new PaintEventHandler(Panel3_Paint);
		}
		private void Panel1_Paint(Object sender, PaintEventArgs e) {
			if (currentImage != null)
			{
				Graphics g = e.Graphics;
				g.DrawImage(currentImage, 0, 25, currentImage.Width, currentImage.Height);
			}
		}
		private void Back_Paint(Object sender, PaintEventArgs e) {
			if (backImage != null)
			{
				Graphics g = e.Graphics;
				g.DrawImage(backImage, 0, 0, backImage.Width, backImage.Height);
			}
		}
		private void Button1_Click(object sender, EventArgs e) {
			if (currentImage != images[0])
			{
				currentImage = images[0];
			}
			else
			{
				currentImage = images[1];
			}
			this.Invalidate();
			panel1.Invalidate();
		}
		public void Panel3_MouseDown(Object sender, MouseEventArgs e) {
			Point p = new Point();
			p.X = e.X;
			p.Y = e.Y;
			ls.Add(p);
			panel3.Invalidate();
		}
		public void Panel3_Paint(Object sender, PaintEventArgs e) {
			Graphics g = e.Graphics;
			Pen dp = new Pen(Color.Pink, 3);
			foreach (Point p in ls)
			{
				g.DrawEllipse(dp, p.X - 5, p.Y - 5, 10, 10);
			}
		}
		public static void Main() {
			Application.Run(new Class1());
		}
	}
}





```


以上。