Columns control the way items and their subitems are displayed in report view. Each column has a title and width and is associated with a specific subitem. The attributes of a column are defined by an LVCOLUMN structure.
To add a column to a list view control, use the LVM_INSERTCOLUMN message. To delete a column, use the LVM_DELETECOLUMN message. You can retrieve and change the properties of an existing column by using the LVM_GETCOLUMN and LVM_SETCOLUMN messages. To retrieve or change a column width, use the LVM_GETCOLUMNWIDTH and LVM_SETCOLUMNWIDTH messages.
Unless the LVS_NOCOLUMNHEADER window style is specified, column headers appear in report view. A user can tap a column header, which causes the list view control to send an LVN_COLUMNCLICK notification message to the parent window. Typically, the parent window sorts the list view control by the specified column when a user taps the column header.
List view controls can set the order in which columns are displayed. To implement this feature, specify the LVCF_ORDER value and assign the proper value to the iOrder member in the LVCOLUMN structure.
The following code example shows how to add columns and set the number of items in the list view window.
BOOL InitListView (HWND hwndListView)
{
int index;
LV_COLUMN lvColumn;
TCHAR szString[5][20] = {TEXT("Main Column"),
TEXT("Column 1"),
TEXT("Column 2"),
TEXT("Column 3"),
TEXT("Column 4")};
// Empty the list in list view.
ListView_DeleteAllItems (hwndListView);
// Initialize the columns in the list view.
lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 120;
// Insert the five columns in the list view.
for (index = 0; index < 5; index++)
{
lvColumn.pszText = szString[index];
ListView_InsertColumn (hwndListView, index, &lvColumn);
}
// Set the number of items in the list to ITEM_COUNT.
ListView_SetItemCount (hwndListView, ITEM_COUNT);
return TRUE;
}