MFC 改变某个单元格的字体颜色和背景色(转)
原文:https://blog.csdn.net/myruo/article/details/83856211
场景:
需要不同的颜色来区分其他数据
操作步骤:
添加 NM_CUSTOMDRAW 消息响应事件,或者手动添加
实现代码如下:
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
if (CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage)
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if (CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage)
{
*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ((CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage)
{
int nItem = static_cast<int>(pLVCD->nmcd.dwItemSpec);
CString strLoad;
switch (pLVCD->iSubItem)
{
case 2:
break;
case 5:
{
COLORREF clrNewTextColor, clrNewBkColor;
strLoad = m_list1.GetItemText(nItem, pLVCD->iSubItem);
if (!strLoad.Compare(_T("成功")))
{
clrNewTextColor = RGB(0, 255, 0);
}
else
{
clrNewTextColor = RGB(255, 0, 0);
}
clrNewBkColor = RGB(255, 255, 255);
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;
}
}