<!-- STEP 2. Include download and sample image information -->
Ever been annoyed at the amount of code you need just to insert a few rows and their
columns' text into a
CListCtrl while using the 'Report' style?
This little MFC subclassing tutorial provides a handy little enhancement to the
CListCtrl, which can make life easier for both beginners and
The code and text that follows assumes you have generated a typcal Dialog based
application with the MFC Application (.exe) Appwizard in MS Visual C++.
Derive a new
CListCtrl based class (here
In the VC IDE, this is as simple as selcting 'Add Class...' from the MFC Class wizard
then, in the new class dialog, naming the control and selecting
the base class in the drop down list.
To use the new class, start by adding a
CListCtrl to the dialog (be sure
to select 'Report' in the Styles page of the proprties for the new control) then, in the
Member Variables page of the MFC Class Wizard (ctrl+w), add a control variable for the
newly created item (
IDC_LIST1, or whatever ID you supplied) - but instead of
accepting the default
CListCtrl as the type, you will be able to select the
CMyListCtrl type. Call the variable
<font color="#0000FF">#include</font> "MyListCtrl.h"
in the header of the dialog class (in this case, the main dialog of the app) and you're
In the code that VC generates for the new class, add the following:
To test, you might add a command button that executes the above code. Also, this
assumes that you have initialized the list with 3 columns - for the sake of this simple
example, you might provide the following in the OnInitDialog method of the dialog class:
<font color="#008040">m_myList.InsertColumn(0, "Column 1");
m_myList.InsertColumn(1, "Column 2");
m_myList.InsertColumn(2, "Column 3");