DEMO4_5.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:15k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo4_5 --- Simple Paint */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include <math.h>
- 7 #include "demo4_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, BOOL);
- 18 void DrawPencil(HDC);
- 19 void DrawLine(HDC, BOOL);
- 20 void DrawRect(HDC, BOOL);
- 21 void DrawEllip(HDC, BOOL);
- 22 void DrawCircle(HDC, BOOL);
- 23 void DrawRoundRect(HDC, 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 /****************************************************************/
- 56 /* WinMain() */
- 57 /****************************************************************/
- 58
- 59 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 60 LPSTR lpszCmdLine, int nCmdShow)
- 61 {
- 62 WNDCLASS wclass;
- 63 MSG msg;
- 64 HWND hWnd;
- 65 char szName[] = "Demo4_5";
- 66
- 67 if (!hPrevInstance)
- 68 {
- 69 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 70 wclass.lpfnWndProc = MainWndProc;
- 71 wclass.cbClsExtra = 0;
- 72 wclass.cbWndExtra = 0;
- 73 wclass.hInstance = hInstance;
- 74 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 75 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 76 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 77 wclass.lpszMenuName = szName;
- 78 wclass.lpszClassName = szName;
- 79
- 80 if (!RegisterClass (&wclass))
- 81 return (FALSE);
- 82
- 83 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 84 wclass.lpfnWndProc = ChooseCtrlProc;
- 85 wclass.cbClsExtra = 0;
- 86 wclass.cbWndExtra = 0;
- 87 wclass.hInstance = hInstance;
- 88 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 89 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 90 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 91 wclass.lpszMenuName = NULL;
- 92 wclass.lpszClassName = "Choose";
- 93
- 94 if (!RegisterClass (&wclass))
- 95 return (FALSE);
- 96
- 97 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 98 wclass.lpfnWndProc = LineWSCtrlProc;
- 99 wclass.cbClsExtra = 0;
- 100 wclass.cbWndExtra = 0;
- 101 wclass.hInstance = hInstance;
- 102 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 103 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 104 wclass.hbrBackground = COLOR_WINDOW + 1;
- 105 wclass.lpszMenuName = NULL;
- 106 wclass.lpszClassName = "LineWS";
- 107
- 108 if (!RegisterClass (&wclass))
- 109 return (FALSE);
- 110 }
- 111
- 112 hWnd = CreateWindow(
- 113 szName,
- 114 "Simple Paint" ,
- 115 WS_OVERLAPPEDWINDOW,
- 116 CW_USEDEFAULT,
- 117 CW_USEDEFAULT,
- 118 CW_USEDEFAULT,
- 119 CW_USEDEFAULT,
- 120 NULL,
- 121 NULL,
- 122 hInstance,
- 123 NULL );
- 124
- 125 if (!hWnd)
- 126 return (FALSE);
- 127
- 128 ShowWindow(hWnd, nCmdShow);
- 129 UpdateWindow(hWnd);
- 130
- 131 while (GetMessage(&msg, NULL, NULL,NULL))
- 132 {
- 133 TranslateMessage(&msg);
- 134 DispatchMessage(&msg);
- 135 }
- 136 return (msg.wParam);
- 137 }
- 138
- 139
- 140
- 141 /****************************************************************/
- 142 /* MainWndProc() */
- 143 /****************************************************************/
- 144
- 145 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 146 WORD wParam, LONG lParam)
- 147 {
- 148 HDC hDC;
- 149 HMENU hMenu;
- 150 static BOOL bLBDown;
- 151
- 152 switch (message)
- 153 {
- 154 case WM_CREATE :
- 155 hMenu = GetMenu(hWnd);
- 156 CheckMenuItem(hMenu, IDM_PENCIL, MF_CHECKED);
- 157
- 158 memcpy(crPCurColor, crDefColor,
- 159 sizeof(crDefColor));
- 160 memcpy(crBCurColor, crDefColor,
- 161 sizeof(crDefColor));
- 162
- 163 hInst = ((LPCREATESTRUCT) lParam)->hInstance;
- 164 return (0);
- 165
- 166 case WM_COMMAND :
- 167 hMenu = GetMenu(hWnd);
- 168 switch (wParam)
- 169 {
- 170 case IDM_PENCIL :
- 171 case IDM_LINE :
- 172 case IDM_RECT_F :
- 173 case IDM_RECT :
- 174 case IDM_ELLIP_F :
- 175 case IDM_ELLIP :
- 176 case IDM_CIRCLE_F :
- 177 case IDM_CIRCLE :
- 178 case IDM_ROUNDRECT_F:
- 179 case IDM_ROUNDRECT :
- 180
- 181 if (ToolID == wParam)
- 182 return (0);
- 183
- 184 CheckMenuItem(hMenu, ToolID,
- 185 MF_UNCHECKED);
- 186 ToolID = wParam;
- 187 CheckMenuItem(hMenu, ToolID,
- 188 MF_CHECKED);
- 189 break;
- 190
- 191 case IDM_CHOOSEPEN :
- 192
- 193 lpPenDlgProc = MakeProcInstance(
- 194 (FARPROC) PenDlgProc, hInst);
- 195
- 196 DialogBox(hInst, "PENDLG", hWnd,
- 197 lpPenDlgProc);
- 198
- 199 FreeProcInstance(lpPenDlgProc);
- 200 break;
- 201
- 202 case IDM_CHOOSEBRUSH :
- 203
- 204 lpBrushDlgProc = MakeProcInstance(
- 205 (FARPROC) BrushDlgProc, hInst);
- 206
- 207 DialogBox(hInst, "BRUSHDLG", hWnd,
- 208 lpBrushDlgProc);
- 209
- 210 FreeProcInstance(lpBrushDlgProc);
- 211 break;
- 212
- 213 case IDM_CLEAR :
- 214 InvalidateRect(hWnd, NULL, TRUE);
- 215 break;
- 216
- 217 case IDM_QUIT :
- 218 DestroyWindow(hWnd);
- 219 break;
- 220 }
- 221 return (0);
- 222
- 223 case WM_LBUTTONDOWN :
- 224 SetCapture(hWnd);
- 225 bLBDown = TRUE;
- 226
- 227 OrgPoint = MAKEPOINT(lParam);
- 228 CurPoint = PrePoint = OrgPoint;
- 229
- 230 return (0);
- 231
- 232 case WM_LBUTTONUP :
- 233 bLBDown = FALSE;
- 234 ReleaseCapture();
- 235
- 236 hDC = GetDC(hWnd);
- 237 DrawGraph(hDC, TRUE);
- 238 ReleaseDC(hWnd, hDC);
- 239
- 240 return (0);
- 241
- 242 case WM_MOUSEMOVE :
- 243 if (bLBDown)
- 244 {
- 245 PrePoint = CurPoint;
- 246 CurPoint = MAKEPOINT(lParam);
- 247
- 248 hDC = GetDC(hWnd);
- 249 DrawGraph(hDC, FALSE);
- 250 ReleaseDC(hWnd, hDC);
- 251 }
- 252 return (0);
- 253
- 254 case WM_DESTROY :
- 255 PostQuitMessage(0);
- 256 return (0);
- 257
- 258 default :
- 259 return(DefWindowProc(hWnd, message, wParam, lParam));
- 260 }
- 261 }
- 262
- 263
- 264
- 265 HBRUSH MyCreateBrush(int nHatchStyle, COLORREF crColor)
- 266 {
- 267 HBRUSH hBrush;
- 268
- 269 if (nHatchStyle == -1)
- 270 hBrush = CreateSolidBrush(crColor);
- 271 else
- 272 hBrush = CreateHatchBrush(nHatchStyle, crColor);
- 273
- 274 return (hBrush);
- 275 }
- 276
- 277
- 278
- 279 void DrawGraph(HDC hDC, BOOL bSure)
- 280 {
- 281 HPEN hPen, hPrePen;
- 282 HBRUSH hBrush, hPreBrush;
- 283
- 284 if (ToolID==IDM_PENCIL || bSure)
- 285 {
- 286 hPen = CreatePen(nPenStyle, nPenWidth,
- 287 MKCOLOR(crPCurColor[nPenColor]));
- 288 hPrePen = SelectObject(hDC, hPen);
- 289
- 290 if (ToolID==IDM_RECT_F || ToolID==IDM_ELLIP_F ||
- 291 ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
- 292 {
- 293 hBrush = MyCreateBrush(nHatch,
- 294 MKCOLOR(crBCurColor[nBrushColor]));
- 295 hPreBrush = SelectObject(hDC, hBrush);
- 296 }
- 297 else
- 298 {
- 299 hBrush = GetStockObject(NULL_BRUSH);
- 300 hPreBrush = SelectObject(hDC, hBrush);
- 301 }
- 302 }
- 303 else
- 304 SelectObject(hDC, GetStockObject(NULL_BRUSH));
- 305
- 306 switch (ToolID)
- 307 {
- 308 case IDM_PENCIL :
- 309 DrawPencil(hDC);
- 310 break;
- 311
- 312 case IDM_LINE :
- 313 DrawLine(hDC, bSure);
- 314 break;
- 315
- 316 case IDM_RECT_F :
- 317 case IDM_RECT :
- 318 DrawRect(hDC, bSure);
- 319 break;
- 320
- 321 case IDM_ELLIP_F :
- 322 case IDM_ELLIP :
- 323 DrawEllip(hDC, bSure);
- 324 break;
- 325
- 326 case IDM_CIRCLE_F :
- 327 case IDM_CIRCLE :
- 328 DrawCircle(hDC, bSure);
- 329 break;
- 330
- 331 case IDM_ROUNDRECT_F :
- 332 case IDM_ROUNDRECT :
- 333 DrawRoundRect(hDC, bSure);
- 334 break;
- 335 }
- 336
- 337 if (ToolID==IDM_PENCIL || bSure)
- 338 {
- 339 SelectObject(hDC, hPrePen);
- 340 DeleteObject(hPen);
- 341
- 342 if (ToolID==IDM_RECT_F || ToolID==IDM_ELLIP_F ||
- 343 ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
- 344 {
- 345 SelectObject(hDC, hPreBrush);
- 346 DeleteObject(hBrush);
- 347 }
- 348 else
- 349 {
- 350 SelectObject(hDC, hPreBrush);
- 351 }
- 352 }
- 353 }
- 354
- 355
- 356
- 357 void DrawPencil(HDC hDC)
- 358 {
- 359 MoveTo(hDC, PrePoint.x, PrePoint.y);
- 360 LineTo(hDC, CurPoint.x, CurPoint.y);
- 361 }
- 362
- 363
- 364
- 365 void DrawLine(HDC hDC, BOOL bSure)
- 366 {
- 367 int nDrawMode;
- 368
- 369 if (! bSure)
- 370 {
- 371 nDrawMode = SetROP2(hDC, R2_NOT);
- 372
- 373 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 374 LineTo(hDC, PrePoint.x, PrePoint.y);
- 375
- 376 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 377 LineTo(hDC, CurPoint.x, CurPoint.y);
- 378
- 379 SetROP2(hDC, nDrawMode);
- 380 }
- 381 else
- 382 {
- 383 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 384 LineTo(hDC, CurPoint.x, CurPoint.y);
- 385 }
- 386 }
- 387
- 388
- 389
- 390 void DrawRect(HDC hDC, BOOL bSure)
- 391 {
- 392 int nDrawMode;
- 393
- 394 if (! bSure)
- 395 {
- 396 nDrawMode = SetROP2(hDC, R2_NOT);
- 397
- 398 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
- 399 PrePoint.x, PrePoint.y);
- 400
- 401 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
- 402 CurPoint.x, CurPoint.y);
- 403
- 404 SetROP2(hDC, nDrawMode);
- 405 }
- 406 else
- 407 {
- 408 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
- 409 CurPoint.x, CurPoint.y);
- 410 }
- 411 }
- 412
- 413
- 414
- 415 void DrawEllip(HDC hDC, BOOL bSure)
- 416 {
- 417 int nDrawMode;
- 418
- 419 if (! bSure)
- 420 {
- 421 nDrawMode = SetROP2(hDC, R2_NOT);
- 422
- 423 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 424 PrePoint.x, PrePoint.y);
- 425
- 426 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 427 CurPoint.x, CurPoint.y);
- 428
- 429 SetROP2(hDC, nDrawMode);
- 430 }
- 431 else
- 432 {
- 433 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 434 CurPoint.x, CurPoint.y);
- 435 }
- 436 }
- 437
- 438
- 439
- 440 void DrawRoundRect(HDC hDC, BOOL bSure)
- 441 {
- 442 int nDrawMode;
- 443
- 444 if (! bSure)
- 445 {
- 446 nDrawMode = SetROP2(hDC, R2_NOT);
- 447
- 448 RoundRect(hDC, OrgPoint.x, OrgPoint.y,
- 449 PrePoint.x, PrePoint.y,
- 450 (PrePoint.x-OrgPoint.x)/4,
- 451 (PrePoint.y-OrgPoint.y)/4);
- 452
- 453 RoundRect(hDC, OrgPoint.x, OrgPoint.y,
- 454 CurPoint.x, CurPoint.y,
- 455 (CurPoint.x-OrgPoint.x)/4,
- 456 (CurPoint.y-OrgPoint.y)/4);
- 457
- 458 SetROP2(hDC, nDrawMode);
- 459 }
- 460 else
- 461 {
- 462 RoundRect(hDC, OrgPoint.x, OrgPoint.y,
- 463 CurPoint.x, CurPoint.y,
- 464 (CurPoint.x-OrgPoint.x)/4,
- 465 (CurPoint.y-OrgPoint.y)/4);
- 466 }
- 467 }
- 468
- 469
- 470
- 471 void DrawCircle(HDC hDC, BOOL bSure)
- 472 {
- 473 int nDrawMode;
- 474 int nLogPixSx, nLogPixSy;
- 475 int Width, Height;
- 476 int SignX, SignY;
- 477
- 478 nLogPixSx = GetDeviceCaps(hDC, LOGPIXELSX);
- 479 nLogPixSy = GetDeviceCaps(hDC, LOGPIXELSY);
- 480
- 481 Width = CurPoint.x - OrgPoint.x;
- 482 Height = CurPoint.y - OrgPoint.y;
- 483 SignX = (Width >= 0 ? 1 : -1);
- 484 SignY = (Height >= 0 ? 1 : -1);
- 485
- 486 if (fabs((float) Width/nLogPixSx) >
- 487 fabs((float) Height/nLogPixSy) )
- 488 {
- 489 CurPoint.x = OrgPoint.x + (float)
- 490 fabs(Height) * nLogPixSx / nLogPixSy * SignX;
- 491 }
- 492 else
- 493 {
- 494 CurPoint.y = OrgPoint.y + (float)
- 495 fabs(Width) * nLogPixSy / nLogPixSx * SignY;
- 496 }
- 497
- 498
- 499 if (! bSure)
- 500 {
- 501 nDrawMode = SetROP2(hDC, R2_NOT);
- 502
- 503 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 504 PrePoint.x, PrePoint.y);
- 505
- 506 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 507 CurPoint.x, CurPoint.y);
- 508
- 509 SetROP2(hDC, nDrawMode);
- 510 }
- 511 else
- 512 {
- 513 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
- 514 CurPoint.x, CurPoint.y);
- 515 }
- 516 }