实现这个的最简单的方法就是重载ListCtrl的NM_CUSTOMDRAW消息,如下:
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, &Clistctrl_testDlg::OnNMCustomdrawList1)
然后在相关函数中添加处理代码:
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
if ( CDDS_PREPAINT ==pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
// This is the notification message for an item. We'll request
// notifications before each subitem's prepaint stage.
*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{
int nItem=static_cast<int>(pLVCD->nmcd.dwItemSpec );
switch(pLVCD->iSubItem)
{
case 2: break;
case 4: //选择第四列
{
if (nItem % 2 == 0)//选择偶数行
{
COLORREF clrNewTextColor, clrNewBkColor;
clrNewTextColor = RGB(0,0,0);
clrNewBkColor = RGB(198,202,198);
pLVCD->clrText =clrNewTextColor;
pLVCD->clrTextBk =clrNewBkColor;
*pResult = CDRF_DODEFAULT;
break;
}
}
default:
pLVCD->clrText = RGB(0,0,0);
pLVCD->clrTextBk = RGB(255,255,255);
*pResult = CDRF_DODEFAULT;
break;
}
}
///改进一下/
m_ListCtrl.DeleteAllItems();
m_ListCtrl.InsertItem(0,"張三");
m_ListCtrl.SetItemText(0,1,"178CM");
m_ListCtrl.SetItemText(0,2,"70KG");
m_ListCtrl.SetItemText(0,3,"14");
m_ListCtrl.SetItemText(0,4,"男");
m_ListCtrl.SetItemText(0,5,"籃球");
m_ListCtrl.InsertItem(1,"王五");
m_ListCtrl.SetItemText(1,1,"158cm");
m_ListCtrl.SetItemText(1,2,"70kg");
m_ListCtrl.SetItemText(1,3,"12");
m_ListCtrl.SetItemText(1,4,"男");
m_ListCtrl.SetItemText(1,5,"乒乓球");
m_ListCtrl.InsertItem(2,"阿花");
m_ListCtrl.SetItemText(2,1,"168cm");
m_ListCtrl.SetItemText(2,2,"60kg");
m_ListCtrl.SetItemText(2,3,"13");
m_ListCtrl.SetItemText(2,4,"女");
m_ListCtrl.SetItemText(2,5,"排球");
m_ListCtrl.InsertItem(3,"小華");
m_ListCtrl.SetItemText(3,1,"168cm");
m_ListCtrl.SetItemText(3,2,"60kg");
m_ListCtrl.SetItemText(3,3,"13");
m_ListCtrl.SetItemText(3,4,"男");
m_ListCtrl.SetItemText(3,5,"爬山");
m_ListCtrl.InsertItem(4,"大聲");
m_ListCtrl.SetItemText(4,1,"168cm");
m_ListCtrl.SetItemText(4,2,"60kg");
m_ListCtrl.SetItemText(4,3,"16");
m_ListCtrl.SetItemText(4,4,"女");
m_ListCtrl.SetItemText(4,5,"游泳");
m_ListCtrl.InsertItem(5,"高波");
m_ListCtrl.SetItemText(5,1,"168cm");
m_ListCtrl.SetItemText(5,2,"60kg");
m_ListCtrl.SetItemText(5,3,"17");
m_ListCtrl.SetItemText(5,4,"男");
m_ListCtrl.SetItemText(5,5,"跑步");
void CaacolorDlg::OnNMCustomdrawList1(NMHDR *pNMHDR, LRESULT *pResult)
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
if ( CDDS_PREPAINT ==pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
// This is the notification message for an item. We'll request
// notifications before each subitem's prepaint stage.
*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{
int nItem=static_cast<int>(pLVCD->nmcd.dwItemSpec );
switch(pLVCD->iSubItem)
{
case 2:break;
case 4: //列 nItem % 2 == 0 m_ListCtrl.GetItemText(1,3)!="13"
{
//static int i=0;
for(int i=0;i<6;i++)
{
if ((nItem==i)&&(m_ListCtrl.GetItemText(i,3)=="13"))//行
{
COLORREF clrNewTextColor, clrNewBkColor;
clrNewTextColor = RGB(0,0,0);
clrNewBkColor = RGB(255,255,0);
pLVCD->clrText =clrNewTextColor;
pLVCD->clrTextBk =clrNewBkColor;
*pResult = CDRF_DODEFAULT;
break;
}
}
break;
}
case 5:
{
pLVCD->clrText = RGB(0,0,0);
pLVCD->clrTextBk = RGB(255,255,255);
}
break;
default:
//pLVCD->clrText = RGB(0,0,0);
//pLVCD->clrTextBk = RGB(255,255,255);
*pResult = CDRF_DODEFAULT;
break;
}
}
}
///