HatchBrush在构造过程中需要指定画刷类型、前景色、后景色三项内容,其中画刷类型在官方文档中给出的枚举类型如下所示:
1 typedef enum HatchStyle { 2 HatchStyleHorizontal, 3 HatchStyleVertical, 4 HatchStyleForwardDiagonal, 5 HatchStyleBackwardDiagonal, 6 HatchStyleCross, 7 HatchStyleDiagonalCross, 8 HatchStyle05Percent, 9 HatchStyle10Percent, 10 HatchStyle20Percent, 11 HatchStyle25Percent, 12 HatchStyle30Percent, 13 HatchStyle40Percent, 14 HatchStyle50Percent, 15 HatchStyle60Percent, 16 HatchStyle70Percent, 17 HatchStyle75Percent, 18 HatchStyle80Percent, 19 HatchStyle90Percent, 20 HatchStyleLightDownwardDiagonal, 21 HatchStyleLightUpwardDiagonal, 22 HatchStyleDarkDownwardDiagonal, HatchStyleDarkUpwardDiagonal, 23 HatchStyleWideDownwardDiagonal, HatchStyleWideUpwardDiagonal, 24 HatchStyleLightVertical, 25 HatchStyleLightHorizontal, 26 HatchStyleNarrowVertical, 27 HatchStyleNarrowHorizontal, 28 HatchStyleDarkVertical, 29 HatchStyleDarkHorizontal, 30 HatchStyleDashedDownwardDiagonal, 31 HatchStyleDashedUpwardDiagonal, 32 HatchStyleDashedHorizontal, 33 HatchStyleDashedVertical, 34 HatchStyleSmallConfetti, 35 HatchStyleHorizontal, 36 HatchStyleVertical, 37 HatchStyleLargeConfetti, 38 HatchStyleZigZag, 39 HatchStyleWave, 40 HatchStyleDiagonalBrick, 41 HatchStyleHorizontalBrick, 42 HatchStyleWeave, 43 HatchStylePlaid, 44 HatchStyleDivot, 45 HatchStyleDottedGrid, 46 HatchStyleDottedDiamond, 47 HatchStyleShingle, 48 HatchStyleTrellis, 49 HatchStyleSphere, 50 HatchStyleSmallGrid, 51 HatchStyleSmallCheckerBoard, 52 HatchStyleLargeCheckerBoard, 53 HatchStyleOutlinedDiamond, 54 HatchStyleSolidDiamond, 55 HatchStyleTotal, 56 HatchStyleLargeGrid, 57 HatchStyleMin, 58 HatchStyleMax 59 } ;
针对画刷样式代码效果如下所示:
1、变量定义
1 CComboBox m_pStyle; 2 CColorCombo m_pForeColor; 3 COLORREF m_pForeColorRef; 4 CColorCombo m_pBackColor; 5 COLORREF m_pBackColorRef; 6 HatchStyle m_pHatchStyle;
2、关键代码
1 void PenTest::DoDataExchange(CDataExchange* pDX) 2 { 3 CDialogEx::DoDataExchange(pDX); 4 DDX_Control(pDX, IDC_COMBO1, m_pStyle); 5 DDX_Control(pDX, IDC_COMBO_FORECOLOR, m_pForeColor); 6 DDX_Control(pDX, IDC_COMBO_BACKCOLOR, m_pBackColor); 7 } 8 9 10 BEGIN_MESSAGE_MAP(PenTest, CDialogEx) 11 ON_WM_PAINT() 12 ON_CBN_SELCHANGE(IDC_COMBO_FORECOLOR, &PenTest::OnSelchangeComboForecolor) 13 ON_CBN_SELCHANGE(IDC_COMBO_BACKCOLOR, &PenTest::OnSelchangeComboBackcolor) 14 ON_CBN_SELCHANGE(IDC_COMBO1, &PenTest::OnSelchangeCombo1) 15 END_MESSAGE_MAP() 16 17 void PenTest::OnPaint() 18 { 19 CPaintDC dc(this); 20 CRect crt; 21 GetClientRect(&crt); 22 Graphics graphics(dc); 23 Gdiplus::HatchBrush hra(m_pHatchStyle, 24 Color(GetRValue(m_pForeColorRef), GetGValue(m_pForeColorRef), GetBValue(m_pForeColorRef)), 25 Color(GetRValue(m_pBackColorRef), GetGValue(m_pBackColorRef), GetBValue(m_pBackColorRef))); 26 27 graphics.FillEllipse(&hra, 0, crt.Height() / 2, crt.Width(), crt.Height() / 2); 28 } 29 30 BOOL PenTest::OnInitDialog() 31 { 32 CDialogEx::OnInitDialog(); 33 34 m_pStyle.InsertString(0, _T("HatchStyleHorizontal")); 35 m_pStyle.InsertString(1, _T("HatchStyleForwardDiagonal")); 36 m_pStyle.InsertString(2, _T("HatchStyleDiagonalCross")); 37 m_pStyle.InsertString(3, _T("HatchStyle50Percent")); 38 m_pStyle.InsertString(4, _T("HatchStyleLightDownwardDiagonal")); 39 m_pStyle.InsertString(5, _T("HatchStyleDarkDownwardDiagonal")); 40 m_pStyle.InsertString(6, _T("HatchStyleWideDownwardDiagonal")); 41 m_pStyle.InsertString(7, _T("HatchStyleLightVertical")); 42 m_pStyle.InsertString(8, _T("HatchStyleNarrowVertical")); 43 m_pStyle.SetCurSel(0); 44 45 m_pForeColor.AddItem(_T("紫色"), RGB(128, 0, 128)); 46 m_pForeColor.AddItem(_T("青色"), RGB(0, 255, 255)); 47 m_pForeColor.AddItem(_T("红色"), RGB(255, 0, 0)); 48 m_pForeColor.AddItem(_T("蓝色"), RGB(0, 0, 255)); 49 m_pForeColor.AddItem(_T("绿色"), RGB(0, 255, 0)); 50 m_pForeColor.AddItem(_T("黄色"), RGB(255, 255, 0)); 51 m_pForeColor.AddItem(_T("粉色"), RGB(255, 0, 255)); 52 m_pForeColor.AddItem(_T("棕色"), RGB(255, 128, 64)); 53 m_pForeColor.SetCurSel(0); 54 55 m_pBackColor.AddItem(_T("紫色"), RGB(128, 0, 128)); 56 m_pBackColor.AddItem(_T("青色"), RGB(0, 255, 255)); 57 m_pBackColor.AddItem(_T("红色"), RGB(255, 0, 0)); 58 m_pBackColor.AddItem(_T("蓝色"), RGB(0, 0, 255)); 59 m_pBackColor.AddItem(_T("绿色"), RGB(0, 255, 0)); 60 m_pBackColor.AddItem(_T("黄色"), RGB(255, 255, 0)); 61 m_pBackColor.AddItem(_T("粉色"), RGB(255, 0, 255)); 62 m_pBackColor.AddItem(_T("棕色"), RGB(255, 128, 64)); 63 m_pBackColor.SetCurSel(0); 64 65 m_pForeColorRef = RGB(255, 0, 0); 66 m_pBackColorRef = RGB(0, 0, 0); 67 m_pHatchStyle = HatchStyleNarrowVertical; 68 return TRUE; 69 70 } 71 72 void PenTest::OnSelchangeComboForecolor() 73 { 74 m_pForeColorRef = m_pForeColor.GetItemData(m_pForeColor.GetCurSel()); 75 Invalidate(); 76 } 77 78 79 void PenTest::OnSelchangeComboBackcolor() 80 { 81 m_pBackColorRef = m_pBackColor.GetItemData(m_pBackColor.GetCurSel()); 82 Invalidate(); 83 } 84 85 86 void PenTest::OnSelchangeCombo1() 87 { 88 switch (m_pStyle.GetCurSel()) { 89 case 0: 90 m_pHatchStyle = HatchStyleHorizontal; 91 break; 92 case 1: 93 m_pHatchStyle = HatchStyleForwardDiagonal; 94 break; 95 case 2: 96 m_pHatchStyle = HatchStyleDiagonalCross; 97 break; 98 case 3: 99 m_pHatchStyle = HatchStyle50Percent; 100 break; 101 case 4: 102 m_pHatchStyle = HatchStyleLightDownwardDiagonal; 103 break; 104 case 5: 105 m_pHatchStyle = HatchStyleDarkDownwardDiagonal; 106 break; 107 case 6: 108 m_pHatchStyle = HatchStyleWideDownwardDiagonal; 109 break; 110 case 7: 111 m_pHatchStyle = HatchStyleLightVertical; 112 break; 113 case 8: 114 m_pHatchStyle = HatchStyleNarrowVertical; 115 break; 116 } 117 Invalidate(); 118 }
标签:pForeColor,画刷,HatchBrush,pBackColor,AddItem,pStyle,RGB,GDI,255 From: https://www.cnblogs.com/missyou0813/p/17564706.html