Visual C++ - CListCtrl Applying Different Colors for each row
We might be using CListCtrl a lot in Visual C++ project. It is often necessary to have the different colors for each row. It is very easy to do it. Look at the following code and how it is used in the sample project.
Source Code
// CStudentInformationSystemDlg dialog
class CStudentInformationSystemDlg : public CDialog
{
.....
afx_msg void OnNMCustomdrawList1(NMHDR *pNMHDR, LRESULT *pResult);
};
// StudentInformationSystemDlg.cpp : implementation file
//
BEGIN_MESSAGE_MAP(CStudentInformationSystemDlg, CDialog)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, &CStudentInformationSystemDlg::OnNMCustomdrawList1)
END_MESSAGE_MAP()
void CStudentInformationSystemDlg::OnNMCustomdrawList1(NMHDR *pNMHDR, LRESULT *pResult)
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
*pResult = CDRF_DODEFAULT;
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
COLORREF crText = RGB(0,0,0);
if ((pLVCD->nmcd.dwItemSpec %2) == 0) // even lines
crText = RGB(255,0,0);
pLVCD->clrText = crText;
*pResult = CDRF_DODEFAULT;
}
}
Click here to download the VS 2005 Project file and executable
Output
|