Environment: VC6 SP4, NT4 SP3, Win9x/Me/2K/XP, IE 4.0 or above
To set the background color of a column, the list view control has to be owner drawn. The textbackground and textcolor of items can be set by using ownerdraw, but the empty space in the listview remains. For this, we have to override OnEraseBkgnd(CDC* pDC).
// Add this to the message map
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
ON_WM_ERASEBKGND()// You need to declare the oncustomdraw message handlers to
// the header
afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);// Then add this code
void CMyView::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
*pResult = 0;LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
switch(lplvcd->nmcd.dwDrawStage)
{
case CDDS_PREPAINT :
{
*pResult = CDRF_NOTIFYITEMDRAW;
return;
}// Modify item text and or background
case CDDS_ITEMPREPAINT:
{
lplvcd->clrText = RGB(0,0,0);
// If you want the sub items the same as the item,
// set *pResult to CDRF_NEWFONT
*pResult = CDRF_NOTIFYSUBITEMDRAW;
return;
}// Modify sub item text and/or background
case CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEM:
{
if(lplvcd->iSubItem %2){
lplvcd->clrTextBk = RGB(255,255,255);
}
else{
lplvcd->clrTextBk = RGB(247,247,247);
}*pResult = CDRF_NEWFONT;
return;
}
}}
BOOL CMyView::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetClientRect(rect);CBrush brush0(RGB(247, 247, 247));
CBrush brush1(RGB(255, 255, 255));SCROLLINFO si;
ZeroMemory(&si, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
GetScrollInfo(SB_HORZ, &si);
rect.left -= si.nPos;for(int i=0;i<=GetListCtrl().GetHeaderCtrl()
->GetItemCount();i++){
rect.right = rect.left+GetListCtrl().GetColumnWidth(i);
pDC->FillRect(&rect,i%2 ? &brush1 : &brush0);
rect.left += GetListCtrl().GetColumnWidth(i);
}brush0.DeleteObject();
brush1.DeleteObject();return FALSE;
}