Click here to Skip to main content
Licence CPOL
First Posted 20 Mar 2002
Views 204,468
Downloads 3,486
Bookmarked 80 times

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

By | 27 Feb 2008 | Article
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

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:

    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:

  • m_List.AddString(_T("This is a sample string"));
    This adds string to listbox with black text on a white background.
  • 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.
  • 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)

About the Author

Hans Dietrich

Software Developer (Senior)
Hans Dietrich Software
United States United States

Member

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.







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
GeneralMy vote of 5 PinmemberDohwan15:41 8 Dec '11  
QuestionC# version? PinmemberJim Turner15:09 4 Mar '10  
AnswerRe: C# version? PinmentorHans Dietrich3:30 15 Apr '11  
GeneralScroll lock when mouseover. Pinmembermensfort3:56 4 Dec '09  
Questionhow can set font,I set font but not have effec Pinmemberituse23:32 1 Jul '08  
AnswerRe: how can set font,I set font but not have effec PinmvpHans Dietrich0:02 2 Jul '08  
QuestionListbox background color in Forms.Net PinmemberJanBorup21:08 8 Feb '07  
AnswerRe: Listbox background color in Forms.Net PinmemberZero2Cool4:10 1 Oct '08  
QuestionCan Any Body Provide the Port for this code to EVC ++3.0 Pinmembernaveenkm8422:24 21 Aug '06  
NewsOk in .NET, minor tweaks PinmemberDanJL4:09 23 Nov '05  
GeneralRe: Ok in .NET, minor tweaks PinmemberHans Dietrich5:24 23 Nov '05  
QuestionHow to get the text color of the listbox item Pinmembero2122:44 29 Aug '05  
QuestionHow2use CXListBox not as CDialog member ? Pinmemberronen_w3:05 17 Jul '05  
GeneralOK... but..! Pinmemberhw7704113:49 20 May '05  
GeneralRe: OK... but..! PinmemberShaojiang6:53 2 Jun '05  
GeneralHandling context menu PinsussAnonymous14:48 3 May '05  
Generalinserting at a specified postion PinsussAnonymous19:56 16 Feb '05  
GeneralRe: inserting at a specified postion PinsussAnonymous16:25 17 Feb '05  
GeneralSubclassing ListBox so as to provide functionality of both single selection and multiple selection list box PinmemberNice Özgürce6:49 18 Jan '05  
GeneralRe: Subclassing ListBox so as to provide functionality of both single selection and multiple selection list box PinmemberHans Dietrich7:39 18 Jan '05  
GeneralRe: Subclassing ListBox so as to provide functionality of both single selection and multiple selection list box PinmemberNice Özgürce13:08 18 Jan '05  
GeneralRe: Subclassing ListBox so as to provide functionality of both single selection and multiple selection list box PinmemberHans Dietrich13:41 18 Jan '05  
GeneralRe: Subclassing ListBox so as to provide functionality of both single selection and multiple selection list box PinmemberNice Özgürce20:38 18 Jan '05  
GeneralRe: Subclassing ListBox so as to provide functionality of both single selection and multiple selection list box PinmemberNice Özgürce23:25 26 Jan '05  
Hi again
 
I don't know if this is the right platz to ask a similar question but since i get answers very fast here thanks to Mr. Hans Dietrich, I see no problem with submitting my question here.
 
Is it possible to pass contents(items) of a listbox (on parent window) to a combobox (on popup window) DIRECTLY, like below ?
 
popup dlg;
dlg.m_combo1 = m_list2cs;
dlg.DoModal();
 
where m_combo1 and m_list2cs (public variables for controls associated) can only be selected as "category=value" and "variable type=CString" with ClassWizard.
 
Is this not possible with classwizard or with Visual C++ 6.0.
 
Because otherwise I will first have to put items of listbox into a CString array with some delimiter, pass it to popup and unzip individual items into the combox. Is this the most suitable way ?
 
Thanks a lot,
 
Nice Özgürce
GeneralListbox not updated PinmemberDarthVeda2:29 16 Jun '04  

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
Web03 | 2.5.120528.1 | Last Updated 27 Feb 2008
Article Copyright 2002 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid