PAINT.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:31k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Paint --- My Paint */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include <math.h>
- 7 #include "Paint.h"
- 8
- 9 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 10 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 11 long FAR PASCAL ChooseCtrlProc(HWND, unsigned, WORD, LONG);
- 12 long FAR PASCAL LineWSCtrlProc(HWND, unsigned, WORD, LONG);
- 13 BOOL FAR PASCAL PenDlgProc(HWND, unsigned, WORD, LONG);
- 14 BOOL FAR PASCAL BrushDlgProc(HWND, unsigned, WORD, LONG);
- 15
- 16 void BackUpGraph(HDC, HMENU, int, int, int, int, int, int);
- 17
- 18 void DrawGraph(HDC, HMENU, BOOL);
- 19 void DrawPencil(HDC, HMENU);
- 20 void DrawLine(HDC, HMENU, BOOL);
- 21 void DrawRect(HDC, HMENU, BOOL);
- 22 void DrawEllip(HDC, HMENU, BOOL);
- 23 void DrawCircle(HDC, HMENU, BOOL);
- 24 void DrawRoundRect(HDC, HMENU, BOOL);
- 25 void Erase(HDC, HMENU, BOOL);
- 26
- 27 /**** defined in paint_fn.c ****/
- 28 void MakeFontMenu(HWND);
- 29 void MakeStyleMenu(HMENU);
- 30 void DeleteStyleMenu();
- 31 void BeginWrite(HWND, int, int);
- 32 void EndWrite(HWND);
- 33
- 34 /**** defined in paint_pr.c ****/
- 35 void PrintGraph(HWND, int, int);
- 36
- 37 /**** defined in paint_bk.c ****/
- 38 void CancelBound(HDC, HMENU);
- 39 void RecoverBlock();
- 40 void Copy(HWND);
- 41 void Paste(HWND);
- 42 void Cut(HWND);
- 43
- 44 /**** defined in paint_fi.c ****/
- 45 void NewFile(HWND, BOOL);
- 46 void FileOpen(HWND);
- 47 void FileSaveAs(HWND);
- 48 void FileSave(HWND);
- 49
- 50 FARPROC lpPenDlgProc;
- 51 FARPROC lpBrushDlgProc;
- 52
- 53 HANDLE hInst;
- 54
- 55 int OX=0, OY=0;
- 56 int CX, CY;
- 57 int MemX=960, MemY=900;
- 58
- 59 int nLogPixSx;
- 60 int nLogPixSy;
- 61
- 62 int ToolID = IDM_PENCIL;
- 63
- 64 int nPenColor = 1;
- 65 int nPenStyle = PS_SOLID;
- 66 int nPenWidth = 1;
- 67
- 68 int nBrushColor = 1;
- 69 int nHatch = -1;
- 70
- 71 POINT OrgPoint;
- 72 POINT PrePoint;
- 73 POINT CurPoint;
- 74
- 75 BOOL CanUndo;
- 76 HBITMAP hBitmap;
- 77 HDC hMemDC;
- 78
- 79 /**** declared in paint_dg.c ****/
- 80 typedef struct tagCOLORSTRUCT {
- 81 int cR;
- 82 int cG;
- 83 int cB;
- 84 } COLORSTRUCT;
- 85
- 86 #define MKCOLOR(A) (RGB(A.cR, A.cG, A.cB))
- 87
- 88 extern COLORSTRUCT crDefColor[28];
- 89 extern COLORSTRUCT crPCurColor[28];
- 90 extern COLORSTRUCT crBCurColor[28];
- 91
- 92
- 93 /**** declared in paint_fn.c ****/
- 94 extern BOOL bNormal;
- 95 extern BOOL bTextWorking;
- 96 extern int CharX, CharY;
- 97 extern int CharPosX, CharPosY;
- 98 extern BOOL bCaret;
- 99
- 100 /**** declared in paint_bk.c ****/
- 101 extern BOOL bBounded;
- 102
- 103
- 104 /****************************************************************/
- 105 /* WinMain() */
- 106 /****************************************************************/
- 107
- 108 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 109 LPSTR lpszCmdLine, int nCmdShow)
- 110 {
- 111 WNDCLASS wclass;
- 112 MSG msg;
- 113 HWND hWnd;
- 114 char szName[] = "Paint";
- 115
- 116 if (!hPrevInstance)
- 117 {
- 118 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 119 wclass.lpfnWndProc = MainWndProc;
- 120 wclass.cbClsExtra = 0;
- 121 wclass.cbWndExtra = 0;
- 122 wclass.hInstance = hInstance;
- 123 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 124 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 125 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 126 wclass.lpszMenuName = szName;
- 127 wclass.lpszClassName = szName;
- 128
- 129 if (!RegisterClass (&wclass))
- 130 return (FALSE);
- 131
- 132 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 133 wclass.lpfnWndProc = ChooseCtrlProc;
- 134 wclass.cbClsExtra = 0;
- 135 wclass.cbWndExtra = 0;
- 136 wclass.hInstance = hInstance;
- 137 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 138 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 139 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 140 wclass.lpszMenuName = NULL;
- 141 wclass.lpszClassName = "Choose";
- 142
- 143 if (!RegisterClass (&wclass))
- 144 return (FALSE);
- 145
- 146 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 147 wclass.lpfnWndProc = LineWSCtrlProc;
- 148 wclass.cbClsExtra = 0;
- 149 wclass.cbWndExtra = 0;
- 150 wclass.hInstance = hInstance;
- 151 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 152 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 153 wclass.hbrBackground = COLOR_WINDOW + 1;
- 154 wclass.lpszMenuName = NULL;
- 155 wclass.lpszClassName = "LineWS";
- 156
- 157 if (!RegisterClass (&wclass))
- 158 return (FALSE);
- 159 }
- 160
- 161 hWnd = CreateWindow(
- 162 szName,
- 163 "My Paint" ,
- 164 WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
- 165 CW_USEDEFAULT,
- 166 CW_USEDEFAULT,
- 167 CW_USEDEFAULT,
- 168 CW_USEDEFAULT,
- 169 NULL,
- 170 NULL,
- 171 hInstance,
- 172 NULL );
- 173
- 174 if (!hWnd)
- 175 return (FALSE);
- 176
- 177 ShowWindow(hWnd, nCmdShow);
- 178 UpdateWindow(hWnd);
- 179
- 180 while (GetMessage(&msg, NULL, NULL,NULL))
- 181 {
- 182 TranslateMessage(&msg);
- 183 DispatchMessage(&msg);
- 184 }
- 185 return (msg.wParam);
- 186 }
- 187
- 188
- 189 /****************************************************************/
- 190 /* MainWndProc() */
- 191 /****************************************************************/
- 192
- 193 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 194 WORD wParam, LONG lParam)
- 195 {
- 196 HDC hDC;
- 197 HMENU hMenu;
- 198 PAINTSTRUCT ps;
- 199 int Temp;
- 200 static BOOL bLBDown;
- 201
- 202 switch (message)
- 203 {
- 204 case WM_CREATE :
- 205 hInst =
- 206 ((LPCREATESTRUCT) lParam)->hInstance;
- 207
- 208 /**** Process normal menu ****/
- 209
- 210 hMenu = GetMenu(hWnd);
- 211 CheckMenuItem(hMenu, IDM_PENCIL,
- 212 MF_CHECKED);
- 213 EnableMenuItem(hMenu, IDM_UNDO, MF_GRAYED);
- 214 EnableMenuItem(hMenu, IDM_COPY, MF_GRAYED);
- 215 EnableMenuItem(hMenu, IDM_PASTE, MF_GRAYED);
- 216 EnableMenuItem(hMenu, IDM_CUT, MF_GRAYED);
- 217
- 218 /**** Make copy of 28 color value ****/
- 219
- 220 memcpy(crPCurColor, crDefColor,
- 221 sizeof(crDefColor));
- 222 memcpy(crBCurColor, crDefColor,
- 223 sizeof(crDefColor));
- 224
- 225 /*** Create bitmap & MemDC for backup ***/
- 226
- 227 hDC = GetDC(hWnd);
- 228 hMemDC = CreateCompatibleDC(hDC);
- 229 hBitmap = CreateCompatibleBitmap(hDC,
- 230 MemX, MemY);
- 231 SelectObject(hMemDC, hBitmap);
- 232
- 233 NewFile(hWnd, 1);
- 234
- 235 /**** Create Font menu ****/
- 236
- 237 nLogPixSx = GetDeviceCaps(hDC, LOGPIXELSX);
- 238 nLogPixSy = GetDeviceCaps(hDC, LOGPIXELSY);
- 239 ReleaseDC(hWnd, hDC);
- 240
- 241 MakeStyleMenu(hMenu);
- 242 MakeFontMenu(hWnd);
- 243 bNormal = FALSE;
- 244 SendMessage(hWnd, WM_COMMAND, IDM_NORM, 0L);
- 245
- 246 return (0);
- 247
- 248 case WM_COMMAND :
- 249 hMenu = GetMenu(hWnd);
- 250
- 251 if (wParam>=IDM_NORM &&
- 252 wParam<=IDM_SIZE+MAXFACES+MAXSIZES)
- 253 ChooseFontMenu(hWnd, wParam);
- 254 else
- 255 switch (wParam)
- 256 {
- 257 case IDM_PENCIL :
- 258 case IDM_LINE :
- 259
- 260 case IDM_BLOCK :
- 261 case IDM_TEXT :
- 262 case IDM_ERASE :
- 263
- 264 case IDM_RECT :
- 265 case IDM_ELLIP :
- 266 case IDM_CIRCLE :
- 267 case IDM_ROUNDRECT :
- 268
- 269 case IDM_RECT_F :
- 270 case IDM_ELLIP_F :
- 271 case IDM_CIRCLE_F :
- 272 case IDM_ROUNDRECT_F:
- 273
- 274 if (ToolID == wParam)
- 275 return (0);
- 276
- 277 if (ToolID == IDM_TEXT)
- 278 EndWrite(hWnd);
- 279
- 280 hDC = GetDC(hWnd);
- 281 BackUpGraph(hDC, hMenu,
- 282 OX, OY, CX, CY,
- 283 0, 0);
- 284 ReleaseDC(hWnd, hDC);
- 285
- 286 CheckMenuItem(hMenu, ToolID,
- 287 MF_UNCHECKED);
- 288 ToolID = wParam;
- 289 CheckMenuItem(hMenu, ToolID,
- 290 MF_CHECKED);
- 291 break;
- 292
- 293 case IDM_CHOOSEPEN :
- 294
- 295 lpPenDlgProc = MakeProcInstance(
- 296 (FARPROC) PenDlgProc, hInst);
- 297
- 298 DialogBox(hInst, "PENDLG", hWnd,
- 299 lpPenDlgProc);
- 300
- 301 FreeProcInstance(lpPenDlgProc);
- 302 break;
- 303
- 304 case IDM_CHOOSEBRUSH :
- 305
- 306 lpBrushDlgProc = MakeProcInstance(
- 307 (FARPROC) BrushDlgProc, hInst);
- 308
- 309 DialogBox(hInst, "BRUSHDLG", hWnd,
- 310 lpBrushDlgProc);
- 311
- 312 FreeProcInstance(lpBrushDlgProc);
- 313 break;
- 314
- 315 case IDM_NEW :
- 316 if (bTextWorking)
- 317 EndWrite(hWnd);
- 318 NewFile(hWnd, 0);
- 319 InvalidateRect(hWnd, NULL, FALSE);
- 320 break;
- 321
- 322 case IDM_OPEN :
- 323 if (bTextWorking)
- 324 EndWrite(hWnd);
- 325 FileOpen(hWnd);
- 326 break;
- 327
- 328 case IDM_SAVE :
- 329 hDC = GetDC(hWnd);
- 330 BackUpGraph(hDC, hMenu,
- 331 OX, OY, CX, CY,
- 332 0, 0);
- 333 ReleaseDC(hWnd, hDC);
- 334
- 335 FileSave(hWnd);
- 336 break;
- 337
- 338 case IDM_SAVEAS :
- 339 hDC = GetDC(hWnd);
- 340 BackUpGraph(hDC, hMenu,
- 341 OX, OY, CX, CY,
- 342 0, 0);
- 343 ReleaseDC(hWnd, hDC);
- 344
- 345 FileSaveAs(hWnd);
- 346 break;
- 347
- 348 case IDM_PRINT :
- 349 if (bTextWorking)
- 350 EndWrite(hWnd);
- 351
- 352 hDC = GetDC(hWnd);
- 353 BackUpGraph(hDC, hMenu,
- 354 OX, OY, CX, CY,
- 355 0, 0);
- 356 ReleaseDC(hWnd, hDC);
- 357
- 358 PrintGraph(hWnd, MemX, MemY);
- 359 break;
- 360
- 361 case IDM_QUIT :
- 362 SendMessage(hWnd, WM_CLOSE, 0, 0);
- 363 break;
- 364
- 365 case IDM_UNDO :
- 366 if (ToolID == IDM_BLOCK)
- 367 {
- 368 hDC = GetDC(hWnd);
- 369 RecoverBlock();
- 370 CancelBound(hDC, hMenu);
- 371 ReleaseDC(hWnd, hDC);
- 372 }
- 373 if (bTextWorking)
- 374 EndWrite(hWnd);
- 375
- 376 InvalidateRect(hWnd, NULL, FALSE);
- 377 EnableMenuItem(hMenu, IDM_UNDO,
- 378 MF_GRAYED);
- 379 CanUndo = FALSE;
- 380 break;
- 381
- 382 case IDM_COPY :
- 383 if (ToolID == IDM_BLOCK)
- 384 Copy(hWnd);
- 385 break;
- 386
- 387 case IDM_PASTE :
- 388 Paste(hWnd);
- 389 break;
- 390
- 391 case IDM_CUT :
- 392 if (ToolID == IDM_BLOCK)
- 393 Cut(hWnd);
- 394 break;
- 395 }
- 396 return (0);
- 397
- 398 case WM_INITMENUPOPUP :
- 399 if (LOWORD(lParam)==1 &&
- 400 HIWORD(lParam)==0)
- 401 {
- 402 if (IsClipboardFormatAvailable(
- 403 CF_BITMAP))
- 404 EnableMenuItem(wParam, IDM_PASTE,
- 405 MF_ENABLED);
- 406 else
- 407 EnableMenuItem(wParam, IDM_PASTE,
- 408 MF_GRAYED);
- 409 }
- 410
- 411 return (0);
- 412
- 413 case WM_LBUTTONDOWN :
- 414 if (ToolID == IDM_TEXT)
- 415 {
- 416 BeginWrite(hWnd,
- 417 LOWORD(lParam),
- 418 HIWORD(lParam));
- 419 return (0);
- 420 }
- 421
- 422 if (bLBDown == TRUE)
- 423 return (0);
- 424
- 425 SetCapture(hWnd);
- 426 bLBDown = TRUE;
- 427
- 428 OrgPoint = MAKEPOINT(lParam);
- 429 CurPoint = PrePoint = OrgPoint;
- 430
- 431 hDC = GetDC(hWnd);
- 432 DrawGraph(hDC, GetMenu(hWnd), FALSE);
- 433 ReleaseDC(hWnd, hDC);
- 434
- 435 return (0);
- 436
- 437 case WM_LBUTTONUP :
- 438 if (bLBDown == FALSE)
- 439 return (0);
- 440 bLBDown = FALSE;
- 441 ReleaseCapture();
- 442
- 443 hDC = GetDC(hWnd);
- 444 DrawGraph(hDC, GetMenu(hWnd), TRUE);
- 445 ReleaseDC(hWnd, hDC);
- 446
- 447 return (0);
- 448
- 449 case WM_MOUSEMOVE :
- 450 if (bLBDown)
- 451 {
- 452 PrePoint = CurPoint;
- 453 CurPoint = MAKEPOINT(lParam);
- 454
- 455 hDC = GetDC(hWnd);
- 456 DrawGraph(hDC, GetMenu(hWnd), FALSE);
- 457 ReleaseDC(hWnd, hDC);
- 458 }
- 459 return (0);
- 460
- 461 case WM_CHAR :
- 462 if (bTextWorking)
- 463 ProcessChar(hWnd, wParam);
- 464 return (0);
- 465
- 466 case WM_SIZE :
- 467 CX = LOWORD(lParam);
- 468 CY = HIWORD(lParam);
- 469 SetScrollRange(hWnd, SB_HORZ,
- 470 0, MemX-CX, TRUE);
- 471 SetScrollRange(hWnd, SB_VERT,
- 472 0, MemY-CY, TRUE);
- 473 OX = min(OX, MemX-CX);
- 474 OY = min(OY, MemY-CY);
- 475 SetScrollPos(hWnd, SB_HORZ, OX, TRUE);
- 476 SetScrollPos(hWnd, SB_VERT, OY, TRUE);
- 477 return (0);
- 478
- 479 case WM_PAINT :
- 480 hDC = BeginPaint(hWnd, &ps);
- 481
- 482 BitBlt(hDC,
- 483 ps.rcPaint.left,
- 484 ps.rcPaint.top,
- 485 ps.rcPaint.right-ps.rcPaint.left,
- 486 ps.rcPaint.bottom-ps.rcPaint.top,
- 487 hMemDC,
- 488 OX+ps.rcPaint.left,
- 489 OY+ps.rcPaint.top,
- 490 SRCCOPY);
- 491
- 492 EndPaint(hWnd, &ps);
- 493 return (0);
- 494
- 495 case WM_HSCROLL :
- 496 switch (wParam)
- 497 {
- 498 case SB_TOP :
- 499 Temp = 0;
- 500 break;
- 501
- 502 case SB_BOTTOM :
- 503 Temp = MemX - CX;
- 504 break;
- 505
- 506 case SB_LINEUP :
- 507 Temp = OX - 20;
- 508 break;
- 509
- 510 case SB_PAGEUP :
- 511 Temp = OX - CX;
- 512 break;
- 513
- 514 case SB_LINEDOWN :
- 515 Temp = OX + 20;
- 516 break;
- 517
- 518 case SB_PAGEDOWN :
- 519 Temp = OX + CX;
- 520 break;
- 521
- 522 case SB_THUMBPOSITION :
- 523 Temp = LOWORD(lParam);
- 524 break;
- 525
- 526 default :
- 527 return (NULL);
- 528 }
- 529 Temp = min(max(0, Temp), MemX-CX);
- 530 if (Temp == OX) return (0);
- 531
- 532 OX = Temp;
- 533 SetScrollPos(hWnd, SB_HORZ, OX, TRUE);
- 534 InvalidateRect(hWnd, NULL, TRUE);
- 535
- 536 return (0);
- 537
- 538 case WM_VSCROLL :
- 539 switch (wParam)
- 540 {
- 541 case SB_TOP :
- 542 Temp = 0;
- 543 break;
- 544
- 545 case SB_BOTTOM :
- 546 Temp = MemY - CY;
- 547 break;
- 548
- 549 case SB_LINEUP :
- 550 Temp = OY - 20;
- 551 break;
- 552
- 553 case SB_PAGEUP :
- 554 Temp = OY - CY;
- 555 break;
- 556
- 557 case SB_LINEDOWN :
- 558 Temp = OY + 20;
- 559 break;
- 560
- 561 case SB_PAGEDOWN :
- 562 Temp = OY + CY;
- 563 break;
- 564
- 565 case SB_THUMBPOSITION :
- 566 Temp = LOWORD(lParam);
- 567 break;
- 568
- 569 default :
- 570 return (NULL);
- 571 }
- 572 Temp = min(max(0, Temp), MemY-CY);
- 573 if (Temp == OY) return (0);
- 574
- 575 OY = Temp;
- 576 SetScrollPos(hWnd, SB_VERT, OY, TRUE);
- 577 InvalidateRect(hWnd, NULL, TRUE);
- 578
- 579 return (0);
- 580
- 581 case WM_SETFOCUS :
- 582 if (bTextWorking)
- 583 {
- 584 CreateCaret(hWnd, NULL, 1, CharY);
- 585 SetCaretPos(CharPosX, CharPosY);
- 586 ShowCaret(hWnd);
- 587 bCaret = TRUE;
- 588 }
- 589 return (0);
- 590
- 591 case WM_KILLFOCUS :
- 592 if (bTextWorking)
- 593 {
- 594 HideCaret(hWnd);
- 595 DestroyCaret();
- 596 bCaret = FALSE;
- 597 }
- 598 return (0);
- 599
- 600 case WM_ACTIVATE :
- 601 if (wParam != 0)
- 602 break;
- 603 // fall through
- 604 case WM_SYSCOMMAND :
- 605
- 606 if (message==WM_ACTIVATE ||
- 607 ((wParam & 0xfff0) != SC_MOUSEMENU &&
- 608 (wParam & 0xfff0) != SC_KEYMENU))
- 609 {
- 610 if (bTextWorking)
- 611 EndWrite(hWnd);
- 612
- 613 hMenu = GetMenu(hWnd);
- 614
- 615 hDC = GetDC(hWnd);
- 616 BackUpGraph(hDC, hMenu,
- 617 OX, OY, CX, CY,
- 618 0, 0);
- 619 ReleaseDC(hWnd, hDC);
- 620 }
- 621 break;
- 622
- 623 case WM_CLOSE :
- 624 if (bTextWorking)
- 625 EndWrite(hWnd);
- 626
- 627 if (QuerySave(hWnd) == IDCANCEL)
- 628 return (0);
- 629 break;
- 630
- 631 case WM_DESTROY :
- 632 DeleteDC(hMemDC);
- 633 DeleteObject(hBitmap);
- 634 DeleteStyleMenu();
- 635 PostQuitMessage(0);
- 636 return (0);
- 637 }
- 638 return(DefWindowProc(hWnd, message, wParam, lParam));
- 639 }
- 640
- 641
- 642
- 643 void BackUpGraph(HDC hDC, HMENU hMenu,
- 644 int OX, int OY, int W, int H,
- 645 int X, int Y)
- 646 {
- 647 if (ToolID==IDM_BLOCK)
- 648 CancelBound(hDC, hMenu);
- 649
- 650 if (CanUndo)
- 651 {
- 652 BitBlt(hMemDC, OX, OY, W, H,
- 653 hDC, X, Y, SRCCOPY);
- 654
- 655 EnableMenuItem(hMenu, IDM_UNDO, MF_GRAYED);
- 656 CanUndo = FALSE;
- 657 }
- 658 }
- 659
- 660
- 661
- 662 HBRUSH MyCreateBrush(int nHatchStyle, COLORREF crColor)
- 663 {
- 664 HBRUSH hBrush;
- 665
- 666 if (nHatchStyle == -1)
- 667 hBrush = CreateSolidBrush(crColor);
- 668 else
- 669 hBrush = CreateHatchBrush(nHatchStyle, crColor);
- 670
- 671 return (hBrush);
- 672 }
- 673
- 674
- 675
- 676 void DrawGraph(HDC hDC, HMENU hMenu, BOOL bSure)
- 677 {
- 678 HPEN hPen, hPrePen;
- 679 HBRUSH hBrush, hPreBrush;
- 680
- 681 if (ToolID==IDM_PENCIL || bSure)
- 682 {
- 683 hPen = CreatePen(nPenStyle, nPenWidth,
- 684 MKCOLOR(crPCurColor[nPenColor]));
- 685 hPrePen = SelectObject(hDC, hPen);
- 686
- 687 if (ToolID==IDM_RECT_F || ToolID==IDM_ELLIP_F ||
- 688 ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
- 689 {
- 690 hBrush = MyCreateBrush(nHatch,
- 691 MKCOLOR(crBCurColor[nBrushColor]));
- 692 hPreBrush = SelectObject(hDC, hBrush);
- 693 }
- 694 else
- 695 {
- 696 hBrush = GetStockObject(NULL_BRUSH);
- 697 hPreBrush = SelectObject(hDC, hBrush);
- 698 }
- 699 }
- 700 else
- 701 SelectObject(hDC, GetStockObject(NULL_BRUSH));
- 702
- 703 switch (ToolID)
- 704 {
- 705 case IDM_PENCIL :
- 706 DrawPencil(hDC, hMenu);
- 707 break;
- 708
- 709 case IDM_LINE :
- 710 DrawLine(hDC, hMenu, bSure);
- 711 break;
- 712
- 713 case IDM_BLOCK :
- 714 BoundBlock(hDC, hMenu, bSure);
- 715 break;
- 716
- 717 case IDM_ERASE :
- 718 Erase(hDC, hMenu, bSure);
- 719 break;
- 720
- 721 case IDM_RECT_F :
- 722 case IDM_RECT :
- 723 DrawRect(hDC, hMenu, bSure);
- 724 break;
- 725
- 726 case IDM_ELLIP_F :
- 727 case IDM_ELLIP :
- 728 DrawEllip(hDC, hMenu, bSure);
- 729 break;
- 730
- 731 case IDM_CIRCLE_F :
- 732 case IDM_CIRCLE :
- 733 DrawCircle(hDC, hMenu, bSure);
- 734 break;
- 735
- 736 case IDM_ROUNDRECT_F :
- 737 case IDM_ROUNDRECT :
- 738 DrawRoundRect(hDC, hMenu, bSure);
- 739 break;
- 740 }
- 741
- 742 if (ToolID==IDM_PENCIL || bSure)
- 743 {
- 744 SelectObject(hDC, hPrePen);
- 745 DeleteObject(hPen);
- 746
- 747 if (ToolID==IDM_RECT_F || ToolID==IDM_ELLIP_F ||
- 748 ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
- 749 {
- 750 SelectObject(hDC, hPreBrush);
- 751 DeleteObject(hBrush);
- 752 }
- 753 else
- 754 {
- 755 SelectObject(hDC, hPreBrush);
- 756 }
- 757 }
- 758 }
- 759
- 760
- 761
- 762 void DrawPencil(HDC hDC, HMENU hMenu)
- 763 {
- 764 MoveTo(hDC, PrePoint.x, PrePoint.y);
- 765 LineTo(hDC, CurPoint.x, CurPoint.y);
- 766
- 767 if (! CanUndo)
- 768 {
- 769 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 770 CanUndo = TRUE;
- 771 }
- 772 }
- 773
- 774
- 775
- 776 void DrawLine(HDC hDC, HMENU hMenu, BOOL bSure)
- 777 {
- 778 int nDrawMode;
- 779
- 780 if (! bSure)
- 781 {
- 782 nDrawMode = SetROP2(hDC, R2_NOT);
- 783
- 784 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 785 LineTo(hDC, PrePoint.x, PrePoint.y);
- 786
- 787 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 788 LineTo(hDC, CurPoint.x, CurPoint.y);
- 789
- 790 SetROP2(hDC, nDrawMode);
- 791 }
- 792 else
- 793 {
- 794 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 795 LineTo(hDC, CurPoint.x, CurPoint.y);
- 796
- 797 if (! CanUndo)
- 798 {
- 799 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 800 CanUndo = TRUE;
- 801 }
- 802 }
- 803 }
- 804
- 805
- 806
- 807 void Erase(HDC hDC, HMENU hMenu, BOOL bSure)
- 808 {
- 809 int nDrawMode;
- 810
- 811 if (! bSure)
- 812 {
- 813 nDrawMode = SetROP2(hDC, R2_NOT);
- 814
- 815 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
- 816 PrePoint.x, PrePoint.y);
- 817
- 818 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
- 819 CurPoint.x, CurPoint.y);
- 820
- 821 SetROP2(hDC, nDrawMode);
- 822 }
- 823 else
- 824 {
- 825 PatBlt(hDC, OrgPoint.x, OrgPoint.y,
- 826 CurPoint.x-OrgPoint.x,
- 827 CurPoint.y-OrgPoint.y,
- 828 WHITENESS);
- 829
- 830 if (! CanUndo)
- 831 {
- 832 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 833 CanUndo = TRUE;
- 834 }
- 835 }
- 836 }
- 837
- 838
- 839
- 840 void DrawRect(HDC hDC, HMENU hMenu, BOOL bSure)
- 841 {
- 842 int nDrawMode;
- 843
- 844 if (! bSure)
- 845 {
- 846 nDrawMode = SetROP2(hDC, R2_NOT);
- 847
- 848 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
- 849 PrePoint.x, PrePoint.y);
- 850
- 851 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
- 852 CurPoint.x, CurPoint.y);
- 853
- 854 SetROP2(hDC, nDrawMode);
- 855 }
- 856 else
- 857 {
- 858 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
- 859 CurPoint.x, CurPoint.y);
- 860
- 861 if (! CanUndo)
- 862 {
- 863 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 864 CanUndo = TRUE;
- 865 }
- 866 }
- 867 }
- 868
- 869
- 870
- 871 void DrawEllip(HDC hDC, HMENU hMenu, BOOL bSure)
- 872 {
- 873 int nDrawMode;
- 874
- 875 if (! bSure)
- 876 {
- 877 nDrawMode = SetROP2(hDC, R2_NOT);
- 878
- 879 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 880 PrePoint.x, PrePoint.y);
- 881
- 882 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 883 CurPoint.x, CurPoint.y);
- 884
- 885 SetROP2(hDC, nDrawMode);
- 886 }
- 887 else
- 888 {
- 889 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 890 CurPoint.x, CurPoint.y);
- 891
- 892 if (! CanUndo)
- 893 {
- 894 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 895 CanUndo = TRUE;
- 896 }
- 897 }
- 898 }
- 899
- 900
- 901
- 902 void DrawRoundRect(HDC hDC, HMENU hMenu, BOOL bSure)
- 903 {
- 904 int nDrawMode;
- 905
- 906 if (! bSure)
- 907 {
- 908 nDrawMode = SetROP2(hDC, R2_NOT);
- 909
- 910 RoundRect(hDC, OrgPoint.x, OrgPoint.y,
- 911 PrePoint.x, PrePoint.y,
- 912 (PrePoint.x-OrgPoint.x)/4,
- 913 (PrePoint.y-OrgPoint.y)/4);
- 914
- 915 RoundRect(hDC, OrgPoint.x, OrgPoint.y,
- 916 CurPoint.x, CurPoint.y,
- 917 (CurPoint.x-OrgPoint.x)/4,
- 918 (CurPoint.y-OrgPoint.y)/4);
- 919
- 920 SetROP2(hDC, nDrawMode);
- 921 }
- 922 else
- 923 {
- 924 RoundRect(hDC, OrgPoint.x, OrgPoint.y,
- 925 CurPoint.x, CurPoint.y,
- 926 (CurPoint.x-OrgPoint.x)/4,
- 927 (CurPoint.y-OrgPoint.y)/4);
- 928
- 929 if (! CanUndo)
- 930 {
- 931 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 932 CanUndo = TRUE;
- 933 }
- 934 }
- 935 }
- 936
- 937
- 938
- 939 void DrawCircle(HDC hDC, HMENU hMenu, BOOL bSure)
- 940 {
- 941 int nDrawMode;
- 942 int nLogPixSx, nLogPixSy;
- 943 int Width, Height;
- 944 int SignX, SignY;
- 945
- 946 nLogPixSx = GetDeviceCaps(hDC, LOGPIXELSX);
- 947 nLogPixSy = GetDeviceCaps(hDC, LOGPIXELSY);
- 948
- 949 Width = CurPoint.x - OrgPoint.x;
- 950 Height = CurPoint.y - OrgPoint.y;
- 951 SignX = (Width >= 0 ? 1 : -1);
- 952 SignY = (Height >= 0 ? 1 : -1);
- 953
- 954 if (fabs((float) Width/nLogPixSx) >
- 955 fabs((float) Height/nLogPixSy) )
- 956 {
- 957 CurPoint.x = OrgPoint.x + (float)
- 958 fabs(Height) * nLogPixSx / nLogPixSy * SignX;
- 959 }
- 960 else
- 961 {
- 962 CurPoint.y = OrgPoint.y + (float)
- 963 fabs(Width) * nLogPixSy / nLogPixSx * SignY;
- 964 }
- 965
- 966
- 967 if (! bSure)
- 968 {
- 969 nDrawMode = SetROP2(hDC, R2_NOT);
- 970
- 971 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 972 PrePoint.x, PrePoint.y);
- 973
- 974 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 975 CurPoint.x, CurPoint.y);
- 976
- 977 SetROP2(hDC, nDrawMode);
- 978 }
- 979 else
- 980 {
- 981 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 982 CurPoint.x, CurPoint.y);
- 983
- 984 if (! CanUndo)
- 985 {
- 986 EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
- 987 CanUndo = TRUE;
- 988 }
- 989 }
- 990 }