다음을 통해 공유


트리 뷰 컨트롤을 만드는 방법

트리 뷰 컨트롤을 만들려면 CreateWindowEx 함수를 사용하여 창 클래스에 대한 WC_TREEVIEW 값을 지정합니다. 공용 컨트롤 DLL이 로드될 때 트리 뷰 창 클래스는 애플리케이션의 주소 공간에 등록됩니다. DLL이 로드되었는지 확인하려면 InitCommonControls 함수를 사용합니다.

알아야 하는 작업

기술

필수 구성 요소

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

지침

트리 뷰 컨트롤의 인스턴스 만들기

다음 예에서는 부모 창의 클라이언트 영역에 맞게 크기가 조정된 트리 뷰 컨트롤을 만듭니다. 또한 애플리케이션 정의 함수를 사용하여 이미지 목록을 컨트롤과 연결하고 컨트롤에 항목을 추가합니다.

// 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 메시지에서 지정한 글꼴 크기를 사용하여 간격과 레이아웃을 결정합니다.

트리 뷰 컨트롤 사용

CustDTv 샘플은 트리 뷰 컨트롤의 사용자 지정 그리기를 보여 줍니다.