- Create the MFC project with view as ListView.
- Apply styles to the view .cpp file.
BOOL CMyListView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= LVS_REPORT;
return CListView::PreCreateWindow(cs);
}
- Add columns to the view .cpp file.
void CMyListView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
CListCtrl& ListCtrl = GetListCtrl();
ListCtrl.InsertColumn(0, "Col-1", LVCFMT_LEFT, 75);
ListCtrl.InsertColumn(1, "Col-2", LVCFMT_LEFT, 100);
ListCtrl.InsertColumn(2, "Col-3", LVCFMT_LEFT, 100);
ListCtrl.InsertColumn(3, "Col-4", LVCFMT_LEFT, 100);
ListCtrl.InsertColumn(4, "Col-5", LVCFMT_LEFT, 100);
CString item;
for(int i=0;i<10;i++)
{
item.Format("Item-%d",i);
ListCtrl.InsertItem(i,item);
}
}
- Add the following line to the message map area of the view header(.h) file.
afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT*
pResult);
- Add the following line to the message map area of the view .cpp file.
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
- Add the OnCustomDraw function.
void CMyListView::OnCustomDraw(NMHDR* pNMHDR,
LRESULT* pResult)
{
*pResult = 0;
COLORREF dwColourTxt = 0;
COLORREF dwColourBk = RGB(255, 255, 255);
LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
switch(lplvcd->nmcd.dwDrawStage)
{
Case CDDS_PREPAINT:
{
*pResult = CDRF_NOTIFYITEMDRAW;
return;
}
Case CDDS_ITEMPREPAINT:
{
if(lplvcd->nmcd.dwItemSpec == 0)
{
dwColourTxt = RGB(255, 0, 0);
}
else if(lplvcd->nmcd.dwItemSpec == 1)
{
dwColourTxt = RGB(0, 255, 0);
}
Else
{
dwColourTxt = RGB(0, 0, 255);
}
lplvcd->clrText = dwColourTxt;
*pResult = CDRF_NOTIFYSUBITEMDRAW;
return;
}
case CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEM:
{
if(lplvcd->iSubItem != 0)
{
if(lplvcd->iSubItem == 1)
{
dwColourTxt = RGB(0, 127, 127);
dwColourBk = RGB(255, 255, 255);
}
Else
{
dwColourTxt = RGB(255, 255, 255);
if(lplvcd->nmcd.dwItemSpec == 0)
{
dwColourBk = RGB(255, 0, 102);
}
else if(lplvcd->nmcd.dwItemSpec == 1)
{
dwColourBk = RGB(204, 255, 153);
}
Else
{
dwColourBk = RGB(0, 102, 204);
}
}
lplvcd->clrText = dwColourTxt;
lplvcd->clrTextBk = dwColourBk;
}
*pResult = CDRF_NEWFONT;
return;
}
}
}