■ 마우스 히트 테스트(Hit Test)를 사용하는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 그래픽스 경로 1 /// </summary> private GraphicsPath graphicsPath1; /// <summary> /// 그래픽스 경로 2 /// </summary> private GraphicsPath graphicsPath2; /// <summary> /// 영역 2 /// </summary> private Region region2; /// <summary> /// 사각형 3 /// </summary> private Rectangle rectangle3; /// <summary> /// 그래픽스 경로 1 내부 위치 여부 /// </summary> private bool inGraphicsPath1 = false; /// <summary> /// 영역 2 내부 위치 여부 /// </summary> private bool inRegion2 = false; /// <summary> /// 사각형 3 내부 위치 여부 /// </summary> private bool inRectangle3 = false; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.graphicsPath1 = new GraphicsPath(); this.graphicsPath1.AddLine(50 , 50 , 170, 50 ); this.graphicsPath1.AddLine(170, 50 , 170, 140); this.graphicsPath1.AddLine(170, 140, 50 , 170); this.graphicsPath1.CloseFigure(); this.graphicsPath2 = new GraphicsPath(); this.graphicsPath2.AddLine(200, 50 , 380, 50 ); this.graphicsPath2.AddLine(380, 50 , 380, 140); this.graphicsPath2.AddLine(380, 140, 470, 140); this.graphicsPath2.AddLine(470, 140, 470, 200); this.graphicsPath2.AddLine(470, 200, 320, 200); this.graphicsPath2.AddLine(320, 200, 320, 140); this.graphicsPath2.AddLine(320, 140, 200, 140); this.graphicsPath2.CloseFigure(); this.region2 = new Region(graphicsPath2); this.rectangle3 = new Rectangle(50, 290, 260, 200); MouseMove += Form_MouseMove; Paint += Form_Paint; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 폼 마우스 이동시 처리하기 - Form_MouseMove(sender, e) /// <summary> /// 폼 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_MouseMove(object sender, MouseEventArgs e) { Graphics graphics = CreateGraphics(); try { if(this.inGraphicsPath1) { if(!this.graphicsPath1.IsVisible(e.X, e.Y)) { graphics.FillPath(Brushes.White, this.graphicsPath1); graphics.DrawPath(Pens.Black, this.graphicsPath1); this.inGraphicsPath1 = false; return; } } else { if(this.graphicsPath1.IsVisible(e.X, e.Y)) { graphics.FillPath(Brushes.LightBlue, this.graphicsPath1); graphics.DrawPath(Pens.Black, this.graphicsPath1); this.inGraphicsPath1 = true; return; } } if(this.inRegion2) { if(!this.region2.IsVisible(e.X, e.Y)) { graphics.FillRegion(Brushes.White, this.region2); graphics.DrawPath(Pens.Black, this.graphicsPath2); this.inRegion2 = false; return; } } else { if(this.region2.IsVisible(e.X, e.Y)) { graphics.FillRegion(Brushes.Pink, this.region2); graphics.DrawPath(Pens.Black, this.graphicsPath2); this.inRegion2 = true; return; } } if(this.inRectangle3) { if(!this.rectangle3.Contains(e.X, e.Y)) { graphics.FillRectangle(Brushes.White, this.rectangle3); graphics.DrawRectangle(Pens.Black, this.rectangle3); this.inRectangle3 = false; return; } } else { if(this.rectangle3.Contains(e.X, e.Y)) { graphics.FillRectangle(Brushes.LightGreen, this.rectangle3); graphics.DrawRectangle(Pens.Black, this.rectangle3); this.inRectangle3 = true; return; } } } finally { graphics.Dispose(); } } #endregion #region 폼 페인트시 처리하기 - Form_Paint(sender, e) /// <summary> /// 폼 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Paint(object sender, PaintEventArgs e) { Graphics graphics = e.Graphics; graphics.FillRectangle(Brushes.White, ClientRectangle); graphics.DrawPath(Pens.Black, graphicsPath1); graphics.DrawPath(Pens.Black, graphicsPath2); graphics.DrawRectangle(Pens.Black, rectangle3); } #endregion } } |