Header Colors and Fonts
So after this thread, I looked to see if anyone had published such code before, and found several more people that asked over the years but no solution was provided. Thought I'd offer a formal Codebank example so the info is easier to find. It's pretty much identical to how you do this for regular List Items, just a few minor differences really. But it still seems that people will find it useful.
Notes
-This works on all types of ListViews, the demo shows both 6.0 and 5.0--- the 5.0 in the picture up top is running with a Common Controls 6 Manifest, if you don't have one of those in your IDE or compile it into your exe, it will look pretty much identical to 6.0. They key difference is the manifest-enabled styling overrides setting the background color, but you can still set font face, color, and effects.
-Note that while the font dialog has a Color option, it's currently ignored and hard-set in the WndProc.
-This doesn't require owner-draw, just responding to the NM_CUSTOMDRAW message:
RequirementsCode:Case NM_CUSTOMDRAW Dim nmcdr As NMCUSTOMDRAW CopyMemory nmcdr, ByVal lParam, LenB(nmcdr) Select Case nmcdr.dwDrawStage Case CDDS_PREPAINT LVWndProc = CDRF_NOTIFYITEMDRAW Exit Function Case CDDS_ITEMPREPAINT If nmcdr.dwItemSpec = 0 Then If gSetBk Then SetBkColor nmcdr.hDC, vbWhite If gSetTxt Then SetTextColor nmcdr.hDC, vbRed If gSetFont Then If hFontItem Then SelectObject nmcdr.hDC, hFontItem Else SelectObject nmcdr.hDC, hFontOrig End If End If ElseIf nmcdr.dwItemSpec = 1 Then If gSetBk Then SetBkColor nmcdr.hDC, vbBlack If gSetTxt Then SetTextColor nmcdr.hDC, vbGreen If gSetFont Then 'Note that if you're setting any per-item, you're really just 'doing them all per item anyway, and shouldn't bother with 'WM_SETFONT, as it will glitch if we don't always do this SelectObject nmcdr.hDC, hFontOrig End If ElseIf nmcdr.dwItemSpec = 2 Then If gSetBk Then SetBkColor nmcdr.hDC, vbYellow If gSetTxt Then SetTextColor nmcdr.hDC, vbBlue If gSetFont Then SelectObject nmcdr.hDC, hFontOrig End If End If LVWndProc = CDRF_NEWFONT Exit Function
While the general technique might work with earlier Windows versions, the demo project uses subclassing methods that limit this implementation to Windows XP and newer.




Reply With Quote
