2017-01-30 37 views
0

我的代码有问题。 首先,这段代码的实质是用一些列创建一个ListView,使用win32。C++ win32 ListView列

问题是,当我尝试添加列到我的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; 
} 
+0

您用于键入问题的编辑器有一个实时预览,以便您看到问题的样子。使用它来验证您使用的是一致的格式。 – IInspectable

+3

'LVS_REPORT'作为'LVCOLUMN.mask'的一部分不是有效标志,它是一个ListView *风格*。 –

+0

我删除了LVS_REPORT,但仍然不起作用 –

回答

0

你忘记打电话CreateWindow何时创建列表视图指定WS_VISIBLE风格。列表视图和列在那里,只是不可见。

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

编辑:我的答案适用于代码后,它已经从评论修复编辑。 LVS_REPORT仍然不是有效的LVCF_ *标志,但是因为它与LVCF_FMT具有相同的值,所以在此特定代码中不会造成任何伤害,但仍应该删除,因为代码在技术上不正确。

+0

WS_VISIBLE样式绝不是强制性的。常用的模式是创建窗口,然后在返回的句柄上调用'ShowWindow'。在对这个问题的评论中已经解决了真正的问题。 – IInspectable

+0

@IInspectable:如果在编辑之后按照原样执行代码,那么它看起来像listview被破坏,因为父对话框是空的,但只要添加可见标志,您将看到列表视图和列。 – Anders

+0

不,它没有。仍然有'LVS_REPORT'分配给'lvc.mask'。 – IInspectable