Click here to Skip to main content
Click here to Skip to main content

Alternate Row Colors for the CListCtrl

, 19 Dec 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
How to set alternate row colors for the CListCtrl.

Sample Image


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;

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

  int chunk_height=GetCountPerPage();

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


  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.


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


About the Author

Web Developer Paradox Interactive
Greece Greece
George Papaioannou, Greece
Application & Multimedia Programmer
He is a graduate of the Automation Department of Technological Education Institute of Thessaloniki (Greece), specialized in the Industrial Automation (SCADA, embedded Microcontrollers & PLCs) & Programmer / Analyzer of the Aristotelian University specialized in Telecommunication & Computer Networks. He has a great experience in Programming and Systems Analysis on a variety of computer languages like Assembly, C++, Cobol, Fortran, Pascal and Java. He has also worked in researching programs on appreciation and improvement algorithms for detection systems through a coordinated range of Satellite Systems (Global Positioning Systems). He is the Development Manager of his own company "Paradox Interactive",

Comments and Discussions

GeneralMy vote of 4 Pinmemberiampradeepsharma3-Jul-14 1:27 
QuestionThank Pinmemberfuturejo14-Oct-11 23:24 
GeneralIssue with ClistCtrl having no items PinmemberNitheesh George7-Dec-10 2:03 
GeneralDraw issues when using LVS_EX_DOUBLEBUFFER PinmemberSorin Sbarnea22-Mar-10 22:38 
GeneralRe: Draw issues when using LVS_EX_DOUBLEBUFFER PinmemberLe@rner18-Jan-11 23:58 
GeneralThanks PinmemberRajith Ilangasinghe4-Jan-10 22:34 
Generalexcellent list control Pinmemberateixeira_19625-Mar-09 15:32 
GeneralRe: excellent list control Pinmembersdancer756-Mar-09 5:09 
GeneralWith blank list color not displayed. Pinmember"_$h@nky_"10-Feb-09 2:56 
QuestionChanging Color of lines separating items in grid style report view PinmemberHarish Surana16-Nov-07 6:10 
AnswerRe: Changing Color of lines separating items in grid style report view Pinmembersdancer7518-Nov-07 22:28 
GeneralRe: Changing Color of lines separating items in grid style report view PinmemberHarish Surana19-Nov-07 4:05 
GeneralRe: Changing Color of lines separating items in grid style report view Pinmembersdancer7519-Nov-07 22:08 
GeneralCopyright PinmemberJazzMaTazz6-Sep-07 12:10 
GeneralRe: Copyright Pinmembersdancer7510-Sep-07 2:11 
QuestionHow can I implement this in Dialog Based Applixcation Pinmembersheshidar23-Aug-07 0:15 
GeneralHave a question Pinmemberituse18-Nov-06 20:58 
GeneralRe: Have a question Pinmembersdancer7519-Nov-06 21:32 
GeneralRe: Have a question Pinmemberituse22-Nov-06 2:25 
QuestionHow to update codes? PinmemberJian123456714-Nov-06 5:04 
AnswerRe: How to update codes? Pinmembersdancer7514-Nov-06 21:38 
GeneralRe: How to update codes? Pinmembersdancer7514-Nov-06 21:39 
GeneralRe: How to update codes? PinmemberJian123456715-Nov-06 14:50 
GeneralRe: How to update codes? PinmemberMember 310283618-Jun-09 17:06 
GeneralImplementing in CListView Pinmembersoftwaremonkey11-May-06 4:29 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150302.1 | Last Updated 19 Dec 2005
Article Copyright 2005 by sdancer75
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid