CListCtrl单双行颜色不同

 从CListCtrl派生一个类吧。网上有这类代码。就是添加NM_CUSTOMDRAW的响应函数,在里面可以实现单双行不同颜色,选中行不同颜色:

void CListCtrlEx::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult) 
{
// TODO: Add your control notification handler code here
NMLVCUSTOMDRAW* lplvcd=(NMLVCUSTOMDRAW*)(pNMHDR);
int nRow=(int)lplvcd->nmcd.dwItemSpec;
switch (lplvcd->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
*pResult |= CDRF_NOTIFYITEMDRAW;
break;  
case CDDS_ITEMPREPAINT:
//------------------------------------------------------
// 单行与双行的颜色 
if(nRow%2 == 1)//单行
{
lplvcd->clrTextBk = RGB(255,248,220);
lplvcd->clrText = RGB(0,0,0);
*pResult = CDRF_NEWFONT; 
}
else //双行
{
lplvcd->clrTextBk = RGB(176,196,222);
lplvcd->clrText = RGB(0,0,0);
*pResult = CDRF_NEWFONT; 
}
//-------------------------------------------------------

//------------------------------------------------------
// 打勾项
//------------------------------------------------------

//------------------------------------------------------
// 选中行
if(GetItemState(nRow,CDIS_SELECTED))
{
lplvcd->nmcd.uItemState &= ~CDIS_SELECTED;
lplvcd->clrTextBk = RGB(255,255,0);
lplvcd->clrText = RGB(255,0,0);
*pResult = CDRF_NEWFONT;
}
//------------------------------------------------------

break;
default:
*pResult = CDRF_DODEFAULT; 
break;
}
}

你可能感兴趣的:(CListCtrl单双行颜色不同)