⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paint_dg.c

📁 WINDOWS图形编程随书光盘
💻 C
📖 第 1 页 / 共 2 页
字号:
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 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -