之前找了很多!没找到合适的,似乎都要扩展个自定义类,比较麻烦,后来发现个更简单的方法!
首先:重载一个NM_CUSTOMDRAW消息

然后:就自动生成了这个东东

之后:你改为
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
*pResult = 0;
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 )
{
COLORREF m_clrTextBk;
int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);
// 判断使ListCtrl不同颜色现实的条件
CString str = m_listResult.GetItemText(nItem ,2);
std::wstring w_str=str.GetBuffer();
std::string s_tr=_bstr_t(w_str.c_str());
float m_height=(float)atof(s_tr.c_str());
if (m_height<m_distance&&2==pLVCD->iSubItem)
{
// 设置该列红色
m_clrTextBk = RGB(255, 0, 0);
}
else
{
m_clrTextBk = RGB(255, 255, 255) ;
}
pLVCD->clrTextBk = m_clrTextBk;
*pResult = CDRF_DODEFAULT;
}
NMLVCUSTOMDRAW结构体
typedef struct tagNMLVCUSTOMDRAW {
NMCUSTOMDRAW nmcd;
COLORREF clrText;
COLORREF clrTextBk;
} NMLVCUSTOMDRAW, *LPNMLVCUSTOMDRAW;
clrText 表示字体颜色clrTextBk 表示背景色
本文介绍了一种在不扩展自定义类的情况下实现 ListCtrl 控件项颜色自定义的方法。通过重载 NM_CUSTOMDRAW 消息,根据特定条件改变列表项背景色,实现了列表项的动态颜色变化。
3608

被折叠的 条评论
为什么被折叠?



