我的 MFC 应用程序中有 ClistView 控件。我需要根据其内容为某些项目着色。例如,如果它以“No Response to”开头,我需要将其设为红色。
到目前为止,我已经尝试过
BEGIN_MESSAGE_MAP(CMessageView, CListView)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW,customDraw)
END_MESSAGE_MAP()
void CMessageView::Update()
{
CListCtrl& refCtrl = GetListCtrl();
refCtrl.InsertItem(LVIF_TEXT|LVIF_PARAM,0,CTime::GetCurrentTime().Format("%H:%M:%S"),0,0,0,42);
refCtrl.SetItemText(0,1,"some text");
refCtrl.SetItemText(0,2,"No response to open");
}
void CMessageView::customDraw(NMHDR * pNMHDR, LRESULT * pResult)
{
_ASSERTE(*pResult == 0);
NMLVCUSTOMDRAW * pnmlvcd = (NMLVCUSTOMDRAW *)pNMHDR;
DWORD dwDrawStage = pnmlvcd->nmcd.dwDrawStage;
BOOL bSubItem = dwDrawStage & CDDS_SUBITEM;
dwDrawStage &= ~CDDS_SUBITEM;
switch (dwDrawStage)
{
case CDDS_PREPAINT:
{
*pResult = CDRF_NOTIFYITEMDRAW;
break;
}
case CDDS_ITEMPREPAINT:
case CDDS_SUBITEM:
{
if(pnmlvcd->nmcd.lItemlParam == 42)
{
pnmlvcd->clrText = RGB(255,0,0);
}
*pResult = 0;
break;
}
default:
{
*pResult = 0;
break;
}
}
}
但它不起作用。颜色没有变化。我错过了什么或做错了什么?