Share via


如何新增清單檢視影像清單

本主題示範如何將影像清單新增至清單檢視控件。

您只會建立控制項所使用的影像清單。 例如,如果您的應用程式不允許使用者切換至圖示檢視,則不需要建立並指派大型圖示清單。 如果您同時建立大型和小型影像清單,它們必須以相同順序包含相同的影像,因為單一值是用來識別這兩個影像清單中的清單檢視項目圖示。

您需要知道的事項

技術

必要條件

  • C/C++
  • Windows 使用者介面程序設計

指示

若要顯示專案影像,您必須將影像清單指派給清單檢視控件。 若要這樣做,請使用LVM_SETIMAGELIST訊息或對應的宏ListView_SetImageList,指定影像清單是否包含完整大小的圖示、小型圖示或狀態影像。 若要擷取目前指派給清單檢視控件之影像清單的句柄,請使用 LVM_GETIMAGELIST 訊息。 您可以使用 GetSystemMetrics 函式來判斷完整大小和小型圖示的適當維度。

在下列 C++ 程式代碼範例中,應用程式定義的函式會先建立影像清單,然後將它們指派給清單檢視控件。

// InitListViewImageLists: Creates image lists for a list-view control.
// This function only creates image lists. It does not insert the items into
// the control, which is necessary for the control to be visible.   
//
// Returns TRUE if successful, or FALSE otherwise. 
//
// hWndListView: Handle to the list-view control. 
// global variable g_hInst: The handle to the module of either a 
// dynamic-link library (DLL) or executable (.exe) that contains
// the image to be loaded. If loading a standard or system
// icon, set g_hInst to NULL.
//
BOOL InitListViewImageLists(HWND hWndListView) 
{ 
    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(g_hInst, MAKEINTRESOURCE(IDI_ITEM));

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

    DestroyIcon(hiconItem);
 
// When you are dealing with multiple icons, you can use the previous four lines of 
// code inside a loop. The following code shows such a loop. The 
// icons are defined in the application's header file as resources, which 
// are numbered consecutively starting with IDS_FIRSTICON. The number of 
// icons is defined in the header file as C_ICONS.
/*    
    for(index = 0; index < C_ICONS; index++)
    {
        hIconItem = LoadIcon (g_hinst, MAKEINTRESOURCE(IDS_FIRSTICON + index));
        ImageList_AddIcon(hSmall, hIconItem);
        ImageList_AddIcon(hLarge, hIconItem);
        Destroy(hIconItem);
    }
*/
    
    // Assign the image lists to the list-view control. 
    ListView_SetImageList(hWndListView, hLarge, LVSIL_NORMAL); 
    ListView_SetImageList(hWndListView, hSmall, LVSIL_SMALL); 
    
    return TRUE; 
}

清單檢視控件參考

關於清單檢視控件

使用清單檢視控件