트리 뷰 항목을 추가하는 방법

트리 뷰 컨트롤에 항목을 추가하려면 컨트롤에 TVM_INSERTITEM 메시지를 보냅니다. 메시지에는 TVINSERTSTRUCT 구조체의 주소, 부모 항목 지정, 새 항목이 삽입된 후의 항목 및 항목의 특성을 정의하는 TVITEM 구조체가 포함됩니다. 특성에는 항목의 레이블, 선택한 이미지 및 선택되지 않은 이미지, 32비트 애플리케이션 정의 값이 포함됩니다.

알아야 하는 작업

기술

필수 구성 요소

  • C/C++
  • Windows 사용자 인터페이스 프로그래밍

지침

Tree-View 항목 추가

이 섹션의 예제에서는 애플리케이션 정의 배열에 제공된 문서 제목 정보를 기반으로 목차를 만드는 방법을 보여 줍니다. 각 배열 요소는 제목 문자열과 제목 수준을 나타내는 정수로 구성됩니다. 이 예제에서는 세 개의 제목 수준(1, 2 및 3)을 지원합니다.

이 예제에는 두 개의 함수가 포함되어 있습니다. 첫 번째 함수는 각 제목과 함께 제공되는 제목 수준을 추출한 다음 두 번째 함수에 전달합니다.

두 번째 함수는 트리 뷰 컨트롤에 항목을 추가합니다. 제목 텍스트를 항목의 레이블로 사용하고 제목 수준을 사용하여 새 항목의 부모 항목을 확인합니다. 수준 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; 
}

트리 뷰 컨트롤 사용

트리 뷰 컨트롤에서 사용자 지정 그리기를 보여 주는 CustDTv 샘플