在SysListView32中创建一个带有图标的项目

问题描述 投票:0回答:1

我正在尝试创建一个列表视图,一个带有图标的项目,但我创建了该项目,但没有显示图标。

我尝试传递

NULL
,资源索引,并使用
GetModuleHandle(NULL)
,但它只显示文本,而不显示图标。

HWND 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); 

    // Create the list-view window in report view with label editing enabled.
    HWND hWndListView = CreateWindow(WC_LISTVIEW, 
                                     "",
                                     WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
                                     0, 0,
                                     rcClient.right - rcClient.left,
                                     rcClient.bottom - rcClient.top,
                                     hwndParent,
                                     NULL,
                                     (HINSTANCE)GetWindowLong(hwndParent,GWL_HINSTANCE),
                                     NULL); 

    return (hWndListView);
}
VOID 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 
}                   // a bitwise AND between dwStyle and 
                    // the Unary complenent of LVS_TYPEMASK.
BOOL InitListViewColumns(HWND hWndListView, int C_COLUMNS, int FirstColumn) 
{ 
    LPSTR 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 = (LPSTR)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((HINSTANCE)GetWindowLong((HWND)GetWindowLong(hWndListView,GWL_HWNDPARENT),GWL_HINSTANCE),
                   FirstColumn + iCol,
                   (LPSTR)szText,
                   sizeof(szText)/sizeof(szText[0]));

        // Insert the columns into the list view.
        if (ListView_InsertColumn(hWndListView, iCol, &lvc) == -1)
            return FALSE;
    }
    
    return TRUE;
}
BOOL InsertListViewItems(HWND hWndListView, LPSTR text, LPVOID iImage)
{
    LVITEM lvI;

    // Initialize LVITEM members that are common to all items.
    lvI.pszText   = text; // Sends an LVN_GETDISPINFO message.
    lvI.cchTextMax= strlen(text);
    lvI.mask      = LVIF_TEXT | LVIF_IMAGE |LVIF_STATE;
    lvI.stateMask = 0;
    lvI.iSubItem  = 0;
    lvI.state     = 0;
    int index=ListView_GetItemCount(hWndListView)+1;
    lvI.iItem  = index;
    lvI.iImage = (int)iImage;
    
    // Insert items into the list.
    if (ListView_InsertItem(hWndListView, &lvI) == -1)
        return FALSE;

    return TRUE;
}
BOOL InitListViewImageLists(HWND hWndListView, LPVOID IDI_ITEM, HINSTANCE hInstance) 
{ 
    HICON hiconItem;     // Icon for list-view items.
    HIMAGELIST hLarge;   // Image list for icon view.
    HIMAGELIST hSmall;   // Image list for other views.

    // Create the full-sized icon image lists. 
    hLarge = ImageList_Create(GetSystemMetrics(SM_CXICON), 
                              GetSystemMetrics(SM_CYICON), 
                              ILC_MASK, 1, 1); 

    hSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON), 
                              GetSystemMetrics(SM_CYSMICON), 
                              ILC_MASK, 1, 1); 
    
    // Add an icon to each image list.  
    hiconItem = LoadIcon(hInstance,(LPCTSTR)IDI_ITEM);

    ImageList_AddIcon(hLarge, hiconItem);
    ImageList_AddIcon(hSmall, hiconItem);

    DestroyIcon(hiconItem);

    
    // Assign the image lists to the list-view control. 
    ListView_SetImageList(hWndListView, hLarge, LVSIL_NORMAL); 
    ListView_SetImageList(hWndListView, hSmall, LVSIL_SMALL); 
    
    return TRUE; 
}

    hWnd[1] = CreateListView((HWND)hWnd[0]);
    InitListViewImageLists((HWND)hWnd[1], NULL, NULL);
    SetView((HWND)hWnd[1], LVS_ICON);
    InitListViewColumns((HWND)hWnd[1], 1, IDS_FIRSTCOLUMN);
    InsertListViewItems((HWND)hWnd[1], "Windows", NULL);

对于构建系统:Microsoft Visual C/C++ 6.0

c++ winapi syslistview32
1个回答
0
投票

正如 Luke 和 Remy Lebeau 所说,这是正确的。我确实将 NULL 传递给了

InitListViewImageLists()
InsertListViewItems()

并且,

LPSTR
中的
InitListViewColumns()
LPVOID
中的
InsertListViewItems()
并不重要。

这是新代码:

HWND 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); 

    // Create the list-view window in report view with label editing enabled.
    HWND hWndListView = CreateWindow(WC_LISTVIEW, 
                                     "",
                                     WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
                                     0, 0,
                                     rcClient.right - rcClient.left,
                                     rcClient.bottom - rcClient.top,
                                     hwndParent,
                                     NULL,
                                     (HINSTANCE)GetWindowLong(hwndParent,GWL_HINSTANCE),
                                     NULL); 

    return (hWndListView);
}
VOID 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 
}                   // a bitwise AND between dwStyle and 
                    // the Unary complenent of LVS_TYPEMASK.
BOOL InitListViewColumns(HWND hWndListView, int C_COLUMNS, int FirstColumn) 
{ 
    LPSTR 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 = (LPSTR)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((HINSTANCE)GetWindowLong((HWND)GetWindowLong(hWndListView,GWL_HWNDPARENT),GWL_HINSTANCE),
                   FirstColumn + iCol,
                   (LPSTR)szText,
                   sizeof(szText)/sizeof(szText[0]));

        // Insert the columns into the list view.
        if (ListView_InsertColumn(hWndListView, iCol, &lvc) == -1)
            return FALSE;
    }
    
    return TRUE;
}
BOOL InsertListViewItems(HWND hWndListView, LPSTR text, LPVOID iImage)
{
    LVITEM lvI;

    // Initialize LVITEM members that are common to all items.
    lvI.pszText   = text; // Sends an LVN_GETDISPINFO message.
    lvI.cchTextMax= strlen(text);
    lvI.mask      = LVIF_TEXT | LVIF_IMAGE |LVIF_STATE;
    lvI.stateMask = 0;
    lvI.iSubItem  = 0;
    lvI.state     = 0;
    int index=ListView_GetItemCount(hWndListView)+1;
    lvI.iItem  = index;
    lvI.iImage = (int)iImage;
    
    // Insert items into the list.
    if (ListView_InsertItem(hWndListView, &lvI) == -1)
        return FALSE;

    return TRUE;
}
BOOL InitListViewImageLists(HWND hWndListView, int IDI_ITEM, HINSTANCE hInstance) 
{ 
    HICON hiconItem;     // Icon for list-view items.
    HIMAGELIST hLarge;   // Image list for icon view.
    HIMAGELIST hSmall;   // Image list for other views.

    // Create the full-sized icon image lists. 
    hLarge = ImageList_Create(GetSystemMetrics(SM_CXICON), 
                              GetSystemMetrics(SM_CYICON), 
                              ILC_MASK, 1, 1); 

    hSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON), 
                              GetSystemMetrics(SM_CYSMICON), 
                              ILC_MASK, 1, 1); 
    
    // Add an icon to each image list.  
    hiconItem = LoadIcon(hInstance,(LPCTSTR)IDI_ITEM);

    ImageList_AddIcon(hLarge, hiconItem);
    ImageList_AddIcon(hSmall, hiconItem);

    DestroyIcon(hiconItem);

    
    // Assign the image lists to the list-view control. 
    ListView_SetImageList(hWndListView, hLarge, LVSIL_NORMAL); 
    ListView_SetImageList(hWndListView, hSmall, LVSIL_SMALL); 
    
    return TRUE; 
}

    hWnd[1] = CreateListView((HWND)hWnd[0]);
    InitListViewImageLists((HWND)hWnd[1], IDI_DS, hInst);
    SetView((HWND)hWnd[1], LVS_ICON);
    InitListViewColumns((HWND)hWnd[1], 1, IDS_FIRSTCOLUMN);
    InsertListViewItems((HWND)hWnd[1], "Windows", 0);
© www.soinside.com 2019 - 2024. All rights reserved.