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.
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.
The demo project provides a sample app that allows choice of text,
text and background colors, and other options:
Enter text in edit box and click on Add String.
Text is displayed using combination of 16 text and background colors.
XListBox is implemented using same color numbering as
CColorPickerCB class — see ColorPickerCB.cpp.
You can optionally write listbox strings to log file. Note that strings are
always appended to end of file, even when
InsertString() is used.
The Line Number Options dialog allows you to choose whether to display
line numbers, and colors for gutter background and line number text:
printf-style formatting combined with text and background color selection.
Optional right-click context menu — using
menu resource — allows for copying/clearing listbox.
How To Use
XListBox into your own app, you first need to add
following files to your project:
IDR_XLISTBOX Popup Menu Resource
IDR_XLISTBOX MENU DISCARDABLE
MENUITEM "&Select All", ID_EDIT_SELECT_ALL
MENUITEM "&Copy", ID_EDIT_COPY
MENUITEM "C&lear", ID_EDIT_CLEAR
You will also need
IDR_XLISTBOX popup menu resource
(if you want context menu). You can add popup menu by calling
SetContextMenuId() like this:
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
See XListBoxTestDlg.h for example.
The listbox must have styles Owner draw fixed and Has strings.
Here are some examples of how to display text with the
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
Version 1.2 - 2008 February 24
- Fixed problems using XListBox with VS2005.
- Added demo project for VS2005.
InsertString() to accept colors (defaults
to black on white).
Printf() to return index of line just added.
- Added functions
- Added option to display line numbers.
- Added option to log to file.
Version 1.0 - 2002 March 20
XListBox demo uses color picker combobox
written by James R. Twine,
which may be found
CClipboard class written by David Terracino,
which may be found
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.