clistctrl 如何设定某一行的颜色

2024-12-25 14:10:34
推荐回答(1个)
回答1:

在listctrl的NM_Customdraw消息响应函数里弄:例子如下
LRESULT CXXXDialog::OnNMCustomdrawListLayer(int idCtrl, LPNMHDR pNMHDR, BOOL& bHandled)
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast(pNMHDR);
pLVCD->clrFace=RGB(255,0,0);
if ( CDDS_PREPAINT ==pLVCD->nmcd.dwDrawStage )
{
return CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{//这里可以编写代码设置某一行的颜色
int nItem=static_cast(pLVCD->nmcd.dwItemSpec );
if (nItem==1)
{
long r=100;
long g=0;
long b=0;
pLVCD->clrTextBk=RGB(r,g,b);
}

return CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{//这里可以设定具体某一行某一格的背景颜色
int nItem=static_cast(pLVCD->nmcd.dwItemSpec );
int nSubItem=pLVCD->iSubItem;
if (nSubItem==4)
{
long r=100;
long g=0;
long b=0;
pLVCD->clrTextBk=RGB(r,g,b);

}
}

return 0;
}