|
| 1 | +// ColorHeaderCtrl.cpp : implementation file |
| 2 | +// |
| 3 | + |
| 4 | +#include "pch.h" |
| 5 | +#include "ColorHeaderCtrl.h" |
| 6 | + |
| 7 | +#ifdef _DEBUG |
| 8 | +#define new DEBUG_NEW |
| 9 | +#undef THIS_FILE |
| 10 | +static char THIS_FILE[] = __FILE__; |
| 11 | +#endif |
| 12 | + |
| 13 | +// CColorHeaderCtrl |
| 14 | + |
| 15 | +IMPLEMENT_DYNAMIC(CColorHeaderCtrl, CHeaderCtrl) |
| 16 | + |
| 17 | +CColorHeaderCtrl::CColorHeaderCtrl() |
| 18 | + : m_bPrint(FALSE) |
| 19 | + , m_bTheme(FALSE) |
| 20 | + , m_bCustomDraw(FALSE) |
| 21 | + , m_crText(GetSysColor(COLOR_WINDOWTEXT)) |
| 22 | + , m_crBackground(GetSysColor(COLOR_WINDOW)) |
| 23 | +{ |
| 24 | + m_crBackgroundHot = m_crBackgroundPressed = m_crBackground; |
| 25 | + m_crBackgroundHotTheme = m_crBackgroundPressedTheme = m_crBackgroundTheme = m_crBackground; |
| 26 | +} |
| 27 | + |
| 28 | +BEGIN_MESSAGE_MAP(CColorHeaderCtrl, CHeaderCtrl) |
| 29 | + //{{AFX_MSG_MAP(CColorHeaderCtrl) |
| 30 | + ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CColorHeaderCtrl::OnNMCustomdraw) |
| 31 | + //}}AFX_MSG_MAP |
| 32 | +END_MESSAGE_MAP() |
| 33 | + |
| 34 | +// CColorHeaderCtrl message handlers |
| 35 | + |
| 36 | +void CColorHeaderCtrl::OnNMCustomdraw(NMHDR* pNMHDR, LRESULT* pResult) |
| 37 | +{ |
| 38 | + if (!m_bCustomDraw) |
| 39 | + return; |
| 40 | + |
| 41 | + LPNMLVCUSTOMDRAW pNMCD = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR); |
| 42 | + // TODO: Add your control notification handler code here |
| 43 | + |
| 44 | + *pResult = CDRF_DODEFAULT; |
| 45 | + |
| 46 | + switch (pNMCD->nmcd.dwDrawStage) |
| 47 | + { |
| 48 | + case CDDS_PREPAINT: |
| 49 | + *pResult = CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYPOSTPAINT; |
| 50 | + break; |
| 51 | + case CDDS_ITEMPREPAINT: |
| 52 | + { |
| 53 | + static HFONT hNewFont = NULL; |
| 54 | + if (!hNewFont) |
| 55 | + { |
| 56 | + LOGFONT lf; |
| 57 | + GetFont()->GetLogFont(&lf); |
| 58 | + lf.lfQuality = NONANTIALIASED_QUALITY; |
| 59 | + hNewFont = ::CreateFontIndirect(&lf); |
| 60 | + } |
| 61 | + SetTextColor(pNMCD->nmcd.hdc, m_crText); |
| 62 | + SelectObject(pNMCD->nmcd.hdc, hNewFont); |
| 63 | + *pResult = CDRF_NEWFONT; |
| 64 | + } |
| 65 | + break; |
| 66 | + case CDDS_POSTPAINT: |
| 67 | + CRect rect; |
| 68 | + GetClientRect(&rect); |
| 69 | + |
| 70 | + CDC MemDC; |
| 71 | + CBitmap bitmap; |
| 72 | + CDC* pDC = CDC::FromHandle(pNMCD->nmcd.hdc); |
| 73 | + MemDC.CreateCompatibleDC(pDC); |
| 74 | + if (bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height())) |
| 75 | + { |
| 76 | + CBitmap* pOldBitmap = static_cast<CBitmap*>(MemDC.SelectObject(&bitmap)); |
| 77 | + if (!m_bPrint) |
| 78 | + { |
| 79 | + m_bPrint = TRUE; |
| 80 | + ::SendMessage(pNMCD->nmcd.hdr.hwndFrom, WM_PRINTCLIENT, (WPARAM)MemDC.GetSafeHdc(), PRF_CLIENT); |
| 81 | + CBrush brush; |
| 82 | + brush.CreateSolidBrush(m_crBackground); |
| 83 | + CBrush* pOldBrush = static_cast<CBrush*>(pDC->SelectObject(&brush)); |
| 84 | + pDC->FillRect(&rect, &brush); |
| 85 | + pDC->SelectObject(pOldBrush); |
| 86 | + DeleteObject(&brush); |
| 87 | + |
| 88 | + if (!m_bTheme) |
| 89 | + { |
| 90 | + const HTHEME hTheme = OpenThemeData(pNMCD->nmcd.hdr.hwndFrom, L"HEADER"); |
| 91 | + if (hTheme) |
| 92 | + { |
| 93 | + CDC TestDC; |
| 94 | + TestDC.CreateCompatibleDC(pDC); |
| 95 | + CBitmap bitmapTest; |
| 96 | + bitmapTest.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height()); |
| 97 | + CBitmap* pOldBitmapTest = static_cast<CBitmap*>(TestDC.SelectObject(&bitmapTest)); |
| 98 | + DrawThemeBackground(hTheme, TestDC.GetSafeHdc(), HP_HEADERITEM, HIS_NORMAL, &rect, NULL); |
| 99 | + m_crBackgroundTheme = TestDC.GetPixel(1, 1); |
| 100 | + DrawThemeBackground(hTheme, TestDC.GetSafeHdc(), HP_HEADERITEM, HIS_HOT, &rect, NULL); |
| 101 | + m_crBackgroundHotTheme = TestDC.GetPixel(1, 1); |
| 102 | + DrawThemeBackground(hTheme, TestDC.GetSafeHdc(), HP_HEADERITEM, HIS_PRESSED, &rect, NULL); |
| 103 | + m_crBackgroundPressedTheme = TestDC.GetPixel(1, 1); |
| 104 | + TestDC.SelectObject(pOldBitmapTest); |
| 105 | + DeleteObject(bitmapTest); |
| 106 | + DeleteDC(TestDC); |
| 107 | + CloseThemeData(hTheme); |
| 108 | + |
| 109 | + m_bTheme = TRUE; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + CDC HotDC, PressedDC; |
| 114 | + HotDC.CreateCompatibleDC(pDC); |
| 115 | + PressedDC.CreateCompatibleDC(pDC); |
| 116 | + CBitmap bitmapHot, bitmapPressed; |
| 117 | + bitmapHot.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height()); |
| 118 | + CBitmap* pOldBitmapHot = static_cast<CBitmap*>(HotDC.SelectObject(&bitmapHot)); |
| 119 | + bitmapPressed.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height()); |
| 120 | + CBitmap* pOldBitmapPressed = static_cast<CBitmap*>(PressedDC.SelectObject(&bitmapPressed)); |
| 121 | + // default color => custom |
| 122 | + pDC->TransparentBlt(0, 0, rect.Width(), rect.Height(), &MemDC, 0, 0, rect.Width(), rect.Height(), m_crBackgroundTheme); |
| 123 | + // Copy pDC to 2 temporary DC |
| 124 | + HotDC.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY); |
| 125 | + PressedDC.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY); |
| 126 | + // hot brush in PressedDC |
| 127 | + CBrush brushHot; |
| 128 | + brushHot.CreateSolidBrush(m_crBackgroundHot); |
| 129 | + CBrush* pOldBrushHot = static_cast<CBrush*>(PressedDC.SelectObject(&brushHot)); |
| 130 | + PressedDC.FillRect(&rect, &brushHot); |
| 131 | + PressedDC.SelectObject(pOldBrushHot); |
| 132 | + DeleteObject(brushHot); |
| 133 | + // Hot color (Light Blue) => Pressed |
| 134 | + PressedDC.TransparentBlt(0, 0, rect.Width(), rect.Height(), &HotDC, 0, 0, rect.Width(), rect.Height(), m_crBackgroundHotTheme); |
| 135 | + // Pressed brush in pDC |
| 136 | + CBrush brushPressed; |
| 137 | + brushPressed.CreateSolidBrush(m_crBackgroundPressed); |
| 138 | + CBrush* pOldBrushPressed = static_cast<CBrush*>(pDC->SelectObject(&brushPressed)); |
| 139 | + pDC->FillRect(&rect, &brushPressed); |
| 140 | + pDC->SelectObject(pOldBrushPressed); |
| 141 | + DeleteObject(brushPressed); |
| 142 | + // Hot => Pressed |
| 143 | + pDC->TransparentBlt(0, 0, rect.Width(), rect.Height(), &PressedDC, 0, 0, rect.Width(), rect.Height(), m_crBackgroundPressedTheme); |
| 144 | + |
| 145 | + HotDC.SelectObject(pOldBitmapHot); |
| 146 | + DeleteObject(bitmapHot); |
| 147 | + DeleteDC(HotDC); |
| 148 | + |
| 149 | + PressedDC.SelectObject(pOldBitmapPressed); |
| 150 | + DeleteObject(bitmapPressed); |
| 151 | + DeleteDC(PressedDC); |
| 152 | + } |
| 153 | + else |
| 154 | + { |
| 155 | + m_bPrint = FALSE; |
| 156 | + } |
| 157 | + MemDC.SelectObject(pOldBitmap); |
| 158 | + DeleteObject(bitmap); |
| 159 | + } |
| 160 | + DeleteDC(MemDC); |
| 161 | + break; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +void CColorHeaderCtrl::UpdateCustomDrawFlag() |
| 166 | +{ |
| 167 | + if (GetSysColor(COLOR_WINDOW) != m_crBackground || |
| 168 | + GetSysColor(COLOR_WINDOWTEXT) != m_crText) |
| 169 | + m_bCustomDraw = TRUE; |
| 170 | + else |
| 171 | + m_bCustomDraw = FALSE; |
| 172 | +} |
0 commit comments