次の方法で共有


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

ツリー ビュー コントロールを作成するには、 CreateWindowEx 関数を使用して、ウィンドウ クラスの WC_TREEVIEW 値を指定します。 ツリー ビュー ウィンドウ クラスは、共通コントロール DLL が読み込まれると、アプリケーションのアドレス空間に登録されます。 DLL が読み込まれるようにするには、 InitCommonControls 関数を 使用します。

知っておくべきこと

技術

[前提条件]

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

インストラクション

Tree-View コントロールのインスタンスを作成する

次の例では、親ウィンドウのクライアント領域に合わせてサイズ設定されたツリー ビュー コントロールを作成します。 また、アプリケーション定義関数を使用して、イメージ リストをコントロールに関連付け、コントロールに項目を追加します。

// Create a tree-view control. 
// Returns the handle to the new control if successful,
// or NULL otherwise. 
// hwndParent - handle to the control's parent window. 
// lpszFileName - name of the file to parse for tree-view items.
// g_hInst - the global instance handle.
// ID_TREEVIEW - the resource ID of the control.

HWND CreateATreeView(HWND hwndParent)
{ 
    RECT rcClient;  // dimensions of client area 
    HWND hwndTV;    // handle to tree-view control 

    // Ensure that the common control DLL is loaded. 
    InitCommonControls(); 

    // Get the dimensions of the parent window's client area, and create 
    // the tree-view control. 
    GetClientRect(hwndParent, &rcClient); 
    hwndTV = CreateWindowEx(0,
                            WC_TREEVIEW,
                            TEXT("Tree View"),
                            WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES, 
                            0, 
                            0, 
                            rcClient.right, 
                            rcClient.bottom,
                            hwndParent, 
                            (HMENU)ID_TREEVIEW, 
                            g_hInst, 
                            NULL); 

    // Initialize the image list, and add items to the control. 
    // InitTreeViewImageLists and InitTreeViewItems are application- 
    // defined functions, shown later. 
    if (!InitTreeViewImageLists(hwndTV) || 
                !InitTreeViewItems(hwndTV))
    { 
        DestroyWindow(hwndTV); 
        return FALSE; 
    } 
    return hwndTV;
} 

注釈

ツリー ビュー コントロールを作成するときに、テキストに使用するフォントを設定する WM_SETFONT メッセージを送信することもできます。 アイテムを挿入する前に、このメッセージを送信する必要があります。 既定では、ツリー ビューではアイコンタイトルフォントが使用されます。 カスタム 描画を使用して項目ごとのフォントをカスタマイズできますが、ツリー ビュー コントロールは、 WM_SETFONT メッセージで指定されたフォントのサイズを使用して、間隔とレイアウトを決定します。

  • Tree-View コントロールの使用