1,首先使用 ClassWizard 建立一個 CListCtrl 的派生類,在它的頭文件消息響應函數中添加:
// Generated message map functions
protected:
//{{AFX_MSG(CScanFileList) //}}AFX_MSG
afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);
DECLARE_MESSAGE_MAP()
2,相應的,類實現文件添加:
BEGIN_MESSAGE_MAP(CScanFileList, CListCtrlEx)
//{{AFX_MSG_MAP(CScanFileList)
...
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
...
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
3,函數實現:
void CScanFileList::OnCustomDraw ( NMHDR* pNMHDR, LRESULT* pResult )
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
// Take the default processing unless we
// set this to something else below.
*pResult = CDRF_DODEFAULT;
// First thing - check the draw stage. If it''s the control''s prepaint
// stage, then tell Windows we want messages for every item.
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
// 這裡僅僅比較文本,注意:index == pLVCD->nmcd.dwItemSpec
if (GetItemText(pLVCD->nmcd.dwItemSpec, 2) == m_lpszEncrypt)
{
pLVCD->clrText = RGB(255,0,0);
pLVCD->clrTextBk = RGB(255,0,0);
}
// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;
}
}