■ 마우스 드래그 대상 이미지를 표시하는 방법을 보여준다.
▶ 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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Runtime.InteropServices; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 디바이스 컨텍스트 생성하기 - CreateDC(driverName, deviceName, port, initialDataHandle) /// <summary> /// 디바이스 컨텍스트 생성하기 /// </summary> /// <param name="driverName">드라이버명</param> /// <param name="deviceName">장치명</param> /// <param name="port">포트</param> /// <param name="initialDataHandle">초기 데이터 핸들</param> /// <returns>디바이스 컨텍스트 핸들</returns> [DllImport("gdi32")] private static extern IntPtr CreateDC(string driverName, string deviceName, string port, IntPtr initialDataHandle); #endregion #region 디바이스 컨텍스트 삭제하기 - DeleteDC(deviceContextHandle) /// <summary> /// 디바이스 컨텍스트 삭제하기 /// </summary> /// <param name="deviceContextHandle">디바이스 컨텍스트 핸들</param> /// <returns>처리 결과</returns> [DllImport("gdi32")] private static extern bool DeleteDC(IntPtr deviceContextHandle); #endregion #region 클라이언트 좌표에서 화면 좌표 구하기 - ClientToScreen(windowHandle, screenPoint) /// <summary> /// 클라이언트 좌표에서 화면 좌표 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="screenPoint">화면 좌표계 포인트</param> /// <returns>처리 결과</returns> [DllImport("user32")] private static extern bool ClientToScreen(IntPtr windowHandle, ref Point screenPoint); #endregion #region 비트맵 복사하기 - BitBlt(targetDeviceContextHandle, targetX, targetY, width, height, sourceDeviceContextHandle, sourceX, sourceY, rasterOperationCode /// <summary> /// 비트맵 복사하기 /// </summary> /// <param name="targetDeviceContextHandle">타겟 디바이스 컨텍스트 핸들</param> /// <param name="targetX">타겟 X</param> /// <param name="targetY">타겟 Y</param> /// <param name="width">너비</param> /// <param name="height">높이</param> /// <param name="sourceDeviceContextHandle">소스 디바이스 컨텍스트 핸들</param> /// <param name="sourceX">소스 X</param> /// <param name="sourceY">소스 Y</param> /// <param name="rasterOperationCode">래스터 연산 코드</param> /// <returns>처리 결과</returns> [DllImport("gdi32")] private static extern bool BitBlt ( IntPtr targetDeviceContextHandle, int targetX, int targetY, int width, int height, IntPtr sourceDeviceContextHandle, int sourceX, int sourceY, int rasterOperationCode ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// SRCCOPY /// </summary> private const int SRCCOPY = 0xcc0020; /// <summary> /// 드래그 문자열 /// </summary> private string dragString; /// <summary> /// 드래그 문자열 포인트 /// </summary> private Point dragStringPoint; /// <summary> /// 드래그 문자열 폰트 /// </summary> private Font dragStringFont; /// <summary> /// 드래그 이미지 크기 /// </summary> private Size dragImageSize; /// <summary> /// 마우스 DOWN 여부 /// </summary> private bool mouseDown = false; /// <summary> /// 마우스 델타 포인트 /// </summary> private Point mouseDeltaPoint; /// <summary> /// 전체 화면 디바이스 컨텍스트 핸들 /// </summary> private IntPtr displayDeviceContextHandle; /// <summary> /// 전체 화면 그래픽스 /// </summary> private Graphics displayGraphics; /// <summary> /// 백업 저장소 비트맵 /// </summary> private Bitmap backingStoreBitmap; /// <summary> /// 백업 저장소 그래픽스 /// </summary> private Graphics backingStoreGraphics; /// <summary> /// 백업 저장소 디바이스 컨텍스트 핸들 /// </summary> private IntPtr backingStoreDeviceContextHandle; /// <summary> /// 백업 저장소 포인트 /// </summary> private Point backingStorePoint; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.dragString = "Drag Me!"; this.dragStringPoint = new Point(100, 100); this.dragStringFont = new Font("Times New Roman", 14, FontStyle.Bold); Load += Form_Load; MouseDown += Form_MouseDown; MouseMove += Form_MouseMove; MouseUp += Form_MouseUp; Paint += Form_Paint; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { Graphics graphics = CreateGraphics(); SizeF dragStringSize = graphics.MeasureString(this.dragString, this.dragStringFont); this.dragImageSize = new Size((int)dragStringSize.Width, (int)dragStringSize.Height); graphics.Dispose(); } #endregion #region 폼 마우스 DOWN 처리하기 - Form_MouseDown(sender, e) /// <summary> /// 폼 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_MouseDown(object sender, MouseEventArgs e) { Rectangle dragStringRectangle = new Rectangle ( this.dragStringPoint, new Size(this.dragImageSize.Width, this.dragImageSize.Height) ); if(dragStringRectangle.Contains(e.Location)) { this.mouseDown = true; this.mouseDeltaPoint = new Point(e.X - this.dragStringPoint.X, e.Y - this.dragStringPoint.Y); Point screenPoint = GetScreenPoint(e.X, e.Y); screenPoint.X -= this.mouseDeltaPoint.X; screenPoint.Y -= this.mouseDeltaPoint.Y; SetDisplayGraphics(); this.backingStoreBitmap = new Bitmap(this.dragImageSize.Width, this.dragImageSize.Height); this.backingStoreGraphics = Graphics.FromImage(backingStoreBitmap); this.backingStoreDeviceContextHandle = this.backingStoreGraphics.GetHdc(); BitBlt ( this.backingStoreDeviceContextHandle, 0, 0, this.backingStoreBitmap.Width, this.backingStoreBitmap.Height, this.displayDeviceContextHandle, screenPoint.X, screenPoint.Y, SRCCOPY ); this.backingStorePoint = new Point(screenPoint.X, screenPoint.Y); DrawDragImage(this.displayGraphics, true, screenPoint.X, screenPoint.Y, dragString); } } #endregion #region 폼 마우스 이동시 처리하기 - Form_MouseMove(sender, e) /// <summary> /// 폼 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_MouseMove(object sender, MouseEventArgs e) { if(this.mouseDown) { Point screenPoint = GetScreenPoint(e.X, e.Y); screenPoint.X -= this.mouseDeltaPoint.X; screenPoint.Y -= this.mouseDeltaPoint.Y; BitBlt ( this.displayDeviceContextHandle, this.backingStorePoint.X, this.backingStorePoint.Y, this.backingStoreBitmap.Width, this.backingStoreBitmap.Height, this.backingStoreDeviceContextHandle, 0, 0, SRCCOPY ); BitBlt ( this.backingStoreDeviceContextHandle, 0, 0, this.backingStoreBitmap.Width, this.backingStoreBitmap.Height, this.displayDeviceContextHandle, screenPoint.X, screenPoint.Y, SRCCOPY ); this.backingStorePoint = new Point(screenPoint.X, screenPoint.Y); DrawDragImage(this.displayGraphics, true, screenPoint.X, screenPoint.Y, this.dragString); } } #endregion #region 폼 마우스 UP 처리하기 - Form_MouseUp(sender, e) /// <summary> /// 폼 마우스 UP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_MouseUp(object sender, MouseEventArgs e) { if(this.mouseDown) { this.mouseDown = false; BitBlt ( this.displayDeviceContextHandle, this.backingStorePoint.X, this.backingStorePoint.Y, this.backingStoreBitmap.Width, this.backingStoreBitmap.Height, this.backingStoreDeviceContextHandle, 0, 0, SRCCOPY ); DisposeDisplayDeviceContext(); this.backingStoreGraphics.ReleaseHdc(this.backingStoreDeviceContextHandle); this.backingStoreGraphics.Dispose(); this.backingStoreBitmap.Dispose(); this.backingStoreBitmap = null; } } #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.Bisque, ClientRectangle); Font font = new Font("Times New Roman", 32); graphics.DrawString("Hello, World!", font, Brushes.Blue, 10, 10); font.Dispose(); DrawDragImage(graphics, false, this.dragStringPoint.X, this.dragStringPoint.X, this.dragString); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 화면 포인트 구하기 - GetScreenPoint(x, y) /// <summary> /// 화면 포인트 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <returns>화면 포인트</returns> private Point GetScreenPoint(int x, int y) { Point screenPoint = new Point(x, y); ClientToScreen(Handle, ref screenPoint); return screenPoint; } #endregion #region 전체 화면 그래픽스 설정하기 - SetDisplayGraphics() /// <summary> /// 전체 화면 그래픽스 설정하기 /// </summary> private void SetDisplayGraphics() { this.displayDeviceContextHandle = CreateDC("DISPLAY", null, null, (IntPtr)null); this.displayGraphics = Graphics.FromHdc(this.displayDeviceContextHandle); } #endregion #region 드래그 이미지 그리기 - DrawDragImage(graphics, alphaBlending, x, y, text) /// <summary> /// 드래그 이미지 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="alphaBlending">알파 블렌딩 적용 여부</param> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="text">텍스트</param> private void DrawDragImage(Graphics graphics, bool alphaBlending, int x, int y, string text) { Rectangle rectangle = new Rectangle(x, y, this.dragImageSize.Width, this.dragImageSize.Height); if(alphaBlending) { Color color1 = Color.FromArgb(0 , Color.Red); Color color2 = Color.FromArgb(80, Color.Red); Rectangle rectangle1 = new Rectangle ( rectangle.Left, rectangle.Top, rectangle.Width / 2, rectangle.Height ); Rectangle rectangle2 = new Rectangle ( rectangle.Left + rectangle.Width / 2, rectangle.Top, rectangle.Width / 2, rectangle.Height ); LinearGradientBrush brush1 = new LinearGradientBrush(rectangle1, color1, color2, 0f); LinearGradientBrush brush2 = new LinearGradientBrush(rectangle2, color2, color1, 0f); graphics.FillRectangle ( brush1, new Rectangle ( rectangle1.Left + 1, rectangle1.Top, rectangle1.Width + 1, rectangle1.Height ) ); graphics.FillRectangle(brush2, rectangle2); brush1.Dispose(); brush2.Dispose(); } else { graphics.FillRectangle(Brushes.Red, rectangle); } Color textColor; if(alphaBlending) { textColor = Color.FromArgb(80, Color.Black); } else { textColor = Color.Black; } Brush textBrush = new SolidBrush(textColor); graphics.DrawString(text, this.dragStringFont, textBrush, rectangle); textBrush.Dispose(); } #endregion #region 전체 화면 디바이스 컨텍스트 리소스 해제하기 - DisposeDisplayDeviceContext() /// <summary> /// 전체 화면 디바이스 컨텍스트 리소스 해제하기 /// </summary> private void DisposeDisplayDeviceContext() { this.displayGraphics.Dispose(); DeleteDC(this.displayDeviceContextHandle); } #endregion } } |