Click here to Skip to main content
Licence CPOL
First Posted 19 Dec 2005
Views 113,745
Bookmarked 100 times

Alternate Row Colors for the CListCtrl

By | 19 Dec 2005 | Article
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)

About the Author

sdancer75

Web Developer
Paradox Interactive
Greece Greece

Member

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", http://www.paradoxinteractive.gr


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThank Pinmemberfuturejo22:24 14 Oct '11  
GeneralIssue with ClistCtrl having no items PinmemberNitheesh George1:03 7 Dec '10  
GeneralDraw issues when using LVS_EX_DOUBLEBUFFER PinmemberSorin Sbarnea21:38 22 Mar '10  
GeneralRe: Draw issues when using LVS_EX_DOUBLEBUFFER PinmemberLe@rner22:58 18 Jan '11  
GeneralThanks PinmemberRajith Ilangasinghe21:34 4 Jan '10  
Generalexcellent list control Pinmemberateixeira_196214:32 5 Mar '09  
GeneralRe: excellent list control Pinmembersdancer754:09 6 Mar '09  
GeneralWith blank list color not displayed. Pinmember"_$h@nky_"1:56 10 Feb '09  
QuestionChanging Color of lines separating items in grid style report view PinmemberHarish Surana5:10 16 Nov '07  
AnswerRe: Changing Color of lines separating items in grid style report view Pinmembersdancer7521:28 18 Nov '07  
GeneralRe: Changing Color of lines separating items in grid style report view PinmemberHarish Surana3:05 19 Nov '07  
GeneralRe: Changing Color of lines separating items in grid style report view Pinmembersdancer7521:08 19 Nov '07  
GeneralCopyright PinmemberJazzMaTazz11:10 6 Sep '07  
GeneralRe: Copyright Pinmembersdancer751:11 10 Sep '07  
QuestionHow can I implement this in Dialog Based Applixcation Pinmembersheshidar23:15 22 Aug '07  
GeneralHave a question Pinmemberituse19:58 18 Nov '06  
GeneralRe: Have a question Pinmembersdancer7520:32 19 Nov '06  
GeneralRe: Have a question Pinmemberituse1:25 22 Nov '06  
QuestionHow to update codes? PinmemberJian12345674:04 14 Nov '06  
AnswerRe: How to update codes? Pinmembersdancer7520:38 14 Nov '06  
GeneralRe: How to update codes? Pinmembersdancer7520:39 14 Nov '06  
GeneralRe: How to update codes? PinmemberJian123456713:50 15 Nov '06  
GeneralRe: How to update codes? PinmemberMember 310283616:06 18 Jun '09  
GeneralImplementing in CListView Pinmembersoftwaremonkey3:29 11 May '06  
GeneralUpdates for Scrolling, Clipping, Partials PinmemberChuck O'Toole5:36 7 Feb '06  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 19 Dec 2005
Article Copyright 2005 by sdancer75
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid