DEMO5_5D.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:19k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 #include "demo5_5.h"
- 2 #include <windows.h>
- 3
- 4 BOOL FAR PASCAL ColorDlgProc(HWND, unsigned, WORD, LONG);
- 5 HBRUSH MyCreateBrush(int, COLORREF);
- 6
- 7 FARPROC lpColorDlgProc;
- 8
- 9 extern HANDLE hInst;
- 10
- 11 extern int nPenColor;
- 12 extern int nPenStyle;
- 13 extern int nPenWidth;
- 14
- 15 extern int nBrushColor;
- 16 extern int nHatch;
- 17
- 18 typedef struct tagCOLORSTRUCT {
- 19 int cR;
- 20 int cG;
- 21 int cB;
- 22 } COLORSTRUCT;
- 23
- 24 #define MKCOLOR(A) (RGB(A.cR, A.cG, A.cB))
- 25
- 26 COLORSTRUCT crDefColor[28] =
- 27 { {255,255,255}, {255, 0, 0}, { 0,255, 0},
- 28 { 0, 0,255}, {255,255, 0}, { 0,255,255},
- 29 {255, 0,255},
- 30
- 31 {192,192,192}, {128, 0, 0}, { 0,128, 0},
- 32 { 0, 0,128}, {128,128, 0}, { 0,128,128},
- 33 {128, 0,128},
- 34
- 35 {128,128,128}, {255,255,128}, { 0,255,128},
- 36 {128,255,255}, {128,128,255}, {255, 0,128},
- 37 {255,128, 64},
- 38
- 39 { 0, 0, 0}, {128,128, 64}, { 0, 64, 64},
- 40 { 0,128,255}, { 0, 64,128}, { 64, 0,128},
- 41 {128, 64, 0}
- 42 };
- 43
- 44 COLORSTRUCT crPCurColor[28];
- 45 COLORSTRUCT crBCurColor[28];
- 46
- 47 int nChooseColor;
- 48 int nChooseStyle;
- 49 int nChooseWidth;
- 50 int nChooseHatch;
- 51
- 52 int nCurDlgID;
- 53
- 54 long FAR PASCAL ChooseCtrlProc(HWND hCtrl, unsigned msg,
- 55 WORD wParam, LONG lParam)
- 56 {
- 57 PAINTSTRUCT ps;
- 58 HDC hDC;
- 59 HBRUSH hBrush;
- 60 RECT Client;
- 61 HANDLE nCtrlID;
- 62
- 63 switch (msg)
- 64 {
- 65 case WM_PAINT :
- 66 hDC = BeginPaint(hCtrl, &ps);
- 67
- 68 GetClientRect(hCtrl, &Client);
- 69 nCtrlID = GetWindowWord(hCtrl, GWW_ID);
- 70
- 71 if (nCtrlID>=DI_H01 && nCtrlID<=DI_H07)
- 72 hBrush = MyCreateBrush(nCtrlID-DI_H02, 0);
- 73 else
- 74 hBrush = MyCreateBrush(-1,
- 75 (nCurDlgID == IDM_CHOOSEPEN ?
- 76 MKCOLOR(crPCurColor[nCtrlID-DI_PC01]) :
- 77 MKCOLOR(crBCurColor[nCtrlID-DI_BC01])));
- 78
- 79 FillRect(hDC, &Client, hBrush);
- 80 DeleteObject(hBrush);
- 81
- 82 EndPaint(hCtrl, &ps);
- 83 return (0);
- 84
- 85 case WM_LBUTTONDOWN :
- 86 nCtrlID = GetWindowWord(hCtrl, GWW_ID);
- 87 SendMessage(GetParent(hCtrl), WM_COMMAND,
- 88 nCtrlID, (LONG) 0);
- 89 return (0);
- 90 }
- 91
- 92 return (DefWindowProc(hCtrl, msg, wParam, lParam));
- 93 }
- 94
- 95
- 96
- 97 long FAR PASCAL LineWSCtrlProc(HWND hCtrl, unsigned msg,
- 98 WORD wParam, LONG lParam)
- 99 {
- 100 PAINTSTRUCT ps;
- 101 HDC hDC;
- 102 HPEN hPen, hPrePen;
- 103 RECT Client;
- 104 HANDLE nCtrlID;
- 105 int i, Item;
- 106
- 107 switch (msg)
- 108 {
- 109 case WM_PAINT :
- 110 hDC = BeginPaint(hCtrl, &ps);
- 111
- 112 GetClientRect(hCtrl, &Client);
- 113 nCtrlID = GetWindowWord(hCtrl, GWW_ID);
- 114
- 115 if (nCtrlID == DI_WIDTH)
- 116 {
- 117 for (i=1; i<10; i+=2)
- 118 {
- 119 hPen = CreatePen(PS_SOLID, i, 0);
- 120 hPrePen = SelectObject(hDC, hPen);
- 121
- 122 MoveTo(hDC, Client.left,
- 123 Client.bottom*i/10);
- 124 LineTo(hDC, Client.right,
- 125 Client.bottom*i/10);
- 126
- 127 SelectObject(hDC, hPrePen);
- 128 DeleteObject(hPrePen);
- 129 }
- 130 }
- 131 else
- 132 {
- 133 for (i=0; i<5; i++)
- 134 {
- 135 hPen = CreatePen(i, 1, 0);
- 136 hPrePen = SelectObject(hDC, hPen);
- 137 SetBkMode(hDC, TRANSPARENT);
- 138
- 139 MoveTo(hDC, Client.left,
- 140 Client.bottom*(1+2*i)/10);
- 141 LineTo(hDC, Client.right,
- 142 Client.bottom*(1+2*i)/10);
- 143
- 144 SelectObject(hDC, hPrePen);
- 145 DeleteObject(hPrePen);
- 146 }
- 147 }
- 148
- 149 EndPaint(hCtrl, &ps);
- 150 return (0);
- 151
- 152 case WM_LBUTTONDOWN :
- 153 nCtrlID = GetWindowWord(hCtrl, GWW_ID);
- 154 GetClientRect(hCtrl, &Client);
- 155
- 156 Item = HIWORD(lParam)/(Client.bottom/5);
- 157 SendMessage(GetParent(hCtrl), WM_COMMAND,
- 158 nCtrlID, (LONG) Item);
- 159 return (0);
- 160 }
- 161
- 162 return (DefWindowProc(hCtrl, msg, wParam, lParam));
- 163 }
- 164
- 165
- 166
- 167 void ReDrawPenGraph(HDC hCtrl)
- 168 {
- 169 HWND hWnd;
- 170 RECT Client;
- 171 HDC hDC;
- 172 HPEN hPen, hPrePen;
- 173 HBRUSH hBrush;
- 174 COLORREF crBkColor;
- 175
- 176 hWnd = GetParent(GetParent(hCtrl));
- 177 hDC = GetDC(hWnd);
- 178 crBkColor = GetBkColor(hDC);
- 179 ReleaseDC(hWnd, hDC);
- 180
- 181 hDC = GetDC(hCtrl);
- 182 GetClientRect(hCtrl, &Client);
- 183
- 184 hBrush = CreateSolidBrush(crBkColor);
- 185 FillRect(hDC, &Client, hBrush);
- 186
- 187 hPen = CreatePen(nChooseStyle, nChooseWidth,
- 188 MKCOLOR(crPCurColor[nChooseColor]));
- 189 hPrePen = SelectObject(hDC, hPen);
- 190 SetBkMode(hDC, TRANSPARENT);
- 191
- 192 MoveTo(hDC, Client.left+5, Client.top+5);
- 193 LineTo(hDC, Client.right-5, Client.bottom-5);
- 194
- 195 SelectObject(hDC, hPrePen);
- 196 DeleteObject(hPen);
- 197
- 198 ReleaseDC(hCtrl, hDC);
- 199 }
- 200
- 201
- 202
- 203 BOOL FAR PASCAL PenDlgProc(HWND hDlg, unsigned msg,
- 204 WORD wParam, LONG lParam)
- 205 {
- 206 HANDLE hCtrl, hCtrlGraph;
- 207
- 208 switch (msg)
- 209 {
- 210 case WM_INITDIALOG :
- 211 nChooseColor = nPenColor;
- 212 nChooseStyle = nPenStyle;
- 213 nChooseWidth = nPenWidth;
- 214 nCurDlgID = IDM_CHOOSEPEN;
- 215 return (FALSE);
- 216
- 217 case WM_CLOSE :
- 218 EndDialog(hDlg, FALSE);
- 219 return (TRUE);
- 220
- 221 case WM_PAINT :
- 222 hCtrlGraph = GetDlgItem(hDlg, DI_PGRAPH);
- 223 ReDrawPenGraph(hCtrlGraph);
- 224 return (FALSE);
- 225
- 226 case WM_COMMAND :
- 227 switch (wParam)
- 228 {
- 229 case DI_OK :
- 230 nPenColor = nChooseColor;
- 231 nPenWidth = nChooseWidth;
- 232 nPenStyle = nChooseStyle;
- 233 EndDialog(hDlg, TRUE);
- 234 return (TRUE);
- 235
- 236 case DI_CANCEL :
- 237 EndDialog(hDlg, FALSE);
- 238 return (TRUE);
- 239
- 240 case DI_ED :
- 241 lpColorDlgProc = MakeProcInstance(
- 242 (FARPROC) ColorDlgProc, hInst);
- 243
- 244 DialogBox(hInst, "COLORDLG", hDlg,
- 245 lpColorDlgProc);
- 246
- 247 FreeProcInstance(lpColorDlgProc);
- 248
- 249 hCtrlGraph = GetDlgItem(hDlg, DI_PGRAPH);
- 250 ReDrawPenGraph(hCtrlGraph);
- 251
- 252 hCtrl = GetDlgItem(hDlg, DI_PC01+
- 253 nChooseColor);
- 254 InvalidateRect(hCtrl, NULL, TRUE);
- 255 return(TRUE);
- 256
- 257 default :
- 258 if (wParam>=DI_PC01 && wParam<=DI_PC28)
- 259 {
- 260 nChooseColor = wParam-DI_PC01;
- 261 hCtrlGraph =
- 262 GetDlgItem(hDlg, DI_PGRAPH);
- 263 ReDrawPenGraph(hCtrlGraph);
- 264 }
- 265 else
- 266 if (wParam == DI_WIDTH)
- 267 {
- 268 nChooseWidth = lParam*2 + 1;
- 269 hCtrlGraph =
- 270 GetDlgItem(hDlg, DI_PGRAPH);
- 271 ReDrawPenGraph(hCtrlGraph);
- 272 }
- 273 else
- 274 if (wParam == DI_STYLE)
- 275 {
- 276 nChooseStyle = lParam;
- 277 hCtrlGraph =
- 278 GetDlgItem(hDlg, DI_PGRAPH);
- 279 ReDrawPenGraph(hCtrlGraph);
- 280 }
- 281 }
- 282 return (TRUE);
- 283
- 284 default :
- 285 return (FALSE);
- 286 }
- 287 }
- 288
- 289
- 290
- 291 void ReDrawBrushGraph(HDC hCtrl)
- 292 {
- 293 HWND hWnd;
- 294 RECT Client;
- 295 HDC hDC;
- 296 HPEN hPen, hPrePen;
- 297 HBRUSH hBrush, hPreBrush;
- 298 COLORREF crBkColor, crColor;
- 299
- 300 hWnd = GetParent(GetParent(hCtrl));
- 301 hDC = GetDC(hWnd);
- 302 crBkColor = GetBkColor(hDC);
- 303 ReleaseDC(hWnd, hDC);
- 304
- 305 hDC = GetDC(hCtrl);
- 306 GetClientRect(hCtrl, &Client);
- 307
- 308 crColor = GetSysColor(COLOR_WINDOW);
- 309 hBrush = CreateSolidBrush(crColor);
- 310 FillRect(hDC, &Client, hBrush);
- 311
- 312 hPen = CreatePen(nPenStyle, nPenWidth,
- 313 MKCOLOR(crPCurColor[nPenColor]));
- 314 hBrush = MyCreateBrush(nChooseHatch,
- 315 MKCOLOR(crBCurColor[nChooseColor]));
- 316
- 317 hPrePen = SelectObject(hDC, hPen);
- 318 hPreBrush = SelectObject(hDC, hBrush);
- 319 SetBkColor(hDC, crBkColor);
- 320
- 321 Rectangle(hDC, Client.left+2, Client.top+2,
- 322 Client.right-2, Client.bottom-2);
- 323
- 324 SelectObject(hDC, hPrePen);
- 325 SelectObject(hDC, hPreBrush);
- 326 DeleteObject(hPen);
- 327 DeleteObject(hBrush);
- 328
- 329 ReleaseDC(hCtrl, hDC);
- 330 }
- 331
- 332
- 333
- 334 BOOL FAR PASCAL BrushDlgProc(HWND hDlg, unsigned msg,
- 335 WORD wParam, LONG lParam)
- 336 {
- 337 HANDLE hCtrl, hCtrlGraph;
- 338
- 339 switch (msg)
- 340 {
- 341 case WM_INITDIALOG :
- 342 nChooseColor = nBrushColor;
- 343 nChooseHatch = nHatch;
- 344 nCurDlgID = IDM_CHOOSEBRUSH;
- 345 return (TRUE);
- 346
- 347 case WM_CLOSE :
- 348 EndDialog(hDlg, FALSE);
- 349 return (TRUE);
- 350
- 351 case WM_PAINT :
- 352 hCtrlGraph = GetDlgItem(hDlg, DI_BGRAPH);
- 353 ReDrawBrushGraph(hCtrlGraph);
- 354 return (FALSE);
- 355
- 356 case WM_COMMAND :
- 357 switch (wParam)
- 358 {
- 359 case DI_OK :
- 360 nBrushColor = nChooseColor;
- 361 nHatch = nChooseHatch;
- 362 EndDialog(hDlg, TRUE);
- 363 return (TRUE);
- 364
- 365 case DI_CANCEL :
- 366 EndDialog(hDlg, FALSE);
- 367 return (TRUE);
- 368
- 369 case DI_ED :
- 370 lpColorDlgProc = MakeProcInstance(
- 371 (FARPROC) ColorDlgProc, hInst);
- 372
- 373 DialogBox(hInst, "COLORDLG", hDlg,
- 374 lpColorDlgProc);
- 375
- 376 FreeProcInstance(lpColorDlgProc);
- 377
- 378 hCtrlGraph = GetDlgItem(hDlg, DI_BGRAPH);
- 379 ReDrawBrushGraph(hCtrlGraph);
- 380
- 381 hCtrl = GetDlgItem(hDlg, DI_BC01+
- 382 nChooseColor);
- 383 InvalidateRect(hCtrl, NULL, TRUE);
- 384 return(TRUE);
- 385
- 386 default :
- 387 if (wParam>=DI_BC01 && wParam<=DI_BC28)
- 388 {
- 389 nChooseColor = wParam-DI_BC01;
- 390 hCtrlGraph =
- 391 GetDlgItem(hDlg, DI_BGRAPH);
- 392 ReDrawBrushGraph(hCtrlGraph);
- 393 }
- 394 else if (wParam>=DI_H01 && wParam<=DI_H07)
- 395 {
- 396 nChooseHatch = wParam-DI_H02;
- 397 hCtrlGraph = GetDlgItem(hDlg, DI_BGRAPH);
- 398 ReDrawBrushGraph(hCtrlGraph);
- 399 }
- 400 }
- 401 return (TRUE);
- 402
- 403 default :
- 404 return (FALSE);
- 405 }
- 406 }
- 407
- 408
- 409
- 410 void ReDrawNewColor(HDC hCtrl, int Temp[3])
- 411 {
- 412 HWND hWnd;
- 413 RECT Client;
- 414 HDC hDC;
- 415 HPEN hPen, hPrePen;
- 416 HBRUSH hBrush, hPreBrush;
- 417 COLORREF crBkColor, crColor;
- 418
- 419 hDC = GetDC(hCtrl);
- 420 GetClientRect(hCtrl, &Client);
- 421
- 422 hBrush = MyCreateBrush(-1,
- 423 RGB(Temp[0], Temp[1], Temp[2]));
- 424 hPreBrush = SelectObject(hDC, hBrush);
- 425
- 426 Rectangle(hDC, Client.left, Client.top,
- 427 Client.right, Client.bottom);
- 428
- 429 SelectObject(hDC, hPreBrush);
- 430 DeleteObject(hBrush);
- 431
- 432 ReleaseDC(hCtrl, hDC);
- 433 }
- 434
- 435
- 436 BOOL FAR PASCAL ColorDlgProc(HWND hDlg, unsigned msg,
- 437 WORD wParam, LONG lParam)
- 438 {
- 439 HANDLE hCtrl, hCtrlNewColor;
- 440 int hCtrlID, i, nIndex;
- 441 int nCurValue;
- 442 BOOL bTran;
- 443 static int Temp[3];
- 444 static COLORSTRUCT *crColorArr;
- 445
- 446 switch (msg)
- 447 {
- 448 case WM_INITDIALOG :
- 449 if (nCurDlgID == IDM_CHOOSEPEN)
- 450 crColorArr = crPCurColor;
- 451 else
- 452 crColorArr = crBCurColor;
- 453
- 454 Temp[0] = crColorArr[nChooseColor].cR;
- 455 Temp[1] = crColorArr[nChooseColor].cG;
- 456 Temp[2] = crColorArr[nChooseColor].cB;
- 457
- 458 for (i=0; i<3; i++)
- 459 {
- 460 hCtrl = GetDlgItem(hDlg, i+DI_SCR1);
- 461 SetScrollRange(hCtrl, SB_CTL, 0, 255, FALSE);
- 462 SetScrollPos(hCtrl, SB_CTL, Temp[i], FALSE);
- 463
- 464 SetDlgItemInt(hDlg, DI_RVALUE+i, Temp[i],
- 465 FALSE);
- 466 }
- 467 return (TRUE);
- 468
- 469 case WM_CLOSE :
- 470 EndDialog(hDlg, FALSE);
- 471 return (TRUE);
- 472
- 473 case WM_PAINT :
- 474 hCtrlNewColor = GetDlgItem(hDlg, DI_NEWCOLOR);
- 475 ReDrawNewColor(hCtrlNewColor, Temp);
- 476 return (FALSE);
- 477
- 478 case WM_COMMAND :
- 479 switch (wParam)
- 480 {
- 481 case DI_OK :
- 482 crColorArr[nChooseColor].cR = Temp[0];
- 483 crColorArr[nChooseColor].cG = Temp[1];
- 484 crColorArr[nChooseColor].cB = Temp[2];
- 485
- 486 EndDialog(hDlg, TRUE);
- 487 return (TRUE);
- 488
- 489 case DI_CANCEL :
- 490 EndDialog(hDlg, FALSE);
- 491 return (TRUE);
- 492
- 493 case DI_RESET :
- 494 Temp[0] = crDefColor[nChooseColor].cR;
- 495 Temp[1] = crDefColor[nChooseColor].cG;
- 496 Temp[2] = crDefColor[nChooseColor].cB;
- 497
- 498 for (i=0; i<3; i++)
- 499 {
- 500 hCtrl = GetDlgItem(hDlg, i+DI_SCR1);
- 501 SetScrollPos(hCtrl, SB_CTL, Temp[i],
- 502 TRUE);
- 503
- 504 SetDlgItemInt(hDlg, DI_RVALUE+i,
- 505 Temp[i], FALSE);
- 506 }
- 507
- 508 hCtrlNewColor = GetDlgItem(hDlg,
- 509 DI_NEWCOLOR);
- 510 ReDrawNewColor(hCtrlNewColor, Temp);
- 511 return (TRUE);
- 512
- 513 case DI_RVALUE :
- 514 case DI_GVALUE :
- 515 case DI_BVALUE :
- 516
- 517 if (HIWORD(lParam) != EN_CHANGE)
- 518 return (FALSE);
- 519
- 520 nCurValue = GetDlgItemInt(hDlg, wParam,
- 521 &bTran, FALSE);
- 522 nIndex = wParam - DI_RVALUE;
- 523 if (Temp[nIndex] != nCurValue)
- 524 {
- 525 Temp[nIndex] = min(255, nCurValue);
- 526 if (nCurValue > 255)
- 527 SetDlgItemInt(hDlg, DI_RVALUE+nIndex,
- 528 Temp[nIndex], FALSE);
- 529
- 530 hCtrl = GetDlgItem(hDlg, nIndex+DI_SCR1);
- 531 SetScrollPos(hCtrl, SB_CTL,
- 532 Temp[nIndex], TRUE);
- 533
- 534 hCtrlNewColor = GetDlgItem(hDlg,
- 535 DI_NEWCOLOR);
- 536 ReDrawNewColor(hCtrlNewColor, Temp);
- 537 }
- 538 return (FALSE);
- 539 }
- 540 return (TRUE);
- 541
- 542 case WM_HSCROLL :
- 543 hCtrl = HIWORD(lParam);
- 544 hCtrlID = GetWindowWord(hCtrl, GWW_ID);
- 545
- 546 nIndex = hCtrlID - DI_SCR1;
- 547
- 548 switch (wParam)
- 549 {
- 550 case SB_PAGEDOWN :
- 551 Temp[nIndex] = Temp[nIndex] + 16;
- 552 break;
- 553
- 554 case SB_PAGEUP :
- 555 Temp[nIndex] = Temp[nIndex] - 16;
- 556 break;
- 557
- 558 case SB_LINEDOWN :
- 559 Temp[nIndex] ++;
- 560 break;
- 561
- 562 case SB_LINEUP :
- 563 Temp[nIndex] --;
- 564 break;
- 565
- 566 case SB_TOP :
- 567 Temp[nIndex] = 0;
- 568 break;
- 569
- 570 case SB_BOTTOM :
- 571 Temp[nIndex] = 255;
- 572 break;
- 573
- 574 case SB_THUMBTRACK :
- 575 case SB_THUMBPOSITION :
- 576 Temp[nIndex] = LOWORD(lParam);
- 577 break;
- 578 }
- 579
- 580 Temp[nIndex] = max(0,min(255,Temp[nIndex]));
- 581 SetScrollPos(hCtrl, SB_CTL, Temp[nIndex], TRUE);
- 582 SetDlgItemInt(hDlg, DI_RVALUE+nIndex,
- 583 Temp[nIndex], FALSE);
- 584
- 585 hCtrlNewColor = GetDlgItem(hDlg, DI_NEWCOLOR);
- 586 ReDrawNewColor(hCtrlNewColor, Temp);
- 587
- 588 return (TRUE);
- 589
- 590 default :
- 591 return (FALSE);
- 592 }
- 593 }