The following sections describe how to use the tree control and tree view classes.
In the Visual Studio resource editor, create a tree control resource on the dialog resource.
Instantiate an SECTreeCtrl object as a member of your dialog class.
In the OnInitDialog() method of your dialog class, call SubclassTreeCtrlId() on the SECTreeCtrl instance. For example:
m_pTreeCtrl->SubclassTreeCtrlId( IDC_TREE, this ); |
Create a unique control ID for the tree control. In Visual Studio, you can create an ID with the Resource Includes dialog.
Instantiate an SECTreeCtrl object.
Call the Create() method and specify the desired styles, rectangle, and parent. For example:
DWORD dwStyle = TVS_SHOWSELALWAYS | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_HASLINES | TVS_EDITLABELS | TVS_SHOWSELALWAYS | TVS_DISABLEDRAGDROP | WS_CHILD | WS_VISIBLE; DWORD dwStyleEx = TVXS_MULTISEL | TVXS_FLYBYTOOLTIPS | LVXS_HILIGHTSUBITEMS; m_pTreeCtrl->Create( dwStyle, dwStyleEx, rect, this, IDC_TREE); |
If you are creating a tree control inside a control bar or other resizable window, remember to override the OnSize() method to resize the tree control with the parent window.
void CMyControlBar::OnSize(UINT nType, int cx, int cy) { SECControlBar::OnSize(nType, cx, cy); if ( ::IsWindow(m_tree) ) m_tree.SetWindowPos( NULL, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER ); } |
Instantiate and initialize a TV_ITEM structure for the tree item to be added. For example the following initializes a TV_ITEM structure to be inserted at the root level of the tree.
TV_ITEM tvi; memset(&tvi,0,sizeof(tvi)); tvi.mask = TVIF_IMAGE|TVIF_TEXT|TVIF_SELECTEDIMAGE; tvi.pszText = LPSTR_TEXTCALLBACK; tvi.iImage = I_IMAGECALLBACK; tvi.iSelectedImage = I_IMAGECALLBACK; |
Call the InsertItem() method using a TV_INSERTSTRUCT structure as a parameter.
TV_INSERTSTRUCT tvis; memmove(&(tvis.item), &tvi, sizeof(TV_ITEM)); tvis.hParent = TVI_ROOT; tvis.hInsertAfter = TVI_LAST; HTREEITEM htiItem1 = m_pTreeCtrl-> InsertItem(&tvis ); |
Alternatively, call one of the overloaded InsertItem() methods.
HTREEITEM htiitem2 = m_pTreeCtrl->InsertItem(LPSTR_TEXTCALLBACK, I_IMAGECALLBACK, I_IMAGECALLBACK, TVI_ROOT, TVI_LAST ); |
Call the InsertColumn() method.
m_pTreeCtrl->InsertColumn( 1 /*0-based column index*/, "Attendance", LVCFMT_CENTER, 40 /*width*/ ); m_pTreeCtrl->InsertColumn( 2 /*0-based column index*/, "Grade", LVCFMT_CENTER, 40 /*width*/ ); |
If you want, you can mark all items as needing a remeasurement when a WM_PAINT occurs, and invalidate the control.
m_pTreeCtrlX->ReMeasureAllItems(); m_pTreeCtrlX->Invalidate(); |
In trees with multiple columns, the objects called subitems are managed by the items of the tree (in column 0). These subitems control what is displayed in the additional columns.
Method 1
Handle the LVN_GETDISPINFO notification.
In the handler for the LVN_GETDISPINFO notification, manage the subitem text storage yourself and then provide it to the control on demand via the callback.
Method 2
Call StoreSubItemText(TRUE) after creation to enable the tree control to manage subitem text storage for you.
Call the SetItemText() or SetItemString() methods to set the text directly.
Call the SetImageList() method.
SetImageList( CImageList*, TVSIL_NORMAL); |
Use of an image list is optional. If state images are also associated with a tree item, you can assign them different widths than the standard image, but not different heights.
Call the SetImageList with the TVSIL_STATE flag.
SetImageList( CImageList*, TVSIL_STATE ); |
A state image is an additional image drawn to the left of the normal image like a check box. State images are optional. If the standard images are also associated with a tree item, they can be different widths than the state image, but must be the same height.
Create a state image list containing each state image to be displayed. See Section 14.8.7.
Instantiate a TV_ITEM data structure. For example,
TV_ITEM tvi; |
Specify the TVIF_SELECTEDIMAGE flag in the mask data member. For example:
tvi.mask = TVIF_HANDLE | TVIF_TEXT | TVIF_SELECTEDIMAGE | TVIF_STATE; |
Specify the state image and TVIS_STATEIMAGEMASK to the state and stateMask data members, respectively.
tvi.state = INDEXTOSTATEIMAGEMASK(1); tvi.stateMask = TVIS_STATEIMAGEMASK; |
Specify any additional data members as indicated by any other flags included in the mask data member.
tvi.pszText = _T("Line Species 1"); tvi.iImage = m_idiFolderClosed; tvi.iSelectedImage = m_idiFolderClosed; tvi.lParam = (LPARAM)m_secTree.m_hWnd; |
Instantiate a TV_INSERTITEM structure and then initialize the item data member with a pointer to the TV_ITEM data structure.
TV_INSERTSTRUCT tvis; |
Fill in the remaining TV_INSERTITEM data members.
tvis.item = tvi; tvis.hParent = TVI_ROOT; tvis.hInsertAfter = TVI_LAST; |
Create the tree item with the InsertItem method.
HTREEITEM htreeitem = m_tree.InsertItem(&tvis); |
Create a state image list containing each state image to be displayed. See Section 14.8.7, "To create a state image list for tree items."
Instantiate a TV_ITEM structure and update the data members to prepare for a request for the state image of the item. For example:
TV_ITEM tvi; tvi.hItem = htiItemClicked; tvi.mask = TVIF_STATE | TVIF_HANDLE; tvi.stateMask = TVIS_STATEIMAGEMASK; |
Request the state image using the GetItem() method by passing the address of the TV_ITEM structure as a parameter.
m_secTree.GetItem(&tvi); UINT state = tvi.state & TVIS_STATEIMAGEMASK; |
Change the state image in the TV_ITEM structure to the image.
int index = (state == INDEXTOSTATEIMAGEMASK(1)) ? 2 : // checked 1); // not checked tvi.state = INDEXTOSTATEIMAGEMASK(index); |
Call SetImage() to have the tree control use the new image.
m_tree.SetItem(&tvi); |
Obtain a HTREEITEM handle for the tree item. For example:
HTREEITEM hti = m_tree.InsertItem(&tvi); |
Instantiate a TV_ITEM structure and update the members for the desired overlay image.
TV_ITEM tvi; tvi.hItem = hti; tvi.mask = TVIF_HANDLE | TVIF_STATE; tvi.stateMask = TVIS_OVERLAYMASK; tvi.state = INDEXTOOVERLAYMASK( iImage ); |
Call the SetItem() method using the TV_ITEM as a parameter.
m_secTree.SetItem( &tvi ); |
Call the GetSelectionArray() method.
pArrSelected = m_secTree.GetSelectionArray(); |
Override PickTextColor().
In the PickTextColor() override, assign the rgbText member of the LvPaintContext structure supplied as a parameter. For example, the following code is in the Objective Toolkit Build Wizard.
void CMyTreeView::PickTextColors(LvPaintContext* pPC) { ASSERT(pPC); SECTreeView::PickTextColors(pPC); if(pPC->lvi.iSubItem == 0) // subitem (column) number 0 { // Inside tree column, get a convenient context. TvPaintContext* pTvPC = (TvPaintContext*)pPC; // Check the properties of tree item (pTvPC->tvi), // and change color accordingly. For example, // to change the color of root nodes: HTREEITEM hParent=GetParentItem(pTvPC->tvi.hItem); if(hParent == NULL) { pTvPC->rgbText= m_rootColor; if ( GetFocus()== this && (pTvPC->lvi.state & LVIS_SELECTED) ) pTvPC->rgbTextBkgnd = RGB(255, 255, 0); // highlight } } } |
Override the PickTextFont() method.
In the PickTextFont() override, assign the pFont member of the LvPaintContext structure supplied as a parameter. For example, the following code is in the Objective Toolkit Build Wizard.
void CMyTreeView::PickTextFont(LvPaintContext* pPC) { ASSERT(pPC); SECTreeView::PickTextFont(pPC); if(pPC->lvi.iSubItem == 0) // subitem (column) number 0 { // Inside tree column, get a convenient context. TvPaintContext* pTvPC = (TvPaintContext*)pPC; // Check the properties of tree item (pTvPC->tvi), // and change font accordingly. For example, // to change the font of root nodes: HTREEITEM hParent=GetParentItem(pTvPC->tvi.hItem); if(hParent == NULL) pTvPC->pFont= &m_fontRoot; } } |
Call the following methods after making changes to the tree control/view.
ReMeasureAllItems(); Invalidate(); |
To update the GUI after every insert/delete, call EnableRedrawAfterInsert(TRUE). The dialog portion of the TREEDEMO sample demonstrates this. The Expand() function has an additional BOOL parameter to control redrawing of expanded/collapsed nodes.
In the header file, locate your instance of CTreeCtrl.
Change this from CTreeCtrl to SECTreeCtrl.
Population of the tree is the same as for CTreeCtrl.
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.