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

Adding Color to Listview with Customdraw

, 8 Mar 2008 CPL
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.

Introduction

I'm trying to learn about CustomDraw. Since MFC ListView lacks color Smile | :) , 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 Frown | :(

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 Frown | :( I could hardly find new articles these days.)

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

Background

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.

rowCell.jpg

To get the above effect, I have used:

m_list1.SetRowColor(RGB(0,120,0),0);           
m_list1.SetCellColor(RGB(153,102,102),2,1);

Grid.jpg

To get the above effect, I have used:

m_list1.SetGridMode(true,RGB(0,255,255),RGB(153,153,102));

AltRows.jpg

To get the above effect, I have used:

m_list1.SetAltRowColors(true,RGB(0,255,255),RGB(153,153,102));

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:

m_list1.SetTextMask(true);

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.

History

  • 05 Mar 2008: Initial release

License

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

Share

About the Author

Option Greek

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

Comments and Discussions

 
QuestionBlinking and minimize/maximize problem Pinmembermmccm16-Feb-14 7:37 
QuestionHow can set Item Text Color? PinmemberLe@rner4-Nov-09 19:44 
AnswerRe: How can set Item Text Color? PinmemberOption Greek4-Nov-09 23:56 
GeneralRe: How can set Item Text Color? PinmemberLe@rner5-Nov-09 0:03 
GeneralRe: How can set Item Text Color? PinmemberOption Greek5-Nov-09 0:12 
GeneralGreat Article. PinmemberFISH78612-May-09 11:10 
GeneralRe: Great Article. PinmemberOption Greek4-Nov-09 23:59 
GeneralUse SetItemData PinmemberSnakefoot10-Mar-08 23:57 
GeneralRe: Use SetItemData PinmemberOption Greek11-Mar-08 0:20 
GeneralRe: Use SetItemData PinmemberSnakefoot11-Mar-08 2:37 
GeneralRe: Use SetItemData PinmemberOption Greek11-Mar-08 4:01 

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.141216.1 | Last Updated 8 Mar 2008
Article Copyright 2008 by Option Greek
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid