自绘CListCtrl 间隔行颜色变换

用CMFCListCtrl 的话非常简单.重载GetCellColor就行. 对于CListCtrl,使用下面的方法.

 

BOOL CReportCtrl::OnEraseBkgnd( CDC* PaintDC ) { if (_IsUseRowColor()) { CRect ctrl; CRect rect; CRect head; int itemHeight; CDC *pDC = GetDC(); // non-clipped DC CBrush brush0(m_colRow1); CBrush brush1(m_colRow2); GetClientRect(ctrl); GetHeaderCtrl().GetClientRect(head); PaintDC->FillRect(&ctrl, &brush0); int index = GetTopIndex(); int last_visible_index = index + GetCountPerPage() + 1; // since it's rare that an exact number fits, last visible may be a partial if (GetItemRect(index, &rect, LVIR_BOUNDS) != 0) { while (index <= last_visible_index) { GetItemRect(index, &rect, LVIR_BOUNDS); itemHeight = rect.Height(); PaintDC->FillRect(&rect, index % 2 ? &brush1 : &brush0); ctrl.left = rect.right; ctrl.top = max(rect.top, head.bottom); ctrl.bottom = rect.bottom; pDC->FillRect(&ctrl, index % 2 ? &brush1 : &brush0); // fill part of control without column data rect.top = rect.bottom; rect.bottom = rect.top + itemHeight; index++; } } brush0.DeleteObject(); brush1.DeleteObject(); ReleaseDC(pDC); return FALSE; } return FALSE; } void CReportCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult) { *pResult = 0; LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR; int iRow = lplvcd->nmcd.dwItemSpec; switch(lplvcd->nmcd.dwDrawStage) { case CDDS_PREPAINT : { *pResult = CDRF_NOTIFYITEMDRAW; return; } // Modify item text and or background case CDDS_ITEMPREPAINT: { lplvcd->clrText = GetTextColor(); // 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 (_IsUseRowColor()) { if(iRow % 2) { lplvcd->clrTextBk = m_colRow2; } else { lplvcd->clrTextBk = m_colRow1; } } else { lplvcd->clrTextBk = GetTextBkColor(); } } default: { *pResult = CDRF_DODEFAULT; return; } } }  

你可能感兴趣的:(自绘CListCtrl 间隔行颜色变换)