Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


The following example shows how we insert the first item, to which we give the caption “Click Me.” Note that this function returns a handle to an item of type HTREEITEM and that we save that item as the root item.

void CTreeViewView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();

    HTREEITEM root = GetTreeCtrl().InsertItem(CString(“Click Me”));     ⇐
        .
        .
        .

Now we can add three more items, making them subitems of the root item by passing that item to InsertItem():

void CTreeViewView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();

    HTREEITEM root = GetTreeCtrl().InsertItem(CString(“Click Me”));
    GetTreeCtrl().InsertItem(CString(“Hello”), root);                 ⇐
    GetTreeCtrl().InsertItem(CString(“from”), root);                   ⇐
    GetTreeCtrl().InsertItem(CString(“tree views!”), root);            ⇐
        .
        .
        .

We’ve added our three items, yet we can do more here, such as associate a bitmap with each item.

Adding Bitmaps to Tree Views

To add bitmaps, we need a bitmap resource, so create one now with the Insert menu’s Resource item by selecting Bitmap in the Insert Resource box. This creates a new bitmap, IDB_BITMAP1, which we edit to show a star, as displayed in Figure 3.8.


Figure 3.8  Creating a bitmap.

We can add the new bitmap to our tree control by first creating an image list.

void CTreeViewView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();

    HTREEITEM root = GetTreeCtrl().InsertItem(CString(“Click Me”));
    GetTreeCtrl().InsertItem(CString(“Hello”), root);
    GetTreeCtrl().InsertItem(CString(“from”), root);
    GetTreeCtrl().InsertItem(CString(“tree views!”), root);
    CImageList* pImageList = new CImageList();                   ⇐
    .
    .
    .

This new image list holds the bitmaps we use in the tree view. We use only IDB_BITMAP1 here, passing its ID, size, “growable” size (the number of pixels you can “grow” the bitmap, which we set to 0 here), and a mask with which to display the bitmap (we use a white mask, which means the bitmap appears as we’ve designed it).

void CTreeViewView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();

    HTREEITEM root = GetTreeCtrl().InsertItem(CString(“Click Me”));
    GetTreeCtrl().InsertItem(CString(“Hello”), root);
    GetTreeCtrl().InsertItem(CString(“from”), root);
    GetTreeCtrl().InsertItem(CString(“tree views!”), root);
    CImageList* pImageList = new CImageList();
    pImageList->Create(IDB_BITMAP1, 40, 0, RGB(255, 255, 255));         ⇐
        .
        .
        .
}


Using Images in Tree Controls

You can associate a different image with each item in a tree control and even use different images when the item is selected or deselected. In this short example, we use the same bitmap for all purposes.


Finally, we create the image list with its Create() function and add it to the tree control with that control’s SetImageList() function.

void CTreeViewView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();

    HTREEITEM root = GetTreeCtrl().InsertItem(CString(“Click Me”));
    GetTreeCtrl().InsertItem(CString(“Hello”), root);
    GetTreeCtrl().InsertItem(CString(“from”), root);
    GetTreeCtrl().InsertItem(CString(“tree views!”), root);
    CImageList* pImageList = new CImageList();
    pImageList->Create(IDB_BITMAP1, 40, 0, RGB(255, 255, 255));        ⇐
    GetTreeCtrl().SetImageList(pImageList, TVSIL_NORMAL);              ⇐
}

We’ve set up our tree view now, but what happens when the user double- clicks an item in the list, which usually means we should take some action like opening a file? How will we know?

Determining a Double-Clicked Item

We can make the first subitem in our tree view “clickable” by displaying a message box when the user double-clicks that item. To determine if that item was double-clicked, we first make sure we store that item when it is created by setting aside space for it in TreeViewView.h.

class CTreeViewView : public CTreeView
{
protected: // create from serialization only
    CTreeViewView();
    DECLARE_DYNCREATE(CTreeViewView)
    HTREEITEM item1;                        ⇐

When the item is created, we store it like this:

void CTreeViewView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();

    HTREEITEM root = GetTreeCtrl().InsertItem(CString(“Click Me”));
    item1 = GetTreeCtrl().InsertItem(CString(“Hello”), root);           ⇐
    GetTreeCtrl().InsertItem(CString(“from”), root);
    GetTreeCtrl().InsertItem(CString(“tree views!”), root);
    CImageList* pImageList = new CImageList();
    pImageList->Create(IDB_BITMAP1, 40, 0, RGB(255, 255, 255));
}

Use ClassWizard to connect the NM_DBLCLICK message to a new view function, OnDblClick(). Note that NM_DBLCLICK is a Windows notify message, and it’s passed on to us through a mechanism called message reflection.

void CTreeViewView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
    // TODO: Add your control notification handler code here

    *pResult = 0;
}

Setting *pResult to 0 here indicates that we’ve handled the message correctly. In this case, we want to display a message only if item1 was clicked.

void CTreeViewView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
    if(GetTreeCtrl().GetSelectedItem() == item1){                   ⇐
        MessageBox(“You clicked the first subitem.”);               ⇐
    };                                                              ⇐
    *pResult = 0;


Figure 3.9  Using a tree view.

Run the program, and double-click the root node (marked “Click Me”) to open the tree, as also shown in Figure 3.9. When you click the “Hello” item, the message box appears on the screen indicating that you’ve clicked the first subitem. Now we’re working with tree views in Visual C++.

The listing for this program, TreeViewView.h and TreeViewView.cpp, appears in Listing 3.2.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.