Click here to Skip to main content
13,901,400 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


51 bookmarked
Posted 4 Mar 2008
Licenced CPL

Adding Color to Listview with Customdraw

, 8 Mar 2008
Rate this:
Please Sign up or sign in to vote.
Implementation of a CListCtrl control derived class that can be used to set color of rows, columns, cells.


I'm trying to learn about CustomDraw. Since MFC ListView lacks color :), I wrote this little class that will let you set color of any row, column, cell by extending CListCtrl.

Additionally you can set the 'Grid Mode' with any two colors which automatically colors the view. There is one more 'Alternate row mode' which lets you auto set colors of alternate rows. There are a few more functions like 'text mask mode' which lets the text color to be auto set based on the background color of the cell by inverting colors.

One more feature that people might find useful is that the color of the row/cell doesn't change even if new rows are inserted or existing rows are deleted. That means that deleting row 2 doesn't make row 3 to be displayed in row two's color. Though that sounds like expected behavior, half of the code went for tracking rows and cells :(.

I needed all this functionality for my 'pet' project. Since I could not find any, I'm forced to write this class. (Is MFC dead or what :(, I could hardly find new articles these days.)

Comments/suggestions other than asking me to improve my coding standards :) are welcome.


We need to capture NM_CUSTOMDRAW notification to be able to set the background color or text color. In the item prepaint stage of painting, we need to request for sub item painting. This part was simple when compared to the grunt work needed to keep track of item colors.

The objective of this article isn't to explain NM_CUSTOMDRAW in detail, but to give out an easy to use class. If you want to learn about custom draw in detail, go through this article.

Using the Code

The following are the new methods that can be used. The names hopefully are self explanatory. 'Clr' is for 'clear'. So ClrRowColor() clears the row color if already set. ResetColors() will reset everything back to normal:

void SetRowColor(COLORREF cf, long row);
void SetColColor(COLORREF cf, long col);
void SetCellColor(COLORREF cf, long row, long col);
void ClrRowColor(long row);
void ClrColColor(long col);
void ClrCellColor(long row,long col);
void SetGridMode(bool TurnOn,COLORREF x=0,COLORREF y=0);
void SetTextMask(bool TurnOn);
void SetAltRowColors(bool TurnOn,COLORREF x=0,COLORREF y=0);
void ResetColors(void);

For SetGridMode() and SetAltRowColors, you may need to experiment with x and y values to make the view look good.

You must keep in mind that column color will be painted over row color. And cell color will have precedence over both row, column color. GridMode and AltRowMode will have least precedence with respect to row/column/cell color. You can try and see what I mean.

To use the code include "CColorListCtrl.h" and use CColorListCtrl class instead of CListCtrl.


To get the above effect, I have used:



To get the above effect, I have used:



To get the above effect, I have used:


Examples two, three have been run with 'MaskMode' on and the first with mask mode off. You can clearly note the difference. To set the mask mode, you have to use:


Points of Interest

The most annoying part was that OnPaint()/UpdateWindow() methods don't seem to update ListView control. So I had to resort to RedrawWindow() method (found about this in one of the articles). Also, I haven't tested my code with sorting option turned on. So you may have to find that out.


  • 05 Mar 2008: Initial release


This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


About the Author

Option Greek
India India
One of the many obscure programmers dreaming at their desks.

You may also be interested in...

Comments and Discussions

Praisegreat code. Pin
qhbo18-May-16 20:28
memberqhbo18-May-16 20:28 
QuestionBlinking and minimize/maximize problem Pin
mmccm16-Feb-14 6:37
membermmccm16-Feb-14 6:37 
AnswerRe: Blinking and minimize/maximize problem Pin
Option Greek27-Feb-16 19:34
memberOption Greek27-Feb-16 19:34 
QuestionHow can set Item Text Color? Pin
Le@rner4-Nov-09 18:44
memberLe@rner4-Nov-09 18:44 
AnswerRe: How can set Item Text Color? Pin
Option Greek4-Nov-09 22:56
memberOption Greek4-Nov-09 22:56 
GeneralRe: How can set Item Text Color? Pin
Le@rner4-Nov-09 23:03
memberLe@rner4-Nov-09 23:03 
GeneralRe: How can set Item Text Color? Pin
Option Greek4-Nov-09 23:12
memberOption Greek4-Nov-09 23:12 
GeneralGreat Article. Pin
FISH78612-May-09 10:10
memberFISH78612-May-09 10:10 
GeneralRe: Great Article. Pin
Option Greek4-Nov-09 22:59
memberOption Greek4-Nov-09 22:59 
GeneralUse SetItemData Pin
Rolf Kristensen10-Mar-08 22:57
memberRolf Kristensen10-Mar-08 22:57 
GeneralRe: Use SetItemData Pin
Option Greek10-Mar-08 23:20
memberOption Greek10-Mar-08 23:20 
GeneralRe: Use SetItemData Pin
Rolf Kristensen11-Mar-08 1:37
memberRolf Kristensen11-Mar-08 1:37 
GeneralRe: Use SetItemData Pin
Option Greek11-Mar-08 3:01
memberOption Greek11-Mar-08 3:01 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 8 Mar 2008
Article Copyright 2008 by Option Greek
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid