Click here to Skip to main content
15,884,472 members
Articles / Desktop Programming / MFC
Article

XListBox - Owner-draw CListBox with selectable text and background colors

Rate me:
Please Sign up or sign in to vote.
4.93/5 (31 votes)
27 Feb 2008CPOL3 min read 301K   7K   102   42
XListBox is an owner-draw listbox that provides printf-style formatting and selection of text and background colors

Introduction

Listboxes are still useful for many things. I use them frequently when I need to log something. Usually, logging means flagging some exception or unusual event, so it is nice to be able to use colors to draw user's attention to these items.

Here is my CXListBox class. It is very simple to use, and can be used like a CListBox if you wish. Its power lies in its ability to display lines with text and background colors. Currently, the colors are limited to the 16 basic colors, because of the way the colors are stored.

Implementation

Briefly, here is the implementation: the listbox's style is set to ownerdraw fixed and has strings. Each string that is added to listbox is prefixed by two additional characters — one for text color and one for background color. When it is time to display string, CXListBox::DrawItem looks at these two color bytes to determine how to draw line. This approach is far simpler than allocating a memory block and stuffing the pointer using SetItemData(), although the tradeoff is that you are limited to 16 colors.

XListBox Features

The demo project provides a sample app that allows choice of text, text and background colors, and other options:

screenshot

  1. Enter text in edit box and click on Add String.
  2. Text is displayed using combination of 16 text and background colors. XListBox is implemented using same color numbering as CColorPickerCB class — see ColorPickerCB.cpp.
  3. You can optionally write listbox strings to log file. Note that strings are always appended to end of file, even when InsertString() is used.
  4. The Line Number Options dialog allows you to choose whether to display line numbers, and colors for gutter background and line number text:

    screenshot

  5. XListBox offers printf-style formatting combined with text and background color selection.
  6. Optional right-click context menu — using IDR_XLISTBOX popup menu resource — allows for copying/clearing listbox.

How To Use

To integrate XListBox into your own app, you first need to add following files to your project:

  • XListBox.cpp
  • XListBox.h
  • Clipboard.cpp
  • Clipboard.h

IDR_XLISTBOX Popup Menu Resource

C++
IDR_XLISTBOX MENU DISCARDABLE 
BEGIN
    POPUP "XListBox"
    BEGIN
        MENUITEM "&Select All",   ID_EDIT_SELECT_ALL
        MENUITEM "&Copy",         ID_EDIT_COPY
        MENUITEM "C&lear",        ID_EDIT_CLEAR
    END
END

You will also need IDR_XLISTBOX popup menu resource (if you want context menu). You can add popup menu by calling SetContextMenuId() like this:

C++
m_List.SetContextMenuId(IDR_XLISTBOX);

Next, include header file XListBox.h in dialog's .h file, and create a CXListBox variable (easiest is to let Class WIzard generate a variable for CListBox object, then rename to CXListBox). See XListBoxTestDlg.h for example.

NOTE

The listbox must have styles Owner draw fixed and Has strings.

Here are some examples of how to display text with the CXListBox class, assuming you have variable named m_List:

  • C++
    m_List.AddString(_T("This is a sample string"));
    This adds string to listbox with black text on a white background.
  • C++
    m_List.AddLine(CXListBox::White, CXListBox::Red, _T("This is a sample string"));
    This adds string to listbox with white text on a red background.
  • C++
    m_List.Printf(CXListBox::Blue, CXListBox::White, 0, _T("XListBox Version %d.%d"), nMajor, nMinor);
    This adds string to listbox with blue text on a white background, using printf formatting.

Revision History

Version 1.2 - 2008 February 24

  • Fixed problems using XListBox with VS2005.
  • Added demo project for VS2005.
  • Changed InsertString() to accept colors (defaults to black on white).
  • Changed AddLine(), AddString(), and Printf() to return index of line just added.
  • Added functions GetBackgroundColor(), GetTextColor(), SetBackgroundColor(), and SetTextColor().
  • Added option to display line numbers.
  • Added option to log to file.

Version 1.0 - 2002 March 20

  • Initial public release

Acknowledgments

The XListBox demo uses color picker combobox written by James R. Twine, which may be found here, and the CClipboard class written by David Terracino, which may be found here.

Usage

This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

License

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


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions

 
GeneralRe: Subclassing ListBox so as to provide functionality of both single selection and multiple selection list box Pin
Close Network18-Jan-05 13:08
Close Network18-Jan-05 13:08 
GeneralRe: Subclassing ListBox so as to provide functionality of both single selection and multiple selection list box Pin
Hans Dietrich18-Jan-05 13:41
mentorHans Dietrich18-Jan-05 13:41 
GeneralRe: Subclassing ListBox so as to provide functionality of both single selection and multiple selection list box Pin
Close Network18-Jan-05 20:38
Close Network18-Jan-05 20:38 
GeneralRe: Subclassing ListBox so as to provide functionality of both single selection and multiple selection list box Pin
Close Network26-Jan-05 23:25
Close Network26-Jan-05 23:25 
GeneralListbox not updated Pin
DarthVeda16-Jun-04 2:29
DarthVeda16-Jun-04 2:29 
GeneralRe: Listbox not updated Pin
Hans Dietrich16-Jun-04 3:26
mentorHans Dietrich16-Jun-04 3:26 
GeneralRe: Listbox not updated Pin
DarthVeda16-Jun-04 4:09
DarthVeda16-Jun-04 4:09 
GeneralRe: Listbox not updated Pin
Hans Dietrich16-Jun-04 11:43
mentorHans Dietrich16-Jun-04 11:43 
I got your VC7 project source. When I compile with VC6 and run it, I see entries in "TRC Point" and "Sub Heading" listboxes. So your problem may be something to do with VC7.

Best wishes,
Hans

GeneralRe: Listbox not updated Pin
DarthVeda16-Jun-04 20:31
DarthVeda16-Jun-04 20:31 
GeneralRe: Listbox not updated Pin
Hans Dietrich16-Jun-04 22:46
mentorHans Dietrich16-Jun-04 22:46 
GeneralRe: Listbox not updated Pin
DarthVeda17-Jun-04 0:46
DarthVeda17-Jun-04 0:46 
GeneralASSERT in Winhand.cpp line: 229 Pin
mbeutner30-Apr-04 4:57
mbeutner30-Apr-04 4:57 
GeneralRe: ASSERT in Winhand.cpp line: 229 Pin
Hans Dietrich30-Apr-04 5:25
mentorHans Dietrich30-Apr-04 5:25 
GeneralRe: ASSERT in Winhand.cpp line: 229 Pin
Jesse Chisholm4-Aug-04 20:29
Jesse Chisholm4-Aug-04 20:29 
QuestionWhy add IDR_XLISTBOX popup menu? Pin
btorrey14-Jan-04 14:50
btorrey14-Jan-04 14:50 
AnswerRe: Why add IDR_XLISTBOX popup menu? Pin
Hans Dietrich15-Jan-04 0:37
mentorHans Dietrich15-Jan-04 0:37 
GeneralAbout time Pin
Shay Harel21-Jul-03 8:28
Shay Harel21-Jul-03 8:28 
GeneralThank you very much Pin
cpf2-Jun-03 17:38
cpf2-Jun-03 17:38 
GeneralSetitemimage Pin
Alex Evans30-Dec-03 13:14
Alex Evans30-Dec-03 13:14 
GeneralDoes not work on Doc/View project !!! Pin
kortebi24-Apr-02 1:33
kortebi24-Apr-02 1:33 
GeneralRe: Does not work on Doc/View project !!! Pin
kortebi24-Apr-02 1:56
kortebi24-Apr-02 1:56 

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.