VC 中改变ListCtrl每一行的文字颜色

通过处理NM_CUSTOMDRAW,可以实现你的功能!但是NM_CUSTOMDRAW在Class Wizard中有可能看不到,不用管他,直接按照小面的方法添加处理过程即可!
1. 在消息映射表中
BEGIN_MESSAGE_MAP(CIHISSERVERView, CListView)
//{{AFX_MSG_MAP(CIHISSERVERView)
...
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
...
//}}AFX_MSG_MAP
// Standard printing commands
END_MESSAGE_MAP()
2.在头文件中
afx_msg void OnCustomDraw(NMHDR*, LRESULT*);
3.在cpp文件中
void CIHISSERVERView::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
// Contains information specific to an NM_CUSTOMDRAW
// notification message sent by a list-view control.
// mean:draw each item.set txt color,bkcolor....
NMLVCUSTOMDRAW* pLVCD = (NMLVCUSTOMDRAW*)(pNMHDR);
// Take the default processing unless we set this to something else below.
*pResult = CDRF_NEWFONT;
// First thing - check the draw stage. If it's the control's prepaint
// stage, then tell Windows we want messages for every item.
if ( pLVCD->nmcd.dwDrawStage==CDDS_PREPAINT)
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
// This is the notification message for an item. We'll request
// notifications before each subitem's prepaint stage.
else if ( pLVCD->nmcd.dwDrawStage==CDDS_ITEMPREPAINT )
{
COLORREF m_crTextBk , m_clrText;
int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);
// 判断使ListCtrl不同颜色现实的条件 CListCtrl &m_list = GetListCtrl();
CString str1 = m_list.GetItemText(nItem ,15);
bool bDBImplFail = false ;
if (str1 == "信息不祥")
{
m_crTextBk = RGB(255, 255, 0) ;
m_clrText = RGB(128, 0, 128) ;
} else
{ m_crTextBk = RGB(150, 255, 255);
m_clrText = RGB(12,26,234);
}
pLVCD->clrTextBk = m_crTextBk;
pLVCD->clrText = m_clrText;
*pResult = CDRF_DODEFAULT;
}
}


然后要在listctrl中增加记录的时候,示例代码如下:
// TODO: 在此添加额外的初始化代码
m_msgListCtrl.InsertColumn(0,"时间");
m_msgListCtrl.InsertColumn(1,"内容",LVCFMT_LEFT);
m_msgListCtrl.SetColumnWidth(0,50);
m_msgListCtrl.SetColumnWidth(1,500);

for(int i=0;i<50;i++){
int index=m_msgListCtrl.InsertItem(LVIF_TEXT|LVIF_STATE,"内容1");
m_msgListCtrl.SetItemText(index,1,"什么?");
}

记住:用InsertItem插入一行的时候,其item的值并不是从0一直递增的,必须像上面一样写。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值