C++ win32 列表视图列

C++ win32 ListView columns

本文关键字:视图 列表 win32 C++      更新时间:2023-10-16

我的代码有问题。首先,此代码的本质是使用 win32 创建一个包含一些列的列表视图。

问题是当我尝试向我的 ListView 添加列并尝试显示它时,它不会向我显示列。这是我的代码,感谢您的任何帮助。

HWND function::CreateListView (HWND hwndParent) 
{
    INITCOMMONCONTROLSEX icex;           // Structure for control initialization.
    icex.dwICC = ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&icex);
    RECT rcClient;   
    // The parent window's client area.
    GetClientRect (hwndParent, &rcClient); 
    HWND hWndListView = CreateWindow(WC_LISTVIEW, "ViewList",WS_BORDER| WS_CHILD | LVS_REPORT | LVS_EDITLABELS,500,300,300,300,hwndParent,NULL,hInst,NULL); 
    return (hWndListView);
}
VOID function::SetView(HWND hWndListView, DWORD dwView) 
{ 
    // Retrieve the current window style. 
    DWORD dwStyle = GetWindowLong(hWndListView, GWL_STYLE); 
    // Set the window style only if the view bits changed.
    if ((dwStyle & LVS_TYPEMASK) != dwView) 
    {
        SetWindowLong(hWndListView,
                      GWL_STYLE,
                      (dwStyle & ~LVS_TYPEMASK) | dwView);
    }                 // Logical OR'ing of dwView with the result of 
}     
BOOL InitListViewColumns(HWND hWndListView) 
{ 
    char szText[256] ="test";     // 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|LVS_REPORT;
    // Add the columns.
    for (iCol = 0; iCol < C_COLUMNS; iCol++)
    {
        lvc.iSubItem = iCol;
        lvc.pszText = "LOL";
        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(hInst,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;
} 

在调用CreateWindow创建列表视图时,您忘记指定WS_VISIBLE样式。列表视图和列在那里,只是不可见。

您传递给LoadString的缓冲区永远不会使用,因为您从未设置lvc.pszText = szText因此所有列都命名为"LOL"。

编辑

:我的答案适用于使用注释中的修复程序编辑后的代码。LVS_REPORT仍然不是一个有效的 LVCF_* 标志,但由于它具有与 LVCF_FMT 相同的值,因此在此特定代码中没有害处,但仍应删除,因为代码在技术上不正确。