CColorListBox - A list box with colored strings

Environment: Windows NT4 SP4, Visual C++ 6

ColorLBTest

This CColorListBox class is based on Patrice Godard's colored list box class of the same name, with the following additions:

  • The focus rectangle is now drawn only when the control has the input focus.
  • Items in the list are properly colored when the list box is disabled.
  • Items in the list are properly displayed when the LBS_USETABSTOPS style is set.
  • The entire rectangle of the selected item is now colored, instead of just the text background rectangle.
  • When an item is selected, the text color inverts, and the highlight color is the original text color (like the Cyan list item in the above graphic).

The following public methods are used to set the item text and/or color:


int AddString(LPCTSTR lpszItem);
int AddString(LPCTSTR lpszItem, COLORREF rgb);
int InsertString(int nIndex, LPCTSTR lpszItem);
int InsertString(int nIndex, LPCTSTR lpszItem, COLORREF rgb);
void SetItemColor(int nIndex, COLORREF rgb);

Follow these steps to add a CColorListBox to a dialog:

  • Add ColorListBox.cpp and ColorListBox.h to your project.
  • Add include "ColorListBox.h" to the header file of your dialog class.
  • Add a list box control to your dialog, with the Owner Draw style set to Fixed or Variable, and with the Has Strings style checked.
  • Use ClassWizard to add a member variable of type CColorListBox for the list box you just added. If CColorListBox is not in the Variable Type list, choose CListBox, and manually change the type to CColorListBox later.

Now add or insert strings into the list box, using the overloaded functions:


m_lbColor.AddString("Green", RGB(0, 255, 0));
m_lbColor.InsertString(0, "Red", RGB(255, 0, 0));

Downloads

Download demo project - 20 Kb
Download source - 4 Kb


Comments

  • god job!

    Posted by marine on 06/02/2012 11:58am

    thanks

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Today's "average" business in general is ever more reliant on technology and the Internet. Mobility is the most often cited business trend that has transformed the way many of us work and communicate. From an IT security perspective, this means that protection methods and tools from even a few years ago are rapidly becoming "unfit for purpose." This guide provides crucial facts to assist you in building a robust business case, meeting the demands of your business, and protecting against threats now and in the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds