![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
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); ⇐ . . . Weve added our three items, yet we can do more here, such as associate a bitmap with each item. Adding Bitmaps to Tree ViewsTo add bitmaps, we need a bitmap resource, so create one now with the Insert menus 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.
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 weve 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)); ⇐ . . . }
Finally, we create the image list with its Create() function and add it to the tree control with that controls 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); ⇐ } Weve 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 ItemWe 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 its 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 weve 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;
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 youve clicked the first subitem. Now were working with tree views in Visual C++. The listing for this program, TreeViewView.h and TreeViewView.cpp, appears in Listing 3.2.
|
![]() |
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. |