如何添加列表视图列

本主题演示如何在列表视图控件中添加列。 当列表视图控件位于报表(详细信息)视图中时,列用于显示项和子项。 所选列中的文本也可以显示在磁贴视图中。

需要了解的事项

技术

先决条件

  • C/C++
  • Windows 用户界面编程

说明

要将列添加到列表视图控件,请发送 LVM_INSERTCOLUMN 消息或使用 ListView_InsertColumn 宏。 若要删除列,请使用 LVM_DELETECOLUMN 消息。

以下 C++ 代码示例调用 ListView_InsertColumn 宏,将列添加到列表视图控件。 列标题在应用程序的头文件中定义为字符串资源,从 IDS_FIRSTCOLUMN 开始连续编号。 列数在标头文件中定义为 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;
} 

列表视图控件参考

关于列表视图控件

使用列表视图控件