Visual C++ and MFC Fundamentals Chapter 21: Tree and List Controls
The iOrder member variable is used to identify the column addressed by the
LVCOLUMN variable.
Besides, or instead of, the above version of the CListCtrl::InsertColumn() method, you
can use the following version to create columns for the control:
int InsertColumn(int nCol, LPCTSTR lpszColumnHeading, int nFormat = LVCFMT_LEFT,
int nWidth = -1, int nSubItem = -1);
This version simplifies the first a little bit. The nCol argument is the index of the column
that will be configured. The second argument, lpszColumnHeading, is the string that will
be displayed on the column header. It follows the same rules as the
LVCOLUMN::pszText member variable.
The optional nFormat argument is used to specify the horizontal alignment of the
lpszColumnHeading text. It can be set to LVCFMT_LEFT for left alignment (the
default), LVCFMT_CENTER for center alignment, or LVCFMT_RIGHT for right
alignment. If you do not specify this argument, the text would be aligned to the left. The
nWidth ardument is used to set the width of the column header in pixels. If you do not
want to specify this argument, pass it at –1. The nSubItem is used to set the index of the
sub item used on the current column.
With the columns configured, you must provide a string that will be displayed under a
particular column header for an item. To do this, you must first specify which item will
use the information you are going to add. The InsertColumn() method returns an integer
that is the index of the new item. You can use this returned value to identify the column
whose information you are adding. Then to specify a string for each column of the
current item, call the CListCtrl::SetItemText() method. Its syntax is:
BOOL SetItemText(int nItem, int nSubItem, LPTSTR lpszText);
The nItem argument is the index of the column whose information you are adding. It can
be the return value of a previously called InsertColumn(). The pieces of information for
each item are stored in a 0-based array. The index of the current sub item is specified
using the nSubItem argument. The lpszText is the actual text that will display under the
column for the current item.
Here is an example:
BOOL COthersDlg::OnInitDialog()
{
CDialog::OnInitDialog();