본문 바로가기
VisualStudio.C++.C#/코딩팁,함수활용,단편

MFC. CTreeCtrl. Tree Control . 트리 컨트롤 사용법.

by i.got.it 2022. 4. 4.

 

 

 

개요.

 
VC++ 에서 Tree Control사용법 정리.
 
기본 활용법.
1. 도구상자에서 Tree Control 을  대화상자에 배치하고 Tree Control 의 속성창에서 ID를 적절한것으로 설정.
2. 클래스 위저드 실행하여 멤버 변수추가. 예: m_TreeNXTask
3. Tree Control 속성의 Has Button과 Has Lines 속성을 모두 True 로 하기.
4. 트리에 아이템 추가하기 위한 트리컨트롤 멤버 함수 InsertItem (, , ,) 호출한다.
 
   

 

 
 

상세. - 기본 활용법.

  앞의 순서대로 각 단계별 이미지와 함깨 설명한다.


1. 도구상자에서 Tree Control 을  대화상자에 배치하고 Tree Control 의 속성창에서 ID를 적절한것으로 설정.


2. 클래스 위저드 실행하여 상기1의 Tree Control 의 멤버변수 추가.  아래예와 같이 m_TreeNXTask 로 했다.




3. Tree Control 속성의 Has Button과 Has Lines 속성을 모두 True 로 하기.
- 이 설정을 해야 아래 트리에서 보이듯이 +,- ,라인이 보이게 된다. 루트 아이템에도 +,-,라인 이 보이게 하려면 Lines At Root 를 true 로 설정한다.



4.  트리에 아이템 추가하기 위한 트리컨트롤 멤버 함수 InsertItem (, , ,) 호출한다.
InsertItem() 함수는 인자타입이 다른 4종의 함수들이 제공된다.  


더보기
CTreeCtrl::InsertItem


Call this function to insert a new item in a tree view control.
 
복사
HTREEITEM InsertItem( LPTVINSERTSTRUCT lpInsertStruct );




HTREEITEM InsertItem( UINT nMask, LPCTSTR lpszItem, int nImage, int nSelectedImage, UINT nState, UINT nStateMask, LPARAM lParam, HTREEITEM hParent, HTREEITEM hInsertAfter );




HTREEITEM InsertItem( LPCTSTR lpszItem, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST );


HTREEITEM InsertItem( LPCTSTR lpszItem, int nImage, int nSelectedImage, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST );
lpInsertStruct
A pointer to a TVINSERTSTRUCT that specifies the attributes of the tree view item to be inserted.
nMask
Integer specifying which attributes to set. See the TVITEM structure in the Windows SDK.
lpszItem
Address of a string containing the item's text.
nImage
Index of the item's image in the tree view control's image list.
nSelectedImage
Index of the item's selected image in the tree view control's image list.
nState
Specifies values for the item's states. See Tree View Control Item States in the Windows SDK for a list of appropriate states.
nStateMask
Specifies which states are to be set. See the TVITEM structure in the Windows SDK.
lParam
A 32-bit application-specific value associated with the item.
hParent
Handle of the inserted item's parent.
hInsertAfter
Handle of the item after which the new item is to be inserted.
Handle of the new item if successful; otherwise NULL.
The example shows situations in which you might want to use each version of the function when inserting a tree control item.
C++
복사
// Insert a root item using the structure. We must // initialize a TVINSERTSTRUCT structure and pass its // address to the call. TVINSERTSTRUCT tvInsert; tvInsert.hParent = NULL; tvInsert.hInsertAfter = NULL; tvInsert.item.mask = TVIF_TEXT; tvInsert.item.pszText = _T("United States"); HTREEITEM hCountry = m_TreeCtrl.InsertItem(&tvInsert); // Insert subitems of that root. Pennsylvania is // a state in the United States, so its item will be a child // of the United States item. We won't set any image or states, // so we supply only the TVIF_TEXT mask flag. This // override provides nearly complete control over the // insertion operation without the tedium of initializing // a structure. If you're going to add lots of items // to a tree, you might prefer the structure override // as it affords you a performance win by allowing you // to initialize some fields of the structure only once, // outside of your insertion loop. HTREEITEM hPA = m_TreeCtrl.InsertItem(TVIF_TEXT, _T("Pennsylvania"), 0, 0, 0, 0, 0, hCountry, NULL); // Insert the "Washington" item and assure that it is // inserted after the "Pennsylvania" item. This override is // more appropriate for conveniently inserting items with // images. HTREEITEM hWA = m_TreeCtrl.InsertItem(_T("Washington"), 0, 0, hCountry, hPA); // We'll add some cities under each of the states. // The override used here is most appropriate // for inserting text-only items. m_TreeCtrl.InsertItem(_T("Pittsburgh"), hPA, TVI_SORT); m_TreeCtrl.InsertItem(_T("Harrisburg"), hPA, TVI_SORT); m_TreeCtrl.InsertItem(_T("Altoona"), hPA, TVI_SORT); m_TreeCtrl.InsertItem(_T("Seattle"), hWA, TVI_SORT); m_TreeCtrl.InsertItem(_T("Kalaloch"), hWA, TVI_SORT); m_TreeCtrl.InsertItem(_T("Yakima"), hWA, TVI_SORT);
 



아래 함수 원형인 것을 이용하면 아이템에 간단히 문자열 표현하는 경우 간편하다.


HTREEITEM InsertItem(
   LPCTSTR lpszItem,
   HTREEITEM hParent = TVI_ROOT,
   HTREEITEM hInsertAfter = TVI_LAST
);



코드예.
아래 코드처럼 작성하면 오른쪽 그림 처럼 트리컨트롤에 보이게 된다.








1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void CCyNXTask1_UITDlg::OnBnClickedViewtreeNxtask()
{
    CString cst_level0;
    CString cst_entryord, cst_exitorder;
 
    HTREEITEM h_root;
    HTREEITEM h_entry, h_exit;
    for (int i = 0; i < 5; i++)
    {
        cst_level0.Format(_T("NXtask=%d"), i);
        h_root = m_TreeNXTask.InsertItem(cst_level0, NULL, NULL);
 
        h_entry = m_TreeNXTask.InsertItem(_T("Entry"), h_root, NULL);
        for (int j = 0; j < 2; j++)
        {
            cst_entryord.Format(_T("Order%d"),j);
            m_TreeNXTask.InsertItem(cst_entryord, h_entry, NULL);
        }
 
        h_exit = m_TreeNXTask.InsertItem(_T("Exit"), h_root, NULL);
        for (int j = 0; j < 4; j++)
        {
            cst_entryord.Format(_T("Order%d"), j);
            m_TreeNXTask.InsertItem(cst_entryord, h_exit, NULL);
        }
    }
}
 







   
 

 

 

 

 

 

 
 

상세 - 고급활용법. 트리 아이템에 아이콘 표현하기. 

   앞의 기본 활용법에서는 Tree Control 에 아이템 추가하는 함수 InsertItem 중에서 가장 간단한 인자 의 것을 사용했다.
이 함수로는 트리 컨트롤에 아이콘 표현이 안된다. 


이미지 추가 가능하고, 기타 완전한 제어가 가능한 인자 " LPTVINSERTSTRUCT lpInsertStruct" 인 것을 이용한다.


함수원형 : HTREEITEM InsertItem( LPTVINSERTSTRUCT lpInsertStruct );


더보기


TVINSERTSTRUCT structure

 
Contains information used to add a new item to a tree-view control. This structure is used with the TVM_INSERTITEM message. The structure is identical to the TV_INSERTSTRUCT structure, but it has been renamed to follow current naming conventions.

Syntax

C++
Copy
typedef struct { HTREEITEM hParent; HTREEITEM hInsertAfter; #if (_WIN32_IE >= 0x0400) union { TVITEMEX itemex; TVITEM   item; } DUMMYUNIONNAME; #else TVITEM    item; #endif } TVINSERTSTRUCT, *LPTVINSERTSTRUCT;

Members

hParent
Type: HTREEITEM
Handle to the parent item. If this member is the TVI_ROOT value or NULL, the item is inserted at the root of the tree-view control.
hInsertAfter
Type: HTREEITEM
Handle to the item after which the new item is to be inserted, or one of the following values:
Value Meaning
TVI_FIRST
Inserts the item at the beginning of the list.
TVI_LAST
Inserts the item at the end of the list.
TVI_ROOT
Add the item as a root item.
TVI_SORT
Inserts the item into the list in alphabetical order.

DUMMYUNIONNAME
itemex
Type: TVITEMEX
Version 4.71. TVITEMEX structure that contains information about the item to add.
item
Type: TVITEM
TVITEM structure that contains information about the item to add.
item
Type: TVITEM
TVITEM structure that contains information about the item to add.

Remarks

The unions in this structure have been updated to work with compilers that do not support nameless unions. If your compiler does not support nameless unions, define the NONAMELESSUNION token before including the commctrl.h header file.
Important  Using TVI_LAST to insert an item into a tree-view node that already contains a large number of items can take a long time, causing the application to stop responding until the insert operation completes.
 

Requirements

Minimum supported client Windows Vista [desktop apps only]
Minimum supported server Windows Server 2003 [desktop apps only]
Header
Commctrl.h
Unicode and ANSI names TVINSERTSTRUCTW (Unicode) and TVINSERTSTRUCTA (ANSI)


 

트리컨트롤 노드에 이미지 표현위한 설정.




1
2
3
4
5
6
7
 
  // CImageList m_ImageListTree ; 헤더에 정의.
    m_ImageListTree.Create(IDB_TREE,16,1,ILD_NORMAL);
 
  // 트리컨트롤에 이미지 리스트 추가. 
    m_Tree.SetImageList(&m_ImageListTree, TVSIL_NORMAL);
 
상기 설정은 1회만 설정되면 되고, 대화상자인 경우 OnInitDialog 에서 실행되면 된다.
Create 함수 내의 IDB_TREE 는 리소스에 추가된 비트맵 아이디이며, 비트맵으로 CImageList 생성하는 것.
프로젝트 리소스에 비트맵 추가방법 보기 -> http://igotit.tistory.com/751


더보기
CImageList::Create 

Initializes an image list and attaches it to a CImageList Class object.
 
복사
BOOL Create( int cx, int cy, UINT nFlags, int nInitial, int nGrow ); BOOL Create( UINT nBitmapID, int cx, int nGrow, COLORREF crMask ); BOOL Create( LPCTSTR lpszBitmapID, int cx, int nGrow, COLORREF crMask ); BOOL Create( CImageList& imagelist1, int nImage1, CImageList& imagelist2, int nImage2, int dx, int dy ); BOOL Create( CImageList* pImageList );
cx
Dimensions of each image, in pixels.
cy
Dimensions of each image, in pixels.
nFlags
Specifies the type of image list to create. This parameter can be a combination of the following values, but it can include only one of the ILC_COLOR values.
 
Value Meaning
ILC_COLOR Use the default behavior if none of the other ILC_COLOR* flags is specified. Typically, the default is ILC_COLOR4; but for older display drivers, the default is ILC_COLORDDB.
ILC_COLOR4 Use a 4-bit (16 color) device-independent bitmap (DIB) section as the bitmap for the image list.
ILC_COLOR8 Use an 8-bit DIB section. The colors used for the color table are the same colors as the halftone palette.
ILC_COLOR16 Use a 16-bit (32/64k color) DIB section.
ILC_COLOR24 Use a 24-bit DIB section.
ILC_COLOR32 Use a 32-bit DIB section.
ILC_COLORDDB Use a device-dependent bitmap.
ILC_MASK Uses a mask. The image list contains two bitmaps, one of which is a monochrome bitmap used as a mask. If this value is not included, the image list contains only one bitmap. See Drawing Images from an Image List for additional information on masked images.
nInitial
Number of images that the image list initially contains.
nGrow
Number of images by which the image list can grow when the system needs to resize the list to make room for new images. This parameter represents the number of new images the resized image list can contain.
nBitmapID
Resource IDs of the bitmap to be associated with the image list.
crMask
Color used to generate a mask. Each pixel of this color in the specified bitmap is changed to black, and the corresponding bit in the mask is set to one.
lpszBitmapID
A string containing the resource IDs of the images.
imagelist1
A reference to a CImageList object.
nImage1
Index of the first existing image.
imagelist2
A reference to a CImageList object.
nImage2
Index of the second existing image.
dx
Offset of the x-axis of the second image in relationship to the first image, in pixels.
dy
Offset of the y-axis of the second image in relationship to the first image, in pixels.
pImageList
A pointer to a CImageList object.
Nonzero if successful; otherwise 0.
You construct a CImageList in two steps. First, call the constructor and then call Create, which creates the image list and attaches it to the CImageListobject.
C++
복사
m_myImageList.Create(32, 32, ILC_COLOR8, 0, 4);
Header: afxcmn.h



트리컨트롤에 노드 추가하기 예.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
    TVINSERTSTRUCT tvInsert; 
 
    //TVINSERTSTRUCT 설정들 먼저 하고, 
    tvInsert.hParent = NULL; 
    tvInsert.hInsertAfter = TVI_LAST;
    tvInsert.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT; 
    tvInsert.item.state = 0;
    tvInsert.item.stateMask = 0;
    tvInsert.item.cchTextMax = 60; 
 
    tvInsert.item.cChildren = 0; 
 
    /// 루트에 추가할 노드 특성 설정. 
    tvInsert.item.pszText = _T("Computer");        // 노드 텍스트.
    tvInsert.item.iImage = 0;                    // 노드 기본 이미지 지정. 
    tvInsert.item.iSelectedImage = 1;         // 노드 선택시 이미지 지정.
 
    HTREEITEM hRoot = m_Tree.InsertItem(&tvInsert); //레벨1 루트에 추가. 
 
    /// 루트 밑에 노드 추가.
    tvInsert.hParent = hRoot; 
    tvInsert.item.pszText = _T("노드텍스트1");
    tvInsert.item.iImage = 1;                    // 노드 접힘시 이미지 지정.
    tvInsert.item.iSelectedImage = 1;                  // 노드 펼침시 이미지 지정.
 
    m_Tree.InsertItem(&tvInsert); //루트 하위에 추가. 
 
    tvInsert.item.pszText = _T("노드텍스트2");
    m_Tree.InsertItem(&tvInsert); //루트 하위에 추가. 
 
    m_Tree.Expand(hRoot, TVE_EXPAND);// 루트 펼친상태로 
 
 


위 코드 실행모습.

   

 

 

 

트리 아이템 마우스 클릭 처리

트리 컨트롤의 각 아이템을 마우스 클릭(좌, 우, 더블등) 한 경우 해당 아이템 받는 방법.


1. 트리컨트롤의 마우스 이벤트 처리기코드 추가하고, 
2. 마우스이베트 처리코드내에서 마우스 좌표를 트리 좌표로 변환하고, 함수 ScreenToClient
3. 트리컨트롤 멤버함수인 HitTest 로 마우스로 선택한 아이템을 받는다.


void CMyDlg::OnNMDblclkTree(NMHDR *pNMHDR, LRESULT *pResult)
{
    CPoint p; 
    GetCursorPos(&p); 
    UINT flag; 
    m_Tree.ScreenToClient(&p);// 마우스좌표를 트리좌표로 변환하고, 
    HTREEITEM hItem_dc = m_Tree.HitTest(p,&flag); // HitTest 함수로 트리아이템 받아. 

    // 시험용 문자 출력.
    AfxMessageBox(m_Tree.GetItemText(hItem_dc));


    // TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
    *pResult = 0;
}

 

 

 

컨텍스트 메뉴. 

통상 트리컨트롤에서 우마우스 클리시 컨텍스트 메뉴 보이는 경우 많다. 

컨텍스트 메뉴 만드는방법. 

 

MFC. menu, context menu

MFC 컨텍스트 메뉴 만들기 1. 리소스에서 메뉴 추가. 예: IDR_MENU_3DCHART 2. 메뉴보이게 하기. - 아래 함수 호출되면 마우스커서가 메뉴의 좌 상단 꼭지점에 배치된 상태로 보인다. int CCyChartView::Show_M

igotit.tistory.com

 
 

 

 

드래그 드롭 구현시 텍스트 안보이는 문제 해결. 

 
 
 
 

아이템 정렬 하기. 

CTreeCtrl::SortChildren

- 함수 인자로 전달된 아이템 하위의 아이템들을 알파벳 정렬함. 
 
 

CTreeCtrl::SortChildrenCB

- 별도의 외부 정렬함수 포인터를 인자로 전달하여 아이템 정렬하는것. 
 
 
 
 
트리 순회
 

// 인자로 전달된 HTREEITEM 이하 모든 아이템 순회.-재귀호출. 

int count_item=0; 

int TreeSearch_Recursive(HTREEITEM h_item)
{ 
    if (!ItemHasChildren(h_item)) return 0; // 하위 없다. 무처리 리턴. 

    h_item = GetNextItem(h_item, TVGN_CHILD); //TVGN_CHILD : h_item 의 첫번째 child item 

    while (h_item) { // 모든 sibling item 마다 반복.
          
        count_item++; // 아이템 수량 카운팅.
        
        TreeSearch_Recursive(h_item); //재귀호출. 

        h_item = GetNextItem(h_item,TVGN_NEXT);//TVGN_NEXT : sibling item.        
        
    }
    
    return count_item; 
}
 
 
 

MS 제공 정보.

 
 
 

 

상위정리

 

 

 

 

 

 


첫 등록 : 2016.01.08

최종 수정 : 2022.04.04

단축 주소 : https://igotit.tistory.com/538

 


 

 

 

댓글



 

비트코인




암호화폐       외환/나스닥/골드       암호화폐/외환/나스닥/골드 암호화폐/외환/나스닥/골드   암호화폐/외환/나스닥/골드
     
현물 |선물 인버스 |선물 USDT       전략매니저(카피트레이딩)     롤오버 이자 없는 스왑프리계좌
( 스왑프리 암호화폐도 거래 가능 )    
MT4, MT5 , cTrader 모두 지원     FTMO 계좌 매매운용. MT4,MT5