Objective Toolkit : Chapter 14 Tree Control & Tree View : Tree Control Samples
Tree Control Samples
See the samples TreeDemo and DynaTree in the <stingray-installdir>Samples\Toolkit\MFC\TreeCtrl directory for a demonstration of these classes. See also the sample Samples\Toolkit\TreeCtrl\State. This sample does not ship with the product. For information on how to obtain this sample, see “Location of Sample Code” in the Getting Started part.
The DynaTree sample shows drag-and-drop using drag images and demonstrates using children on demand.
The State sample shows the use of state images and overlay images. Multi-column tree controls can store the subitem text internally if the StoreSubItemText( TRUE ) function is called after creation so that you can call SetItemText() or SetItemString() on subItems without using the LVN_GETDISPINFO callback. This feature is demonstrated in this sample. This sample also shows multi-column editing.
The following is a possible creation scenario for a multi-column tree that supports multiple selection, full row select, label editing, auto column sizing, and tooltips.
 
// standard tree control and window styles go here
DWORD dwStyles = TVS_SHOWSELALWAYS|TVS_HASBUTTONS |
TVS_LINESATROOT|TVS_HASLINES |
TVS_EDITLABELS|TVS_SHOWSELALWAYS |
TVS_DISABLEDRAGDROP|WS_CHILD;
 
// Stingray extended styles go here
DWORD dwStylesEx = TVXS_MULTISEL |
TVXS_FLYBYTOOLTIPS |
LVXS_HILIGHTSUBITEMS;
 
m_secTree.Create( dwStyles, dwStylesEx,
rect, this, IDC_SECTREE);
 
/* make the columns resize if the window width
changes. This is an alternative to having a
horizontal scroll bar. */
 
m_secTree.ModifyListCtrlStyleEx( 0,
LVXS_FITCOLUMNSONSIZE );
 
/* you can set the image and text
foreground/background colors for selected and
normal states */
COLORREF clrBack = RGB( 192, 220, 192);
 
// change the background color
m_secTree.SetBkColor( clrBack );
// change the selected icon background color
m_secTree.SetSelIconBkColor( clrBack );
// change the normal icon background color
m_secTree.SetIconBkColor( clrBack );
// change the normal text color
m_secTree.SetTextColor( RGB( 10, 10, 10 ) );
// change the selection text background color
m_secTree.SetSelTextBkColor( ::GetSysColor(COLOR_INACTIVECAPTION) );
// change the selection text color
m_secTree.SetSelTextColor( RGB( 255, 255, 255 ) );
 
/*as an alternative, you can skip all the color
initialization and simply use the default system
colors. In that case, you will want system color
changes to be shown in the control. Do this by
calling EnableSysColorTracking( TRUE ); */
 
//turn on the header control
m_secTree.EnableHeaderCtrl( TRUE );
// set the header text for column 0
m_secTree.SetColumnHeading(0, _T("Object") );
// set the column width to 60% of the
// view client rect.
m_secTree.SetColumnWidth(0,
(int)(rect.Width() * .60));
// add another column. Every tree item will
// have a sub item.
m_secTree.InsertColumn( 1, _T("Time"), LVCFMT_LEFT,
(int)(rect.Width() * .40) );
 
/ * I don't want to use LVN_GETDISPINFO to populate
my subitem text, so I must turn on subitem text
storage! This option is a much requested change that
allows setting subitem text directly using
SetItemText( item, subItem, _T(“Text”) ) */
m_secTree.StoreSubItemText( TRUE );