TREEVIEW ON AN SDI

To create a treeview on an sdi:
1. Create an application or a class based on CTreeView for this example, we have ExoTree.
2. To create the items in the TreeView, do one of the following:
a) In your OnInitialUpdate() function, make the following changes:
void CExoTreeView::OnInitialUpdate()
{
CTreeCtrl&  trCtrl = GetTreeCtrl();
HTREEITEM hItem;

hItem = trCtrl.InsertItem( "Cameroon", 0, 2 );

trCtrl.InsertItem( "Yaounde", 1, 3, hItem );
trCtrl.InsertItem( "Douala", 1, 3, hItem );
trCtrl.InsertItem( "Ebolowa", 1, 3, hItem );

hItem = trCtrl.InsertItem( "U.S.A.", 0, 2 );
trCtrl.InsertItem( "Washington, DC", 1, 3, hItem );
trCtrl.InsertItem( "New York1, 3, hItem );

htem = trCtrl.InsertItem( "Germany", 0, 2 );
trCtrl.InsertItem( "Bonn", 1, 3, hItem );
trCtrl.InsertItem( "Francfort", 1, 3, hItem );

CTreeView::OnInitialUpdate();

}

But it is rather recommended to create a protected
CreateLVData()
like this:
i/ //}}AFX_MSG DECLARE_MESSAGE_MAP()
protected:
void CreateLVData();
ii/ And implement that function like this:
void CExoTreeView::CreateLVData()
{
CTreeCtrl&  trCtrl = GetTreeCtrl();
HTREEITEM hItem;

hItem = trCtrl.InsertItem( "Cameroon", 0, 2 );
trCtrl.InsertItem( "Yaounde", 1, 3, hItem );
trCtrl.InsertItem( "Douala", 1, 3, hItem );
trCtrl.InsertItem( "Ebolowa", 1, 3, hItem );

hItem = trCtrl.InsertItem( "U.S.A.", 0, 2 );
trCtrl.InsertItem( "Washington, DC", 1, 3, hItem );
trCtrl.InsertItem( "New York", 1, 3, hItem );

hItem = trCtrl.InsertItem( "Germany", 0, 2 );
trCtrl.InsertItem( "Bonn", 1, 3, hItem );
trCtrl.InsertItem( "Francfort", 1, 3, hItem );

}
iii/ Then call it from your OnInitialUpdate() function, like this:
void CExoTreeView::OnInitialUpdate()
{
CreateLVData();
CTreeView::OnInitialUpdate();
}
3. a) To create the buttons and lines connecting the items on the list, specify your LVS (list view style)s in your PreCreateWindow() function (check the documentation to know the style options available) like this:
BOOL CExoTreeView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_EDITLABELS;
return CTreeView::PreCreateWindow(cs);
}
b) To implement the images on the tree list: Create the images. Let's consider four images created on a single bitmab of 80x16, identified as IDB_TREEIMAGES.

The first image will be the default image for any parent/root, unselected.

The second image is the default image for any child item unselected.

The third image is the highlighted parent item.
The fourth image is the highlighted child item.

c) In your view class, create a protected CImageList item like this:
DECLARE_MESSAGE_MAP()
protected:
CImageList m_TreeImages;
void CreateLVData();
d) Make the following changes in your OnInitialUpdate()function:
void CExoTreeView::OnInitialUpdate()
{
CTreeCtrl& trCtrl = GetTreeCtrl();

m_TreeImages.Create( IDB_TREEIMAGES, 16, 1, RGB(0, 255, 0) ); 
trCtrl.SetImageList( m_TreeImages, TVSIL_NORMAL ); CreateLVData();

CTreeView::OnInitialUpdate();

}

Webmaster