Home Index  | Back

New Page 1
Lesson 1 -Building Your First Windows Application

1.1 : Creating a Windows Application

1.2 : Creating a Dialog-Based Application

1.3 : Creating DLLs, Console Applications, and More

1.4 : Changing Your AppWizard Decisions
 1.5 : Understanding AppWizard’s Code
 1.6 : Understanding a MDI Application
 1.7 : Understanding the Components of a Dialog-Based Application

Lesson 2 - Dialogs and Controls

2.1 : Understanding Dialog Boxes

2.2 : Creating a Dialog Box Resource

 2.3 : Writing a Dialog Box Class
 2.4 : Using the Dialog Box Class
Lesson 3 - Messages and Commands

3.1 : Understanding Message Routing

3.2 : Understanding Message Loops

 3.3 : Reading Message Maps
 3.4 : Learning How ClassWizard Helps You Catch Messages
 3.5 : Recognizing Messages
 3.6 : Understanding Commands
 3.7 : Understanding Command Updates
 3.8 : Learning How ClassWizard Helps You Catch Commands and Command Updates

Lesson 4 - Documents and Views

4.1 : Understanding the Document Class

4.2 : Understanding the View Class

4.3 : Creating the Rectangles Application

 4.4 : Other View Classes

4.5 : Document Templates, Views, and Frame Windows

Lesson 5 - Drawing on the Screen

5.1 :Understanding Device Contexts

 5.2 : Introducing the Paint1 Application
 5.3 : Building the Paint1 Application
 5.4 : Scrolling Windows
 5.5 : Building the Scroll Application
Lesson 6 - Building a Complete Application: ShowString

6.1 : Building an Application That Displays a String

 6.2 : Building the ShowString Menus
 6.3 : Building the ShowString Dialog Boxes
 6.4 : Making the Menu Work
 6.5 : Making the Dialog Box Work
 6.6 : Adding Appearance Options to the Options Dialog Box
Lesson 7 -  Status Bars and Toolbars

7.1 : Working with Toolbars

 7.2 : Working with Status Bars
Lesson 8 - Common Controls

8.1 : The Progress Bar Control

 8.2 : The Up-Down Control
 8.3 : The Image List Control
 8.4 : The List View Control
 8.5 : The Tree View Control
 8.6 : The Rich Edit Control
 8.7 : The Date Picker Control
 8.8 : Month Calendar Control
 8.9 : Scrolling the View
Lesson 9 - Property Pages and Sheets

9.1 : Introducing Property Sheets

 9.2 : Creating the Property Sheet Demo Application
 9.3 : Running the Property Sheet Demo Application
Lesson 10 - ActiveX Concepts

10.1 : The Purpose of ActiveX

10.2 : Object Linking

10.3 : Object Embedding

 10.4 : Containers and Servers
 10.5 : Toward a More Intuitive User Interface
 10.6 : The Component Object Model  

10.7 : Automation

 10.8 : ActiveX Controls

Lesson 11 -  Building an ActiveX Control

11.1 : Creating a Rolling-Die Control

11.2 : Displaying the Current Value

11.3 : Reacting to a Mouse Click and Rolling the Die 

 11.4 : Creating a Better User Interface
 11.5 : Generating Property Sheets
Lesson 12 - Database Access

12.1 : Understanding Database Concepts

12.2 : Creating an ODBC Database Program

 12.3 : Choosing Between ODBC and DAO
 12.4 : OLE DB

Lesson -8:Common Controls

8.5- The Tree View Control

In the preceding section, you learned how to use the list view control to organize the display of many items in a window. The list view control enables you to display items both as objects in a window and objects in a report organized into columns. Often, however, the data you’d like to organize for your application’s user is best placed in a hierarchical view. That is, elements of the data are shown as they relate to one other. A good example of a hierarchical display is the directory tree used by Windows to display directories and the files that they contain.

MFC provides this functionality in the CTreeCtrl class. This versatile control displays data in various ways, all the while retaining the hierarchical relationship between the data objects in the view.

8.5.1 Creating the Tree View

Tree views are a little simpler than list views. You will write the rest of CreateTreeView(), which performs the following tasks:

1. Creates an image list

2. Creates the tree view itself

3. Associates the image list with the list view

4. Creates the root item

5. Creates child items

Creating the image list, creating the tree control, and associating the control with the image list are very similar to the steps completed for the image list. You’ve already written the code to create the image list, so add these lines to CreateTreeView():

// Create the Tree View control.

m_treeView.Create(WS_VISIBLE | WS_CHILD | WS_BORDER |

TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS |

TVS_EDITLABELS, CRect(20, 260, 160, 360), this,

IDC_TREEVIEW);

m_treeView.SetImageList(&m_treeImageList, TVSIL_NORMAL);

(Remember to add a resource ID for IDC_TREEVIEW.) The CTreeCtrl class, of which m_treeView is an object, defines special styles to be used with tree view controls. Table 8.3 lists these special styles.

Table 8.3 Tree View Control Styles

Style

Description

TVS_DISABLEDRAGDROP

 Disables drag-and-drop operations

TVS_EDITLABELS Enables the user to edit labels
TVS_HASBUTTONS Gives each parent item a button
TVS_HASLINES Adds lines between items in the tree
TVS_LINESATROOT Adds a line between the root and child items
TVS_SHOWSELALWAYS Forces a selected item to stay selected when losing focus
TVS_NOTOOLTIPS Suppresses ToolTips for the tree items
TVS_SINGLEEXPAND Expands or collapses tree items with a single click rather than a double click

8.5.2 Creating the Tree View’s Items

Creating items for a tree view control is much like creating items for a list view control. As with the list view, Visual C++ defines a structure that you must initialize and pass to the function that creates the items. This structure is called TVITEM and is defined in Listing 8.6.

Listing 8.6 The TVITEM Structure, Defined by MFC

typedef struct _TVITEM

{

UINT mask;

HTREEITEM hItem;

UINT state;

UINT stateMask;

LPSTR pszText;

int cchTextMax;

int iImage;

int iSelectedImage;

int cChildren;

LPARAM lParam;

} TVITEM;

In the TVITEM structure, the mask member specifies the other structure members that are valid. The flags you can use are as follows:

· TVIF_CHILDREN cChildren is valid.

· TVIF_HANDLE hItem is valid.

· TVIF_IMAGE iImage is valid.

· TVIF_PARAM lParam is valid.

· TVIF_SELECTEDIMAGE iSelectedImage is valid.

· TVIF_STATE state and stateMask are valid.

· TVIF_TEXT pszText and cchTextMax are valid.

The hItem member is the handle of the item, whereas the state and stateMask members hold the item’s current state and its valid states, which can be one or more of TVIS_BOLD, TVIS_CUT, TVIS_DROPHILITED, TVIS_EXPANDED, TVIS_EXPANDEDONCE, TVIS_FOCUSED, TVIS_OVERLAYMASK, TVIS_SELECTED, TVIS_STATEIMAGEMASK, and TVIS_USERMASK.

The pszText member is the address of a string buffer. When using the TVITEM structure to create an item, the string buffer contains the item’s text. When obtaining information about the item, pszText is the buffer where the information will be stored, and cchTextMax is the size of the buffer. If pszText is set to LPSTR_TEXTCALLBACK, the item uses the callback mechanism. Finally, the iImage member is the index of the item’s icon in the image list. If set to I_IMAGECALLBACK, the iImage member indicates that the item uses the callback mechanism.

The iSelectedImage member is the index of the icon in the image list that represents the item when the item is selected. As with iImage, if this member is set to I_IMAGECALLBACK, the iSelectedImage member indicates that the item uses the callback mechanism. Finally, cChildren specifies whether there are child items associated with the item.

In addition to the TVITEM structure, you must initialize a TVINSERTSTRUCT structure that holds information about how to insert the new structure into the tree view control. That structure is declared in Listing 8.7.

Listing 8.7 The TVINSERTSTRUCT Structure, Defined by MFC

typedef struct tagTVINSERTSTRUCT {

HTREEITEM hParent;

HTREEITEM hInsertAfter;

#if (_WIN32_IE >= 0x0400)

union

{

TVITEMEX itemex;

TVITEM item;

} DUMMYUNIONNAME;

#else

TVITEM item;

#endif

} TVINSERTSTRUCT, FAR *LPTVINSERTSTRUCT;

In this structure, hParent is the handle to the parent tree-view item. A value of NULL or TVI_ROOT specifies that the item should be placed at the root of the tree. The hInsertAfter member specifies the handle of the item after which this new item should be inserted. It can also be one of the flags TVI_FIRST (beginning of the list), TVI_LAST (end of the list), or TVI_SORT (alphabetical order). Finally, the item member is the TVITEM structure containing information about the item to be inserted into the tree.

Common first initializes the TVITEM structure for the root item (the first item in the tree). Add these lines:

// Create the root item.

TVITEM tvItem;

tvItem.mask =

TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;

tvItem.pszText = "Root";

tvItem.cchTextMax = 4;

tvItem.iImage = 0;

tvItem.iSelectedImage = 0;

TVINSERTSTRUCT tvInsert;

tvInsert.hParent = TVI_ROOT;

tvInsert.hInsertAfter = TVI_FIRST;

tvInsert.item = tvItem;

HTREEITEM hRoot = m_treeView.InsertItem(&tvInsert);

The CTreeCtrl member function InsertItem() inserts the item into the tree view control. Its single argument is the address of the TVINSERTSTRUCT structure.

CreateTreeView() then inserts the remaining items into the tree view control. Add these lines to insert some hard-coded sample items into the tree view:

// Create the first child item.

tvItem.pszText = "Child Item 1";

tvItem.cchTextMax = 12;

tvItem.iImage = 1;

tvItem.iSelectedImage = 1;

tvInsert.hParent = hRoot;

tvInsert.hInsertAfter = TVI_FIRST;

tvInsert.item = tvItem;

HTREEITEM hChildItem = m_treeView.InsertItem(&tvInsert);

// Create a child of the first child item.

tvItem.pszText = "Child Item 2";

tvItem.cchTextMax = 12;

tvItem.iImage = 2;

tvItem.iSelectedImage = 2;

tvInsert.hParent = hChildItem;

tvInsert.hInsertAfter = TVI_FIRST;

tvInsert.item = tvItem;

m_treeView.InsertItem(&tvInsert);

// Create another child of the root item.

tvItem.pszText = "Child Item 3";

tvItem.cchTextMax = 12;

tvItem.iImage = 1;

tvItem.iSelectedImage = 1;

tvInsert.hParent = hRoot;

tvInsert.hInsertAfter = TVI_LAST;

tvInsert.item = tvItem;

m_treeView.InsertItem(&tvInsert);

 

Next>>
 
© Dewsoft Overseas