Düzenle

Aracılığıyla paylaş


How to Add List-View Columns

This topic demonstrates how to add columns to a list-view control. Columns are used to display the items and subitems when a list-view control is in report (details) view. Text from selected columns can also be displayed in tile view.

What you need to know

Technologies

Prerequisites

  • C/C++
  • Windows User Interface Programming

Instructions

To add a column to a list-view control, send the LVM_INSERTCOLUMN message or use the ListView_InsertColumn macro. To delete a column, use the LVM_DELETECOLUMN message.

The following C++ code example calls the ListView_InsertColumn macro to add columns to a list-view control. The column headings are defined in the application's header file as string resources, which are numbered consecutively starting with IDS_FIRSTCOLUMN. The number of columns is defined in the header file as C_COLUMNS.

// InitListViewColumns: Adds columns to a list-view control.
// hWndListView:        Handle to the list-view control. 
// Returns TRUE if successful, and FALSE otherwise. 
BOOL InitListViewColumns(HWND hWndListView) 
{ 
    WCHAR szText[256];     // Temporary buffer.
    LVCOLUMN lvc;
    int iCol;

    // Initialize the LVCOLUMN structure.
    // The mask specifies that the format, width, text,
    // and subitem members of the structure are valid.
    lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

    // Add the columns.
    for (iCol = 0; iCol < C_COLUMNS; iCol++)
    {
        lvc.iSubItem = iCol;
        lvc.pszText = szText;
        lvc.cx = 100;               // Width of column in pixels.

        if ( iCol < 2 )
            lvc.fmt = LVCFMT_LEFT;  // Left-aligned column.
        else
            lvc.fmt = LVCFMT_RIGHT; // Right-aligned column.

        // Load the names of the column headings from the string resources.
        LoadString(g_hInst,
                   IDS_FIRSTCOLUMN + iCol,
                   szText,
                   sizeof(szText)/sizeof(szText[0]));

        // Insert the columns into the list view.
        if (ListView_InsertColumn(hWndListView, iCol, &lvc) == -1)
            return FALSE;
    }
    
    return TRUE;
} 

List-View Control Reference

About List-View Controls

Using List-View Controls