List-View イメージ リストを追加する方法

このトピックでは、リスト ビュー コントロールにイメージ リストを追加する方法について説明します。

コントロールが使用するイメージ リストのみを作成します。 たとえば、アプリケーションでユーザーがアイコン ビューに切り替えることを許可していない場合、大きなアイコン リストを作成して割り当てる必要はありません。 大きい画像リストと小さい画像リストの両方を作成する場合は、同じ順序で同じ画像を含める必要があります。これは、両方の画像リストでリスト ビュー 項目のアイコンを識別するために 1 つの値が使用されるためです。

知っておくべきこと

テクノロジ

前提条件

  • 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; 
}

List-View コントロール リファレンス

リスト ビュー コントロールについて

List-View コントロールの使用