Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 21: Tree and List Controls Visual C++ and MFC Fundamentals


The pszText is the string that will appear as the text of the column. Just like all the other
member variables, this one is not required but, besides the rectangle that limits the
column header, this member is probably the most important characteristic of a column
because it informs the user as to what this column is used for. The string of this variable
can be provided as a null-terminated value. Like all other strings used in an MFC
application, this string can also be a value of a String Table item. It can also be retrieved
from an array of strings. To set a value for this member variable, add the LVCF_TEXT
value to the mask variable. The length of this string can be specified by assigning a value
to the cchTextMax member variable.

After initializing the LVCOLUMN variable, pass it as the CListCtrl::InsertColumn()
second argument. Here is an example:

BOOL COthersDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here
LVCOLUMN lvColumn;
int nCol;

lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 120;
lvColumn.pszText = "Full Name";
nCol = m_List.InsertColumn(0, &lvColumn);

lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 100;
lvColumn.pszText = "Profession";
m_List.InsertColumn(1, &lvColumn);

lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 80;
lvColumn.pszText = "Fav Sport";
m_List.InsertColumn(2, &lvColumn);

lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 75;
lvColumn.pszText = "Hobby";
m_List.InsertColumn(3, &lvColumn);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
Free download pdf