DEMO3_3.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:21k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo3_3 --- Mapping Mode test */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include "demo3_3.h"
- 7
- 8 #define MAXPOINTS 100
- 9
- 10 typedef struct tagSHAPE {
- 11 int x1, y1;
- 12 int x2, y2;
- 13 int ShapeID;
- 14 int MapModeID;
- 15 } SHAPE;
- 16
- 17 static RECT LineRect;
- 18 static RECT RectRect;
- 19 static RECT EllipRect;
- 20
- 21
- 22 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 23 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 24 BOOL FAR PASCAL LineDlgProc(HWND, unsigned, WORD, LONG);
- 25 BOOL FAR PASCAL RectDlgProc(HWND, unsigned, WORD, LONG);
- 26 BOOL FAR PASCAL EllipDlgProc(HWND, unsigned, WORD, LONG);
- 27
- 28 FARPROC lpLineDlgProc;
- 29 FARPROC lpRectDlgProc;
- 30 FARPROC lpEllipDlgProc;
- 31
- 32 void DrawAllGraph(HDC, RECT, SHAPE *, int);
- 33
- 34
- 35
- 36 /****************************************************************/
- 37 /* WinMain() */
- 38 /****************************************************************/
- 39
- 40 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 41 LPSTR lpszCmdLine, int nCmdShow)
- 42 {
- 43 WNDCLASS wclass;
- 44 MSG msg;
- 45 HWND hWnd;
- 46 char szName[] = "Demo3_3";
- 47
- 48 if (!hPrevInstance)
- 49 {
- 50 wclass.style = NULL;
- 51 wclass.lpfnWndProc = MainWndProc;
- 52 wclass.cbClsExtra = 0;
- 53 wclass.cbWndExtra = 0;
- 54 wclass.hInstance = hInstance;
- 55 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 56 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 57 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 58 wclass.lpszMenuName = szName;
- 59 wclass.lpszClassName = szName;
- 60
- 61 if (!RegisterClass (&wclass))
- 62 return (FALSE);
- 63 }
- 64
- 65 hWnd = CreateWindow(
- 66 szName,
- 67 "Mapping Mode test",
- 68 WS_OVERLAPPEDWINDOW,
- 69 CW_USEDEFAULT,
- 70 CW_USEDEFAULT,
- 71 CW_USEDEFAULT,
- 72 CW_USEDEFAULT,
- 73 NULL,
- 74 NULL,
- 75 hInstance,
- 76 NULL );
- 77
- 78 if (!hWnd)
- 79 return (FALSE);
- 80
- 81 ShowWindow(hWnd, nCmdShow);
- 82 UpdateWindow(hWnd);
- 83
- 84 while (GetMessage(&msg, NULL, NULL,NULL))
- 85 {
- 86 TranslateMessage(&msg);
- 87 DispatchMessage(&msg);
- 88 }
- 89 return (msg.wParam);
- 90 }
- 91
- 92
- 93
- 94 /****************************************************************/
- 95 /* MainWndProc() */
- 96 /****************************************************************/
- 97
- 98 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 99 WORD wParam, LONG lParam)
- 100 {
- 101 PAINTSTRUCT ps;
- 102 HDC hDC;
- 103 HMENU hMenu;
- 104 static int MapModeID = IDM_TEXT;
- 105 static int ShapeID = IDM_LINE;
- 106 int i;
- 107 static int Num = 0;
- 108 static SHAPE Shape[MAXPOINTS];
- 109 static HANDLE hInst;
- 110 HANDLE hBrush;
- 111
- 112 switch (message)
- 113 {
- 114 case WM_CREATE :
- 115 hMenu = GetMenu(hWnd);
- 116 CheckMenuItem(hMenu, MapModeID, MF_CHECKED);
- 117 CheckMenuItem(hMenu, ShapeID, MF_CHECKED);
- 118
- 119 SetRect(&LineRect, 0, 0, 0, 0);
- 120 SetRect(&RectRect, 0, 0, 0, 0);
- 121 SetRect(&EllipRect, 0, 0, 0, 0);
- 122 hInst = ((LPCREATESTRUCT)lParam)->hInstance;
- 123 return (0);
- 124
- 125 case WM_COMMAND :
- 126 switch(wParam)
- 127 {
- 128 case IDM_TEXT :
- 129 case IDM_LOMET :
- 130 case IDM_HIMET :
- 131 case IDM_LOENG :
- 132 case IDM_HIENG :
- 133 case IDM_TWIPS :
- 134
- 135 if (MapModeID != wParam)
- 136 {
- 137 hMenu = GetMenu(hWnd);
- 138 CheckMenuItem(hMenu, MapModeID,
- 139 MF_UNCHECKED);
- 140 MapModeID = wParam;
- 141 CheckMenuItem(hMenu, wParam,
- 142 MF_CHECKED);
- 143 }
- 144 break;
- 145
- 146 case IDM_LINE :
- 147 if (ShapeID != wParam)
- 148 {
- 149 hMenu = GetMenu(hWnd);
- 150 CheckMenuItem(hMenu, ShapeID,
- 151 MF_UNCHECKED);
- 152 ShapeID = wParam;
- 153 CheckMenuItem(hMenu, wParam,
- 154 MF_CHECKED);
- 155 }
- 156
- 157 lpLineDlgProc = MakeProcInstance(
- 158 (FARPROC) LineDlgProc, hInst);
- 159
- 160 DialogBox(hInst, "LINEDLG", hWnd,
- 161 lpLineDlgProc);
- 162
- 163 FreeProcInstance(lpLineDlgProc);
- 164 break;
- 165
- 166 case IDM_RECT :
- 167 if (ShapeID != wParam)
- 168 {
- 169 hMenu = GetMenu(hWnd);
- 170 CheckMenuItem(hMenu, ShapeID,
- 171 MF_UNCHECKED);
- 172 ShapeID = wParam;
- 173 CheckMenuItem(hMenu, wParam,
- 174 MF_CHECKED);
- 175 }
- 176
- 177 lpRectDlgProc = MakeProcInstance(
- 178 (FARPROC) RectDlgProc, hInst);
- 179
- 180 DialogBox(hInst, "RECTDLG", hWnd,
- 181 lpRectDlgProc);
- 182
- 183 FreeProcInstance(lpRectDlgProc);
- 184 break;
- 185
- 186 case IDM_ELLIP :
- 187
- 188 if (ShapeID != wParam)
- 189 {
- 190 hMenu = GetMenu(hWnd);
- 191 CheckMenuItem(hMenu, ShapeID,
- 192 MF_UNCHECKED);
- 193 ShapeID = wParam;
- 194 CheckMenuItem(hMenu, wParam,
- 195 MF_CHECKED);
- 196 }
- 197
- 198 lpEllipDlgProc = MakeProcInstance(
- 199 (FARPROC) EllipDlgProc, hInst);
- 200
- 201 DialogBox(hInst, "ELLIPDLG", hWnd,
- 202 lpEllipDlgProc);
- 203
- 204 FreeProcInstance(lpEllipDlgProc);
- 205 break;
- 206
- 207 case IDM_DRAW :
- 208 if (Num == MAXPOINTS)
- 209 {
- 210 MessageBox(hWnd,
- 211 "Memory not enough", "Warning",
- 212 MB_OK | MB_ICONEXCLAMATION);
- 213 break;
- 214 }
- 215
- 216 hDC = GetDC(hWnd);
- 217
- 218 switch (MapModeID)
- 219 {
- 220 case IDM_TEXT :
- 221 SetMapMode(hDC, MM_TEXT);
- 222 break;
- 223
- 224 case IDM_LOMET :
- 225 SetMapMode(hDC, MM_LOMETRIC);
- 226 break;
- 227
- 228 case IDM_HIMET :
- 229 SetMapMode(hDC, MM_HIMETRIC);
- 230 break;
- 231
- 232 case IDM_LOENG :
- 233 SetMapMode(hDC, MM_LOENGLISH);
- 234 break;
- 235
- 236 case IDM_HIENG :
- 237 SetMapMode(hDC, MM_HIENGLISH);
- 238 break;
- 239
- 240 case IDM_TWIPS :
- 241 SetMapMode(hDC, MM_TWIPS);
- 242 break;
- 243 }
- 244
- 245 switch (ShapeID)
- 246 {
- 247 case IDM_LINE :
- 248 MoveTo(hDC, LineRect.left,
- 249 LineRect.top);
- 250 LineTo(hDC, LineRect.right,
- 251 LineRect.bottom);
- 252 Shape[Num].x1 =
- 253 LineRect.left;
- 254 Shape[Num].y1 =
- 255 LineRect.top;
- 256 Shape[Num].x2 =
- 257 LineRect.right;
- 258 Shape[Num].y2 =
- 259 LineRect.bottom;
- 260 break;
- 261
- 262 case IDM_RECT :
- 263 hBrush = GetStockObject(
- 264 NULL_BRUSH);
- 265 SelectObject(hDC, hBrush);
- 266 Rectangle(hDC, RectRect.left,
- 267 RectRect.top,
- 268 RectRect.right,
- 269 RectRect.bottom);
- 270 Shape[Num].x1 =
- 271 RectRect.left;
- 272 Shape[Num].y1 =
- 273 RectRect.top;
- 274 Shape[Num].x2 =
- 275 RectRect.right;
- 276 Shape[Num].y2 =
- 277 RectRect.bottom;
- 278 break;
- 279
- 280 case IDM_ELLIP :
- 281 hBrush = GetStockObject(
- 282 NULL_BRUSH);
- 283 SelectObject(hDC, hBrush);
- 284 Ellipse(hDC, EllipRect.left,
- 285 EllipRect.top,
- 286 EllipRect.right,
- 287 EllipRect.bottom);
- 288 Shape[Num].x1 =
- 289 EllipRect.left;
- 290 Shape[Num].y1 =
- 291 EllipRect.top;
- 292 Shape[Num].x2 =
- 293 EllipRect.right;
- 294 Shape[Num].y2 =
- 295 EllipRect.bottom;
- 296 break;
- 297 }
- 298
- 299 ReleaseDC(hWnd, hDC);
- 300
- 301 Shape[Num].ShapeID = ShapeID;
- 302 Shape[Num].MapModeID = MapModeID;
- 303 Num ++;
- 304 break;
- 305
- 306 case IDM_CLEAR :
- 307 Num = 0;
- 308 InvalidateRect(hWnd, NULL, TRUE);
- 309 UpdateWindow(hWnd);
- 310 break;
- 311
- 312 }
- 313 return (0);
- 314
- 315 case WM_PAINT :
- 316 hDC = BeginPaint(hWnd, &ps);
- 317
- 318 DrawAllGraph(hDC, ps.rcPaint, Shape, Num);
- 319
- 320 EndPaint(hWnd, &ps);
- 321 return (0);
- 322
- 323 case WM_DESTROY :
- 324 PostQuitMessage(0);
- 325 return (0);
- 326
- 327 default :
- 328 return (DefWindowProc(hWnd, message, wParam, lParam));
- 329 }
- 330 }
- 331
- 332
- 333
- 334 void DrawAllGraph(HDC hDC, RECT psRect, SHAPE Shape[], int Num)
- 335 {
- 336 int i, temp;
- 337 RECT Rect, dstRect;
- 338 HANDLE hBrush;
- 339
- 340 for (i=0; i<Num; i++)
- 341 {
- 342 switch (Shape[i].MapModeID)
- 343 {
- 344 case IDM_TEXT :
- 345 SetMapMode(hDC, MM_TEXT);
- 346 break;
- 347
- 348 case IDM_LOMET :
- 349 SetMapMode(hDC, MM_LOMETRIC);
- 350 break;
- 351
- 352 case IDM_HIMET :
- 353 SetMapMode(hDC, MM_HIMETRIC);
- 354 break;
- 355
- 356 case IDM_LOENG :
- 357 SetMapMode(hDC, MM_LOENGLISH);
- 358 break;
- 359
- 360 case IDM_HIENG :
- 361 SetMapMode(hDC, MM_HIENGLISH);
- 362 break;
- 363
- 364 case IDM_TWIPS :
- 365 SetMapMode(hDC, MM_TWIPS);
- 366 break;
- 367 }
- 368
- 369 Rect.left = Shape[i].x1;
- 370 Rect.right = Shape[i].x2;
- 371 Rect.top = Shape[i].y1;
- 372 Rect.bottom= Shape[i].y2;
- 373
- 374 LPtoDP(hDC, (LPPOINT) &Rect, 2);
- 375
- 376 if (Rect.left > Rect.right)
- 377 {
- 378 temp = Rect.left;
- 379 Rect.left = Rect.right;
- 380 Rect.right = temp;
- 381 }
- 382 if (Rect.top > Rect.bottom)
- 383 {
- 384 temp = Rect.top;
- 385 Rect.top = Rect.bottom;
- 386 Rect.bottom = temp;
- 387 }
- 388
- 389 IntersectRect(&dstRect, &Rect, &psRect);
- 390 if (IsRectEmpty(&dstRect))
- 391 continue;
- 392
- 393 switch (Shape[i].ShapeID)
- 394 {
- 395 case IDM_LINE :
- 396 MoveTo(hDC, Shape[i].x1, Shape[i].y1);
- 397 LineTo(hDC, Shape[i].x2, Shape[i].y2);
- 398 break;
- 399
- 400 case IDM_RECT :
- 401 hBrush = GetStockObject(NULL_BRUSH);
- 402 SelectObject(hDC, hBrush);
- 403 Rectangle(hDC, Shape[i].x1, Shape[i].y1,
- 404 Shape[i].x2, Shape[i].y2);
- 405 break;
- 406
- 407 case IDM_ELLIP :
- 408 hBrush = GetStockObject(NULL_BRUSH);
- 409 SelectObject(hDC, hBrush);
- 410 Ellipse(hDC, Shape[i].x1, Shape[i].y1,
- 411 Shape[i].x2, Shape[i].y2);
- 412 break;
- 413 }
- 414 }
- 415 }
- 416
- 417
- 418 BOOL FAR PASCAL LineDlgProc(HWND hDlg, unsigned msg,
- 419 WORD wParam, LONG lParam)
- 420 {
- 421 BOOL btran1, btran2, btran3, btran4;
- 422 int x1, y1, x2, y2;
- 423
- 424 switch (msg)
- 425 {
- 426 case WM_INITDIALOG :
- 427 SetDlgItemInt(hDlg, DI_LLEFT,
- 428 LineRect.left, TRUE);
- 429 SetDlgItemInt(hDlg, DI_LTOP,
- 430 LineRect.top, TRUE);
- 431 SetDlgItemInt(hDlg, DI_LRIGHT,
- 432 LineRect.right, TRUE);
- 433 SetDlgItemInt(hDlg, DI_LBOTTOM,
- 434 LineRect.bottom, TRUE);
- 435 return (TRUE);
- 436
- 437 case WM_COMMAND :
- 438 switch (wParam)
- 439 {
- 440 case DI_LOK :
- 441 x1 = GetDlgItemInt(hDlg, DI_LLEFT,
- 442 &btran1, TRUE);
- 443 y1 = GetDlgItemInt(hDlg, DI_LTOP,
- 444 &btran2, TRUE);
- 445 x2 = GetDlgItemInt(hDlg, DI_LRIGHT,
- 446 &btran3, TRUE);
- 447 y2 = GetDlgItemInt(hDlg, DI_LBOTTOM,
- 448 &btran4, TRUE);
- 449
- 450 if (btran1 && btran2 && btran3 && btran4)
- 451 SetRect(&LineRect, x1, y1, x2, y2);
- 452
- 453 EndDialog(hDlg, TRUE);
- 454 return (TRUE);
- 455
- 456 case DI_LCANCEL :
- 457 EndDialog(hDlg, FALSE);
- 458 return (TRUE);
- 459 }
- 460 return (TRUE);
- 461
- 462 default :
- 463 return (FALSE);
- 464 }
- 465 }
- 466
- 467
- 468 BOOL FAR PASCAL RectDlgProc(HWND hDlg, unsigned msg,
- 469 WORD wParam, LONG lParam)
- 470 {
- 471 BOOL btran1, btran2, btran3, btran4;
- 472 int x1, y1, x2, y2;
- 473
- 474 switch (msg)
- 475 {
- 476 case WM_INITDIALOG :
- 477 SetDlgItemInt(hDlg, DI_RLEFT,
- 478 RectRect.left, TRUE);
- 479 SetDlgItemInt(hDlg, DI_RTOP,
- 480 RectRect.top, TRUE);
- 481 SetDlgItemInt(hDlg, DI_RRIGHT,
- 482 RectRect.right, TRUE);
- 483 SetDlgItemInt(hDlg, DI_RBOTTOM,
- 484 RectRect.bottom, TRUE);
- 485 return (TRUE);
- 486
- 487 case WM_COMMAND :
- 488 switch (wParam)
- 489 {
- 490 case DI_ROK :
- 491 x1 = GetDlgItemInt(hDlg, DI_RLEFT,
- 492 &btran1, TRUE);
- 493 y1 = GetDlgItemInt(hDlg, DI_RTOP,
- 494 &btran2, TRUE);
- 495 x2 = GetDlgItemInt(hDlg, DI_RRIGHT,
- 496 &btran3, TRUE);
- 497 y2 = GetDlgItemInt(hDlg, DI_RBOTTOM,
- 498 &btran4, TRUE);
- 499
- 500 if (btran1 && btran2 && btran3 && btran4)
- 501 SetRect(&RectRect, x1, y1, x2, y2);
- 502
- 503 EndDialog(hDlg, TRUE);
- 504 return (TRUE);
- 505
- 506 case DI_RCANCEL :
- 507 EndDialog(hDlg, FALSE);
- 508 return (TRUE);
- 509 }
- 510 return (TRUE);
- 511
- 512 default :
- 513 return (FALSE);
- 514 }
- 515 }
- 516
- 517
- 518 BOOL FAR PASCAL EllipDlgProc(HWND hDlg, unsigned msg,
- 519 WORD wParam, LONG lParam)
- 520 {
- 521 BOOL btran1, btran2, btran3, btran4;
- 522 int x1, y1, x2, y2;
- 523
- 524 switch (msg)
- 525 {
- 526 case WM_INITDIALOG :
- 527 SetDlgItemInt(hDlg, DI_ELEFT,
- 528 EllipRect.left, TRUE);
- 529 SetDlgItemInt(hDlg, DI_ETOP,
- 530 EllipRect.top, TRUE);
- 531 SetDlgItemInt(hDlg, DI_ERIGHT,
- 532 EllipRect.right, TRUE);
- 533 SetDlgItemInt(hDlg, DI_EBOTTOM,
- 534 EllipRect.bottom, TRUE);
- 535 return (TRUE);
- 536
- 537 case WM_COMMAND :
- 538 switch (wParam)
- 539 {
- 540 case DI_EOK :
- 541 x1 = GetDlgItemInt(hDlg, DI_ELEFT,
- 542 &btran1, TRUE);
- 543 y1 = GetDlgItemInt(hDlg, DI_ETOP,
- 544 &btran2, TRUE);
- 545 x2 = GetDlgItemInt(hDlg, DI_ERIGHT,
- 546 &btran3, TRUE);
- 547 y2 = GetDlgItemInt(hDlg, DI_EBOTTOM,
- 548 &btran4, TRUE);
- 549
- 550 if (btran1 && btran2 && btran3 && btran4)
- 551 SetRect(&EllipRect, x1, y1, x2, y2);
- 552
- 553 EndDialog(hDlg, TRUE);
- 554 return (TRUE);
- 555
- 556 case DI_ECANCEL :
- 557 EndDialog(hDlg, FALSE);
- 558 return (TRUE);
- 559 }
- 560 return (TRUE);
- 561
- 562 default :
- 563 return (FALSE);
- 564 }
- 565 }
- 566