List-View コントロールを作成する方法

このトピックでは、リスト ビュー コントロールを作成する方法を説明します。 リスト ビュー コントロールを作成するには、CreateWindow 関数または CreateWindowEx 関数を使用し、WC_LISTVIEW ウィンドウ クラスを指定します。

リスト ビュー コントロールは、ダイアログ ボックス テンプレートの一部として作成することもできます。 クラス名として WC_LISTVIEW を指定する必要があります。 ダイアログ ボックス テンプレートの一部としてリスト ビュー コントロールを使用するには、ダイアログ ボックスのインスタンスを作成する前に、InitCommonControls または InitCommonControlsEx を呼び出す必要があります。

知っておくべきこと

テクノロジ

前提条件

  • C/C++
  • Windows ユーザー インターフェイス プログラミング

手順

まず、InitCommonControlsEx 関数を呼び出してウィンドウ クラスを登録し、付随する INITCOMMONCONTROLSEX 構造体に ICC_LISTVIEW_CLASSES ビットを指定します。 これにより、共通コントロール DLL が読み込まれます。 次に、CreateWindow 関数または CreateWindowEx 関数を使用し、WC_LISTVIEW ウィンドウ クラスを指定します。

Note

既定では、リスト ビュー コントロールではアイコン タイトル フォントが使用されます。 ただし、WM_SETFONT メッセージを使用して、テキストに使用するフォントを指定することができます。 このメッセージは、項目を挿入する前に送信する必要があります。 コントロールは、WM_SETFONT メッセージによって指定されたフォントの寸法を使用して、間隔とレイアウトを決定します。 項目ごとにフォントをカスタマイズすることもできます。 詳細については、「カスタム描画」を参照してください。

 

次の C++ コード例では、レポート ビューにリスト ビュー コントロールを作成します。

// CreateListView: Creates a list-view control in report view.
// Returns the handle to the new control
// TO DO:  The calling procedure should determine whether the handle is NULL, in case 
// of an error in creation.
//
// HINST hInst: The global handle to the applicadtion instance.
// HWND  hWndParent: The handle to the control's parent window. 
//
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, 
                                     L"",
                                     WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
                                     0, 0,
                                     rcClient.right - rcClient.left,
                                     rcClient.bottom - rcClient.top,
                                     hwndParent,
                                     (HMENU)IDM_CODE_SAMPLES,
                                     g_hInst,
                                     NULL); 

    return (hWndListView);
}

一般に、リスト ビュー アプリケーションを使用すると、ユーザーがビューを別のビューに変更できるようになります。

次の C++ コード例では、リスト ビューのウィンドウ スタイルを変更し、その結果としてビューが変更されます。

// SetView: Sets a list-view's window style to change the view.
// hWndListView: A handle to the list-view control. 
// dwView:       A value specifying the new view style.
//
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.

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

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

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