Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC
Article

Alternate Row Colors for the CListCtrl

Rate me:
Please Sign up or sign in to vote.
4.58/5 (30 votes)
19 Dec 2005CPOL1 min read 196.7K   8.6K   109   31
How to set alternate row colors for the CListCtrl.

Sample Image

Introduction

I have seen a lot of requests asking how to implement a list control with different colors in each row. After a long time, taking a lot of useful information from this site, it's time to give back something. So, this MFC wrapper code just replaces the CListCtrl control with one that alternates the row color of the control.

Using the code

This class uses the OnCustomDraw and OnEraseBkgnd to accomplish the alternate color effect. The big job is done in the OnEraseBkgnd function, since this part of the code is responsible to "paint" our object. We have to find how many rows are shown as well as the height of each row. We receive these information from the ::GetCountPerPage and ::GetItemPosition (you can refer to MSDN for details) respectively. From this point and after, things are easy.

BOOL CColoredListCtrl::OnEraseBkgnd(CDC* pDC) 
{
  // TODO: Add your message handler code here
  //       and/or call default

  CRect rect;
  CColoredListCtrl::GetClientRect(rect);


  POINT mypoint;  
  
  CBrush brush0(m_colRow1);
  CBrush brush1(m_colRow2);


 
  int chunk_height=GetCountPerPage();
  pDC->FillRect(&rect,&brush1);

  for (int i=0;i<=chunk_height;i++)
  {
    GetItemPosition(i,&mypoint);
    rect.top=mypoint.y ;
    GetItemPosition(i+1,&mypoint);
    rect.bottom =mypoint.y;
    pDC->FillRect(&rect,i %2 ? &brush1 : &brush0);
  }

  brush0.DeleteObject();
  brush1.DeleteObject();

  return FALSE;
}

To use this code, add the CColoredListCtrl class to your project and replace any CListCtrl with this one.

If you want to change the default color of the rows, just replace the m_colRow1 and m_colRow2 variables in the CColoredListCtrl constructor with the colors you prefer.

The default text color is black RGB(0,0,0). If you also want to change the item's text color then replace the code lplvcd->clrText = RGB(0,0,0); that you can find in the CColoredListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult) function.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Paradox Interactive
Greece Greece
George Papaioannou, Greece
Application & Multimedia Programmer
http://www.paradoxinteractive.gr

Comments and Discussions

 
GeneralMy vote of 4 Pin
iampradeepsharma3-Jul-14 0:27
iampradeepsharma3-Jul-14 0:27 
QuestionThank Pin
futurejo14-Oct-11 22:24
futurejo14-Oct-11 22:24 
GeneralIssue with ClistCtrl having no items Pin
Nitheesh George7-Dec-10 1:03
Nitheesh George7-Dec-10 1:03 
GeneralDraw issues when using LVS_EX_DOUBLEBUFFER Pin
Sorin Sbarnea22-Mar-10 21:38
Sorin Sbarnea22-Mar-10 21:38 
GeneralRe: Draw issues when using LVS_EX_DOUBLEBUFFER Pin
Le@rner18-Jan-11 22:58
Le@rner18-Jan-11 22:58 
GeneralThanks Pin
Rajith Ilangasinghe4-Jan-10 21:34
Rajith Ilangasinghe4-Jan-10 21:34 
Generalexcellent list control Pin
ateixeira_19625-Mar-09 14:32
ateixeira_19625-Mar-09 14:32 
GeneralRe: excellent list control Pin
sdancer756-Mar-09 4:09
sdancer756-Mar-09 4:09 
GeneralWith blank list color not displayed. Pin
Le@rner10-Feb-09 1:56
Le@rner10-Feb-09 1:56 
QuestionChanging Color of lines separating items in grid style report view Pin
Harish Surana16-Nov-07 5:10
Harish Surana16-Nov-07 5:10 
AnswerRe: Changing Color of lines separating items in grid style report view Pin
sdancer7518-Nov-07 21:28
sdancer7518-Nov-07 21:28 
GeneralRe: Changing Color of lines separating items in grid style report view Pin
Harish Surana19-Nov-07 3:05
Harish Surana19-Nov-07 3:05 
GeneralRe: Changing Color of lines separating items in grid style report view Pin
sdancer7519-Nov-07 21:08
sdancer7519-Nov-07 21:08 
GeneralCopyright Pin
JazzMaTazz6-Sep-07 11:10
JazzMaTazz6-Sep-07 11:10 
GeneralRe: Copyright Pin
sdancer7510-Sep-07 1:11
sdancer7510-Sep-07 1:11 
QuestionHow can I implement this in Dialog Based Applixcation Pin
sheshidar22-Aug-07 23:15
sheshidar22-Aug-07 23:15 
GeneralHave a question Pin
ituse18-Nov-06 19:58
ituse18-Nov-06 19:58 
GeneralRe: Have a question Pin
sdancer7519-Nov-06 20:32
sdancer7519-Nov-06 20:32 
GeneralRe: Have a question Pin
ituse22-Nov-06 1:25
ituse22-Nov-06 1:25 
QuestionHow to update codes? Pin
Jian123456714-Nov-06 4:04
Jian123456714-Nov-06 4:04 
AnswerRe: How to update codes? Pin
sdancer7514-Nov-06 20:38
sdancer7514-Nov-06 20:38 
GeneralRe: How to update codes? Pin
sdancer7514-Nov-06 20:39
sdancer7514-Nov-06 20:39 
GeneralRe: How to update codes? Pin
Jian123456715-Nov-06 13:50
Jian123456715-Nov-06 13:50 
GeneralRe: How to update codes? Pin
Member 310283618-Jun-09 16:06
Member 310283618-Jun-09 16:06 
GeneralImplementing in CListView Pin
softwaremonkey11-May-06 3:29
softwaremonkey11-May-06 3:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.