次の方法で共有


Tree-View 項目を追加する方法

ツリー ビュー コントロールに項目を追加するには、 TVM_INSERTITEM メッセージをコントロールに送信します。 このメッセージには、 TVINSERTSTRUCT 構造体のアドレス、親項目、新しい項目の挿入後の項目、および項目の属性を定義する TVITEM 構造体が含まれます。 属性には、項目のラベル、選択されたイメージと選択されていないイメージ、および 32 ビットのアプリケーション定義値が含まれます。

知っておくべきこと

技術

[前提条件]

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

インストラクション

Tree-View に項目を追加する

このセクションの例では、アプリケーション定義配列で提供されるドキュメント見出し情報に基づいて目次を作成する方法を示します。 各配列要素は、見出し文字列と、見出しレベルを示す整数で構成されます。 この例では、3 つの見出しレベル (1、2、3) がサポートされています。

この例には、2 つの関数が含まれています。 最初の関数は、各見出しとそれに付随する見出しレベルを抽出し、2 番目の関数に渡します。

2 番目の関数は、ツリー ビュー コントロールに項目を追加します。 見出しテキストを項目のラベルとして使用し、見出しレベルを使用して新しいアイテムの親アイテムを決定します。 レベル 1 の見出しがツリー ビュー コントロールのルートに追加され、レベル 2 の見出しが前のレベル 1 の項目の子項目として追加されます。 この関数は、子項目があるかどうかに基づいて、項目にイメージを割り当てます。 アイテムに子項目がある場合は、閉じたフォルダーを表すイメージを取得します。 それ以外の場合は、ドキュメントを表すイメージを取得します。 項目は、選択した状態と選択されていない状態の両方に同じイメージを使用します。

// Adds items to a tree-view control. 
// Returns the handle to the newly added item. 
// hwndTV - handle to the tree-view control. 
// lpszItem - text of the item to add. 
// nLevel - level at which to add the item. 
//
// g_nClosed, and g_nDocument - global indexes of the images.

HTREEITEM AddItemToTree(HWND hwndTV, LPTSTR lpszItem, int nLevel)
{ 
    TVITEM tvi; 
    TVINSERTSTRUCT tvins; 
    static HTREEITEM hPrev = (HTREEITEM)TVI_FIRST; 
    static HTREEITEM hPrevRootItem = NULL; 
    static HTREEITEM hPrevLev2Item = NULL; 
    HTREEITEM hti; 

    tvi.mask = TVIF_TEXT | TVIF_IMAGE 
               | TVIF_SELECTEDIMAGE | TVIF_PARAM; 

    // Set the text of the item. 
    tvi.pszText = lpszItem; 
    tvi.cchTextMax = sizeof(tvi.pszText)/sizeof(tvi.pszText[0]); 

    // Assume the item is not a parent item, so give it a 
    // document image. 
    tvi.iImage = g_nDocument; 
    tvi.iSelectedImage = g_nDocument; 

    // Save the heading level in the item's application-defined 
    // data area. 
    tvi.lParam = (LPARAM)nLevel; 
    tvins.item = tvi; 
    tvins.hInsertAfter = hPrev; 

    // Set the parent item based on the specified level. 
    if (nLevel == 1) 
        tvins.hParent = TVI_ROOT; 
    else if (nLevel == 2) 
        tvins.hParent = hPrevRootItem; 
    else 
        tvins.hParent = hPrevLev2Item; 

    // Add the item to the tree-view control. 
    hPrev = (HTREEITEM)SendMessage(hwndTV, TVM_INSERTITEM, 
        0, (LPARAM)(LPTVINSERTSTRUCT)&tvins); 

    if (hPrev == NULL)
        return NULL;

    // Save the handle to the item. 
    if (nLevel == 1) 
        hPrevRootItem = hPrev; 
    else if (nLevel == 2) 
        hPrevLev2Item = hPrev; 

    // The new item is a child item. Give the parent item a 
    // closed folder bitmap to indicate it now has child items. 
    if (nLevel > 1)
    { 
        hti = TreeView_GetParent(hwndTV, hPrev); 
        tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE; 
        tvi.hItem = hti; 
        tvi.iImage = g_nClosed; 
        tvi.iSelectedImage = g_nClosed; 
        TreeView_SetItem(hwndTV, &tvi); 
    } 

    return hPrev; 
} 

// Extracts heading text and heading levels from a global 
// array and passes them to a function that adds them as
// parent and child items to a tree-view control. 
// Returns TRUE if successful, or FALSE otherwise. 
// hwndTV - handle to the tree-view control. 

BOOL InitTreeViewItems(HWND hwndTV)
{ 
    HTREEITEM hti;

    // g_rgDocHeadings is an application-defined global array of 
    // the following structures: 
    //     typedef struct 
    //       { 
    //         TCHAR tchHeading[MAX_HEADING_LEN]; 
    //         int tchLevel; 
    //     } Heading; 
    for (int i = 0; i < ARRAYSIZE(g_rgDocHeadings); i++) 
    { 
        // Add the item to the tree-view control. 
        hti = AddItemToTree(hwndTV, g_rgDocHeadings[i].tchHeading, 
            g_rgDocHeadings[i].tchLevel); 

        if (hti == NULL)
            return FALSE;
    } 
           
    return TRUE; 
}
  • Tree-View コントロールの使用