DEMO5_5.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:19k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo5_5 --- Simple Paint V.2 */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include <math.h>
- 7 #include "demo5_5.h"
- 8
- 9
- 10 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 11 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 12 long FAR PASCAL ChooseCtrlProc(HWND, unsigned, WORD, LONG);
- 13 long FAR PASCAL LineWSCtrlProc(HWND, unsigned, WORD, LONG);
- 14 BOOL FAR PASCAL PenDlgProc(HWND, unsigned, WORD, LONG);
- 15 BOOL FAR PASCAL BrushDlgProc(HWND, unsigned, WORD, LONG);
- 16
- 17 void DrawGraph(HDC, HMENU, BOOL);
- 18 void DrawPencil(HDC, HMENU);
- 19 void DrawLine(HDC, HMENU, BOOL);
- 20 void DrawRect(HDC, HMENU, BOOL);
- 21 void DrawEllip(HDC, HMENU, BOOL);
- 22 void DrawCircle(HDC, HMENU, BOOL);
- 23 void DrawRoundRect(HDC, HMENU, BOOL);
- 24
- 25 FARPROC lpPenDlgProc;
- 26 FARPROC lpBrushDlgProc;
- 27
- 28 HANDLE hInst;
- 29
- 30 int ToolID = IDM_PENCIL;
- 31
- 32 int nPenColor = 1;
- 33 int nPenStyle = PS_SOLID;
- 34 int nPenWidth = 1;
- 35
- 36 int nBrushColor = 1;
- 37 int nHatch = -1;
- 38
- 39 typedef struct tagCOLORSTRUCT {
- 40 int cR;
- 41 int cG;
- 42 int cB;
- 43 } COLORSTRUCT;
- 44
- 45 #define MKCOLOR(A) (RGB(A.cR, A.cG, A.cB))
- 46
- 47 extern COLORSTRUCT crDefColor[28];
- 48 extern COLORSTRUCT crPCurColor[28];
- 49 extern COLORSTRUCT crBCurColor[28];
- 50
- 51 POINT OrgPoint;
- 52 POINT PrePoint;
- 53 POINT CurPoint;
- 54
- 55 BOOL CanUndo = FALSE;
- 56 HBITMAP hBitmap;
- 57 HDC hMemDC;
- 58
- 59 /****************************************************************/
- 60 /* WinMain() */
- 61 /****************************************************************/
- 62
- 63 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 64 LPSTR lpszCmdLine, int nCmdShow)
- 65 {
- 66 WNDCLASS wclass;
- 67 MSG msg;
- 68 HWND hWnd;
- 69 char szName[] = "Demo5_5";
- 70
- 71 if (!hPrevInstance)
- 72 {
- 73 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 74 wclass.lpfnWndProc = MainWndProc;
- 75 wclass.cbClsExtra = 0;
- 76 wclass.cbWndExtra = 0;
- 77 wclass.hInstance = hInstance;
- 78 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 79 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 80 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 81 wclass.lpszMenuName = szName;
- 82 wclass.lpszClassName = szName;
- 83
- 84 if (!RegisterClass (&wclass))
- 85 return (FALSE);
- 86
- 87 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 88 wclass.lpfnWndProc = ChooseCtrlProc;
- 89 wclass.cbClsExtra = 0;
- 90 wclass.cbWndExtra = 0;
- 91 wclass.hInstance = hInstance;
- 92 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 93 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 94 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 95 wclass.lpszMenuName = NULL;
- 96 wclass.lpszClassName = "Choose";
- 97
- 98 if (!RegisterClass (&wclass))
- 99 return (FALSE);
- 100
- 101 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 102 wclass.lpfnWndProc = LineWSCtrlProc;
- 103 wclass.cbClsExtra = 0;
- 104 wclass.cbWndExtra = 0;
- 105 wclass.hInstance = hInstance;
- 106 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 107 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 108 wclass.hbrBackground = COLOR_WINDOW + 1;
- 109 wclass.lpszMenuName = NULL;
- 110 wclass.lpszClassName = "LineWS";
- 111
- 112 if (!RegisterClass (&wclass))
- 113 return (FALSE);
- 114 }
- 115
- 116 hWnd = CreateWindow(
- 117 szName,
- 118 "Simple Paint V.2" ,
- 119 WS_OVERLAPPEDWINDOW,
- 120 CW_USEDEFAULT,
- 121 CW_USEDEFAULT,
- 122 CW_USEDEFAULT,
- 123 CW_USEDEFAULT,
- 124 NULL,
- 125 NULL,
- 126 hInstance,
- 127 NULL );
- 128
- 129 if (!hWnd)
- 130 return (FALSE);
- 131
- 132 ShowWindow(hWnd, nCmdShow);
- 133 UpdateWindow(hWnd);
- 134
- 135 while (GetMessage(&msg, NULL, NULL,NULL))
- 136 {
- 137 TranslateMessage(&msg);
- 138 DispatchMessage(&msg);
- 139 }
- 140 return (msg.wParam);
- 141 }
- 142
- 143
- 144 /****************************************************************/
- 145 /* MainWndProc() */
- 146 /****************************************************************/
- 147
- 148 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 149 WORD wParam, LONG lParam)
- 150 {
- 151 HDC hDC;
- 152 HMENU hMenu;
- 153 PAINTSTRUCT ps;
- 154 static BOOL bLBDown;
- 155 static int CX, CY;
- 156 static int MemX=800, MemY=600;
- 157
- 158 switch (message)
- 159 {
- 160 case WM_CREATE :
- 161 hMenu = GetMenu(hWnd);
- 162 CheckMenuItem(hMenu, IDM_PENCIL, MF_CHECKED);
- 163 EnableMenuItem(hMenu, IDM_UNDO, MF_GRAYED);
- 164
- 165 memcpy(crPCurColor, crDefColor,
- 166 sizeof(crDefColor));
- 167 memcpy(crBCurColor, crDefColor,
- 168 sizeof(crDefColor));
- 169
- 170 hDC = GetDC(hWnd);
- 171 hMemDC = CreateCompatibleDC(hDC);
- 172 hBitmap = CreateCompatibleBitmap(hDC,
- 173 MemX, MemY);
- 174 SelectObject(hMemDC, hBitmap);
- 175 PatBlt(hMemDC, 0, 0, 800, 600, WHITENESS);
- 176 ReleaseDC(hWnd, hDC);
- 177
- 178 hInst = ((LPCREATESTRUCT) lParam)->hInstance;
- 179 return (0);
- 180
- 181 case WM_COMMAND :
- 182 hMenu = GetMenu(hWnd);
- 183 switch (wParam)
- 184 {
- 185 case IDM_PENCIL :
- 186 case IDM_LINE :
- 187 case IDM_RECT_F :
- 188 case IDM_RECT :
- 189 case IDM_ELLIP_F :
- 190 case IDM_ELLIP :
- 191 case IDM_CIRCLE_F :
- 192 case IDM_CIRCLE :
- 193 case IDM_ROUNDRECT_F:
- 194 case IDM_ROUNDRECT :
- 195
- 196 if (ToolID == wParam)
- 197 return (0);
- 198
- 199 CheckMenuItem(hMenu, ToolID,
- 200 MF_UNCHECKED);
- 201 ToolID = wParam;
- 202 CheckMenuItem(hMenu, ToolID,
- 203 MF_CHECKED);
- 204
- 205 hDC = GetDC(hWnd);
- 206 BitBlt(hMemDC, 0, 0, CX, CY,
- 207 hDC, 0, 0, SRCCOPY);
- 208 ReleaseDC(hWnd, hDC);
- 209
- 210 hMenu = GetMenu(hWnd);
- 211 EnableMenuItem(hMenu, IDM_UNDO,
- 212 MF_GRAYED);
- 213 CanUndo = FALSE;
- 214
- 215 break;
- 216
- 217 case IDM_CHOOSEPEN :
- 218
- 219 lpPenDlgProc = MakeProcInstance(
- 220 (FARPROC) PenDlgProc, hInst);
- 221
- 222 DialogBox(hInst, "PENDLG", hWnd,
- 223 lpPenDlgProc);
- 224
- 225 FreeProcInstance(lpPenDlgProc);
- 226 break;
- 227
- 228 case IDM_CHOOSEBRUSH :
- 229
- 230 lpBrushDlgProc = MakeProcInstance(
- 231 (FARPROC) BrushDlgProc, hInst);
- 232
- 233 DialogBox(hInst, "BRUSHDLG", hWnd,
- 234 lpBrushDlgProc);
- 235
- 236 FreeProcInstance(lpBrushDlgProc);
- 237 break;
- 238
- 239 case IDM_UNDO :
- 240 InvalidateRect(hWnd, NULL, FALSE);
- 241 EnableMenuItem(hMenu, IDM_UNDO,
- 242 MF_GRAYED);
- 243 CanUndo = FALSE;
- 244 break;
- 245
- 246 case IDM_CLEAR :
- 247 PatBlt(hMemDC, 0, 0, MemX, MemY,
- 248 PATCOPY);
- 249 InvalidateRect(hWnd, NULL, TRUE);
- 250 break;
- 251
- 252 case IDM_QUIT :
- 253 DestroyWindow(hWnd);
- 254 break;
- 255 }
- 256 return (0);
- 257
- 258 case WM_LBUTTONDOWN :
- 259 SetCapture(hWnd);
- 260 bLBDown = TRUE;
- 261
- 262 OrgPoint = MAKEPOINT(lParam);
- 263 CurPoint = PrePoint = OrgPoint;
- 264
- 265 return (0);
- 266
- 267 case WM_LBUTTONUP :
- 268 bLBDown = FALSE;
- 269 ReleaseCapture();
- 270
- 271 hDC = GetDC(hWnd);
- 272 DrawGraph(hDC, GetMenu(hWnd), TRUE);
- 273 ReleaseDC(hWnd, hDC);
- 274
- 275 return (0);
- 276
- 277 case WM_MOUSEMOVE :
- 278 if (bLBDown)
- 279 {
- 280 PrePoint = CurPoint;
- 281 CurPoint = MAKEPOINT(lParam);
- 282
- 283 hDC = GetDC(hWnd);
- 284 DrawGraph(hDC, GetMenu(hWnd), FALSE);
- 285 ReleaseDC(hWnd, hDC);
- 286 }
- 287 return (0);
- 288
- 289 case WM_SIZE :
- 290 CX = LOWORD(lParam);
- 291 CY = HIWORD(lParam);
- 292 return (0);
- 293
- 294 case WM_PAINT :
- 295 hDC = BeginPaint(hWnd, &ps);
- 296
- 297 BitBlt(hDC,
- 298 ps.rcPaint.left, ps.rcPaint.top,
- 299 ps.rcPaint.right-ps.rcPaint.left,
- 300 ps.rcPaint.bottom-ps.rcPaint.top,
- 301 hMemDC,
- 302 ps.rcPaint.left, ps.rcPaint.top,
- 303 SRCCOPY);
- 304
- 305 EndPaint(hWnd, &ps);
- 306 return (0);
- 307
- 308 case WM_DESTROY :
- 309 DeleteDC(hMemDC);
- 310 DeleteObject(hBitmap);
- 311 PostQuitMessage(0);
- 312 return (0);
- 313
- 314 case WM_SYSCOMMAND :
- 315
- 316 if ((wParam & 0xfff0) != SC_MOUSEMENU &&
- 317 (wParam & 0xfff0) != SC_KEYMENU)
- 318 {
- 319 hDC = GetDC(hWnd);
- 320 BitBlt(hMemDC, 0, 0, CX, CY,
- 321 hDC, 0, 0, SRCCOPY);
- 322 ReleaseDC(hWnd, hDC);
- 323
- 324 hMenu = GetMenu(hWnd);
- 325 EnableMenuItem(hMenu, IDM_UNDO,
- 326 MF_GRAYED);
- 327 CanUndo = FALSE;
- 328 }
- 329
- 330 default :
- 331 return(DefWindowProc(hWnd, message, wParam, lParam));
- 332 }
- 333 }
- 334
- 335
- 336
- 337 HBRUSH MyCreateBrush(int nHatchStyle, COLORREF crColor)
- 338 {
- 339 HBRUSH hBrush;
- 340
- 341 if (nHatchStyle == -1)
- 342 hBrush = CreateSolidBrush(crColor);
- 343 else
- 344 hBrush = CreateHatchBrush(nHatchStyle, crColor);
- 345
- 346 return (hBrush);
- 347 }
- 348
- 349
- 350
- 351 void DrawGraph(HDC hDC, HMENU hMenu, BOOL bSure)
- 352 {
- 353 HPEN hPen, hPrePen;
- 354 HBRUSH hBrush, hPreBrush;
- 355
- 356 if (ToolID==IDM_PENCIL || bSure)
- 357 {
- 358 hPen = CreatePen(nPenStyle, nPenWidth,
- 359 MKCOLOR(crPCurColor[nPenColor]));
- 360 hPrePen = SelectObject(hDC, hPen);
- 361
- 362 if (ToolID==IDM_RECT_F || ToolID==IDM_ELLIP_F ||
- 363 ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
- 364 {
- 365 hBrush = MyCreateBrush(nHatch,
- 366 MKCOLOR(crBCurColor[nBrushColor]));
- 367 hPreBrush = SelectObject(hDC, hBrush);
- 368 }
- 369 else
- 370 {
- 371 hBrush = GetStockObject(NULL_BRUSH);
- 372 hPreBrush = SelectObject(hDC, hBrush);
- 373 }
- 374 }
- 375 else
- 376 SelectObject(hDC, GetStockObject(NULL_BRUSH));
- 377
- 378 switch (ToolID)
- 379 {
- 380 case IDM_PENCIL :
- 381 DrawPencil(hDC, hMenu);
- 382 break;
- 383
- 384 case IDM_LINE :
- 385 DrawLine(hDC, hMenu, bSure);
- 386 break;
- 387
- 388 case IDM_RECT_F :
- 389 case IDM_RECT :
- 390 DrawRect(hDC, hMenu, bSure);
- 391 break;
- 392
- 393 case IDM_ELLIP_F :
- 394 case IDM_ELLIP :
- 395 DrawEllip(hDC, hMenu, bSure);
- 396 break;
- 397
- 398 case IDM_CIRCLE_F :
- 399 case IDM_CIRCLE :
- 400 DrawCircle(hDC, hMenu, bSure);
- 401 break;
- 402
- 403 case IDM_ROUNDRECT_F :
- 404 case IDM_ROUNDRECT :
- 405 DrawRoundRect(hDC, hMenu, bSure);
- 406 break;
- 407 }
- 408
- 409 if (ToolID==IDM_PENCIL || bSure)
- 410 {
- 411 SelectObject(hDC, hPrePen);
- 412 DeleteObject(hPen);
- 413
- 414 if (ToolID==IDM_RECT_F || ToolID==IDM_ELLIP_F ||
- 415 ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
- 416 {
- 417 SelectObject(hDC, hPreBrush);
- 418 DeleteObject(hBrush);
- 419 }
- 420 else
- 421 {
- 422 SelectObject(hDC, hPreBrush);
- 423 }
- 424 }
- 425 }
- 426
- 427
- 428
- 429 void DrawPencil(HDC hDC, HMENU hMenu)
- 430 {
- 431 MoveTo(hDC, PrePoint.x, PrePoint.y);
- 432 LineTo(hDC, CurPoint.x, CurPoint.y);
- 433
- 434 if (! CanUndo)
- 435 {
- 436 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 437 CanUndo = TRUE;
- 438 }
- 439 }
- 440
- 441
- 442
- 443 void DrawLine(HDC hDC, HMENU hMenu, BOOL bSure)
- 444 {
- 445 int nDrawMode;
- 446
- 447 if (! bSure)
- 448 {
- 449 nDrawMode = SetROP2(hDC, R2_NOT);
- 450
- 451 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 452 LineTo(hDC, PrePoint.x, PrePoint.y);
- 453
- 454 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 455 LineTo(hDC, CurPoint.x, CurPoint.y);
- 456
- 457 SetROP2(hDC, nDrawMode);
- 458 }
- 459 else
- 460 {
- 461 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 462 LineTo(hDC, CurPoint.x, CurPoint.y);
- 463
- 464 if (! CanUndo)
- 465 {
- 466 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 467 CanUndo = TRUE;
- 468 }
- 469 }
- 470 }
- 471
- 472
- 473
- 474 void DrawRect(HDC hDC, HMENU hMenu, BOOL bSure)
- 475 {
- 476 int nDrawMode;
- 477
- 478 if (! bSure)
- 479 {
- 480 nDrawMode = SetROP2(hDC, R2_NOT);
- 481
- 482 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
- 483 PrePoint.x, PrePoint.y);
- 484
- 485 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
- 486 CurPoint.x, CurPoint.y);
- 487
- 488 SetROP2(hDC, nDrawMode);
- 489 }
- 490 else
- 491 {
- 492 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
- 493 CurPoint.x, CurPoint.y);
- 494
- 495 if (! CanUndo)
- 496 {
- 497 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 498 CanUndo = TRUE;
- 499 }
- 500 }
- 501 }
- 502
- 503
- 504
- 505 void DrawEllip(HDC hDC, HMENU hMenu, BOOL bSure)
- 506 {
- 507 int nDrawMode;
- 508
- 509 if (! bSure)
- 510 {
- 511 nDrawMode = SetROP2(hDC, R2_NOT);
- 512
- 513 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 514 PrePoint.x, PrePoint.y);
- 515
- 516 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 517 CurPoint.x, CurPoint.y);
- 518
- 519 SetROP2(hDC, nDrawMode);
- 520 }
- 521 else
- 522 {
- 523 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 524 CurPoint.x, CurPoint.y);
- 525
- 526 if (! CanUndo)
- 527 {
- 528 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 529 CanUndo = TRUE;
- 530 }
- 531 }
- 532 }
- 533
- 534
- 535
- 536 void DrawRoundRect(HDC hDC, HMENU hMenu, BOOL bSure)
- 537 {
- 538 int nDrawMode;
- 539
- 540 if (! bSure)
- 541 {
- 542 nDrawMode = SetROP2(hDC, R2_NOT);
- 543
- 544 RoundRect(hDC, OrgPoint.x, OrgPoint.y,
- 545 PrePoint.x, PrePoint.y,
- 546 (PrePoint.x-OrgPoint.x)/4,
- 547 (PrePoint.y-OrgPoint.y)/4);
- 548
- 549 RoundRect(hDC, OrgPoint.x, OrgPoint.y,
- 550 CurPoint.x, CurPoint.y,
- 551 (CurPoint.x-OrgPoint.x)/4,
- 552 (CurPoint.y-OrgPoint.y)/4);
- 553
- 554 SetROP2(hDC, nDrawMode);
- 555 }
- 556 else
- 557 {
- 558 RoundRect(hDC, OrgPoint.x, OrgPoint.y,
- 559 CurPoint.x, CurPoint.y,
- 560 (CurPoint.x-OrgPoint.x)/4,
- 561 (CurPoint.y-OrgPoint.y)/4);
- 562
- 563 if (! CanUndo)
- 564 {
- 565 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 566 CanUndo = TRUE;
- 567 }
- 568 }
- 569 }
- 570
- 571
- 572
- 573 void DrawCircle(HDC hDC, HMENU hMenu, BOOL bSure)
- 574 {
- 575 int nDrawMode;
- 576 int nLogPixSx, nLogPixSy;
- 577 int Width, Height;
- 578 int SignX, SignY;
- 579
- 580 nLogPixSx = GetDeviceCaps(hDC, LOGPIXELSX);
- 581 nLogPixSy = GetDeviceCaps(hDC, LOGPIXELSY);
- 582
- 583 Width = CurPoint.x - OrgPoint.x;
- 584 Height = CurPoint.y - OrgPoint.y;
- 585 SignX = (Width >= 0 ? 1 : -1);
- 586 SignY = (Height >= 0 ? 1 : -1);
- 587
- 588 if (fabs((float) Width/nLogPixSx) >
- 589 fabs((float) Height/nLogPixSy) )
- 590 {
- 591 CurPoint.x = OrgPoint.x + (float)
- 592 fabs(Height) * nLogPixSx / nLogPixSy * SignX;
- 593 }
- 594 else
- 595 {
- 596 CurPoint.y = OrgPoint.y + (float)
- 597 fabs(Width) * nLogPixSy / nLogPixSx * SignY;
- 598 }
- 599
- 600
- 601 if (! bSure)
- 602 {
- 603 nDrawMode = SetROP2(hDC, R2_NOT);
- 604
- 605 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 606 PrePoint.x, PrePoint.y);
- 607
- 608 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 609 CurPoint.x, CurPoint.y);
- 610
- 611 SetROP2(hDC, nDrawMode);
- 612 }
- 613 else
- 614 {
- 615 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 616 CurPoint.x, CurPoint.y);
- 617
- 618 if (! CanUndo)
- 619 {
- 620 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 621 CanUndo = TRUE;
- 622 }
- 623 }
- 624 }