List Control for MFC Applications

Table of Contents

Introduction

When I faced a necessity in List control with the ability to display tooltips on arbitrary cells, I was discouraged with the fact that such obvious feature simply doesn’t exist in standard MFC CListCtrl class. That was the main reason behind the creation of this control.

Long Story Short

CListEx is a CMFCListCtrl derived class with cells tooltip ability, and even more.
It works with LVS_OWNERDRAWFIXED and LVS_REPORT styles.
The tooltiping is achieved with the public method:

With this method, you can set a tooltip for any cell (item/subitem) in the list, and it will show up on mouse hovering.
Tooltips are implemented as Windows balloons.

To remove tooltip from a cell, just set it again with empty string L"".
Control was built and tested in Visual Studio 2019.

Usage

The usage of the control is pretty easy:

Copy ListEx folder into your project's folder.

Add all files from ListEx folder into your project.

Add #include "ListEx/ListEx.h" where you are supposed to use the control.

Control uses its own namespace - LISTEX. So it's up to you, whether to use namespace prefix before declarations:

LISTEX::

or to define namespace in the source file's beginning:

usingnamespace LISTEX;

Create

Manually

Create is the main method to create list control. It takes one argument - reference to the LISTEXCREATESTRUCT structure. The LISTEXCREATESTRUCT is a helper structure which fields are described below. This struct is also used in SetColor method.

With LISTEXCREATESTRUCT structure, you can set a plethora of list’s aspects:

Color of the list text and bk (background). Bk is set separately for odd and even rows

Color of the list header

Height of the list header

Font of the list header, and font of list itself

Color of individual header's columns

Color of tooltip's window text and bk

Color of the text and bk of a cell that has tooltip

Color of list grid, and even its width

Color of row when it's selected

In Dialog

To create list control in Dialog, you can manually create it with the Create method.
But most of the times, you prefer to place a list control onto the Dialog template dragging it from the Toolbox within Visual studio.
To use the latter approach, follow these steps:

Put standard list control from the toolbox onto your dialog template and make it a desirable size.

Right click on the control and choose Add variable from the drop-down menu.

In the Variable type: field put CListEx

In OnInitDialog method, make a call to myList.CreateDialogCtrl();

Tooltips

To set a tooltip for a given cell, just write:

myList.SetCellTooltip(0, 1, L"Tooltip text", L"Tooltip caption:");

This will set a tooltip for cell (0, 1) with the text: Tooltip text, and the caption Tooltip caption.

Menu

CListEx class possesses an innate ability to set popup menu for individual cells, as well as for the whole list.

Handle Menu Clicks

When user clicks a menu, CListEx sends WM_NOTIFY message, with NMITEMACTIVATEstruct pointer as lParam, to its parent window. So, in order to properly handle clicks, you have to process this message in your list's parent window:

I rather like this control extension so I did little hacking on it and added a feature or two. Here's a screen shot of it action with my little testing application : screenshot[^]

I use a lot of dialogs and form views and I don't call Create in those scenarios. That means I had to add a few methods to access to private data members. Here's the code in InitDialog() that was used to generate what is in the screen shot:

I just added the ability to set the individual cells of the header. Nothing major.

I also made it work in MBCS mode. I added it to my little library that supports 32 and 64-bit modes, MBCS and Unicode, dynamic and static RTL, and debug and release modes. That adds up to 16 different targets. Very good work. Thanks!

"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

I would like to suggest an additional feature. It would be nice to be able to set the list control to display the text in alternating background colors for each row. Usually, one row is just a little darker than the other.

"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"