Advanced MFC Programming

Advanced MFC Programming Advanced MFC Programming

math.hcmuns.edu.vn
from math.hcmuns.edu.vn More from this publisher
11.04.2014 Views

Chapter 5. Common Controls dialog box. To implement tree control in a view, we can implement the view using class CTreeView. To implement tree control in a dialog box, we need to use CTreeCtrl class. In this section we will focus on dialog box implementation of tree control. Like other common controls, we can add tree control resources to the dialog template when designing application’s resource. The tree control will have an ID, which could be used to access the control (by either calling function CWnd::GetDlgItem(…) or adding CTreeCtrl type member variable). Image List We can associate a bitmap image with each node contained in the tree control. This will make the tree control more intuitive. For example, in a file manager application, we may want to use different images to represent different file types: folder, executable file, DLL file, etc. Before using the images to implement the tree control, we must first prepare them. For tree control (also list control and tab control), these images must be managed by Image List, which is supported by class CImageList in MFC. Class CImageList can keep and manage a collection of images with the same size. Each image in the list is assigned a zero-based index. After an image list is created successfully, it can be selected into the tree control. We can associate a node with any image contained in the image list. Here image drawing is handled automatically. If we provide mask bitmaps, only the unmasked portion of the images will be drawn for representing nodes. A mask bitmap must contain only black and white colors. Besides preparing mask bitmaps by ourselves, we can also generate mask bitmaps from the normal images. To use class CImageList, first we need to declare a CImageList type variable. If we create an image list dynamically by using “new” operator, we need to release the memory when it is no longer in use. Before adding images to the list, we need to call function CImageList::Create(…) to initialize it. This function has several versions, the following is one of them: BOOL CImageList::Create(int cx, int cy, UINT nFlags, int nInitial, int nGrow); Here cx and cy indicate the dimension of all images, nInitial represents the number of initial bitmaps that will be included in the image list, nGrow specifies the number of bitmaps that can be added later. Parameter nFlags indicates bitmap types, it could be ILC_COLOR, ILC_COLOR4, ILC_COLOR8, etc., which specify the bitmap format of the images. For example, ILC_COLOR indicates default bitmap format, ILC_COLOR4 indicates 4-bit DIB format (16-color), ILC_COLOR8 indicates 8-bit DIB format (256-color). We can combine ILC_MASK with any of these bitmap format flags to let the image be drawn with transparency. The images can be added by calling function CImageList::Add(…). Again, this function has three versions: int CImageList::Add(CBitmap *pbmImage, CBitmap *pbmMask); int CImageList::Add(CBitmap *pbmImage, COLORREF crMask); int CImageList::Add(HICON hIcon); The image list can be created from either bitmaps or icons. For the first version of this function, the second parameter is a pointer to the mask bitmap that will be used to implement transparent background drawing. The second version allows us to specify a background color that can be used to generate a mask bitmap from the normal image. Here parameter crMask will be used to create the mask bitmap: all pixels in the source bitmap that have the same color with crMask will be masked when the bitmap is being drawn, and their colors will be set to the current background color. We can choose a background color by calling function CImageList::SetBkColor(…). To use image list with a tree control, we need to call function CTreeCtr::SetImageList(…) to assign it to tree control. Then, when creating a node for the tree control, we can use the bitmap index to associate any node with this image. Adding Nodes At the beginning, the tree control does not contain any node. Like other common controls, we can initialize it in function CDialog::OnInitDialog(). To add a node to the tree, we need to call function CTreeCtrl::InsertItem(…). 118

Chapter 5. Common Controls This function also has several versions. The following is the one that has the simplest format: int CTreeCtrl::InsertItem(LPTV_INSERTSTRUCT lpInsertStruct); The only parameter to this function is a TV_INSERTSTRUCT type pointer: typedef struct _TV_INSERTSTRUCT{ HTREEITEM hParent; HTREEITEM hInsertAfter; TV_ITEM item; } TV_INSERTSTRUCT; In a tree control, nodes are managed through handles. After a node is created, it will be assigned an HTREEITEM type handle. Each node has a different handle, so we can use the handle to access a specific node. In the above structure, member hParent indicates which node is the parent of the new node. If we assign NULL to this member, the new node will become the root node. Likewise, member hInsertAfter is used to indicate where the new node should be inserted. We can specify a node handle, or we can use predefined parameters TVI_FIRST, TVI_LAST or TVI_SORT to insert the new node after the first node, last node or let the nodes be sorted automatically. Member item is a TV_ITEM type object, and the structure contains the information of the new node: typedef struct _TV_ITEM { UINT mask; HTREEITEM hItem; UINT state; UINT stateMask; LPSTR pszText; int cchTextMax; int iImage; int iSelectedImage; int cChildren; LPARAM lParam; } TV_ITEM; In order to add new nodes, we need to understand how to use the following four members of this structure: mask, pszText, iImage and iSelectedImage. Member mask indicates which of the other members in the structure contain valid data. Besides mask, every member of this structure has a corresponding mask flag listed as follows: Member Mask Flag hItem TVIF_HANDLE state, stateMask TVIF_STATE pszText, cchTextMax TVIF_TEXT iImage TVIF_IMAGE iImage, iSelectedImage TVIF_SELECTEDIMAGE cChildren TVIF_CHILDREN lParam TVIF_PARAM In order to use members pszText, iImage and iSelectedImage, we need to set the following bits of member mask: TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT Member pszText is a pointer to a null-terminated string text that will be used to label this node. Member iImage and iSelectedImage are indices to two images contained in the image list that will be used to represent the node’s normal and selected state respectively. By calling function CTreeCtrl::InsertItem(…) repeatedly, we could create a tree structure with desired number of nodes. 119

Chapter 5. Common Controls<br />

This function also has several versions. The following is the one that has the simplest format:<br />

int CTreeCtrl::InsertItem(LPTV_INSERTSTRUCT lpInsertStruct);<br />

The only parameter to this function is a TV_INSERTSTRUCT type pointer:<br />

typedef struct _TV_INSERTSTRUCT{<br />

HTREEITEM hParent;<br />

HTREEITEM hInsertAfter;<br />

TV_ITEM item;<br />

} TV_INSERTSTRUCT;<br />

In a tree control, nodes are managed through handles. After a node is created, it will be assigned an<br />

HTREEITEM type handle. Each node has a different handle, so we can use the handle to access a specific<br />

node. In the above structure, member hParent indicates which node is the parent of the new node. If we<br />

assign NULL to this member, the new node will become the root node. Likewise, member hInsertAfter is<br />

used to indicate where the new node should be inserted. We can specify a node handle, or we can use<br />

predefined parameters TVI_FIRST, TVI_LAST or TVI_SORT to insert the new node after the first node, last<br />

node or let the nodes be sorted automatically.<br />

Member item is a TV_ITEM type object, and the structure contains the information of the new node:<br />

typedef struct _TV_ITEM {<br />

UINT mask;<br />

HTREEITEM hItem;<br />

UINT state;<br />

UINT stateMask;<br />

LPSTR pszText;<br />

int cchTextMax;<br />

int iImage;<br />

int iSelectedImage;<br />

int cChildren;<br />

LPARAM lParam;<br />

} TV_ITEM;<br />

In order to add new nodes, we need to understand how to use the following four members of this<br />

structure: mask, pszText, iImage and iSelectedImage.<br />

Member mask indicates which of the other members in the structure contain valid data. Besides mask,<br />

every member of this structure has a corresponding mask flag listed as follows:<br />

Member<br />

Mask Flag<br />

hItem TVIF_HANDLE<br />

state, stateMask TVIF_STATE<br />

pszText, cchTextMax TVIF_TEXT<br />

iImage TVIF_IMAGE<br />

iImage, iSelectedImage TVIF_SELECTEDIMAGE<br />

cChildren TVIF_CHILDREN<br />

lParam TVIF_PARAM<br />

In order to use members pszText, iImage and iSelectedImage, we need to set the following bits of<br />

member mask:<br />

TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT<br />

Member pszText is a pointer to a null-terminated string text that will be used to label this node.<br />

Member iImage and iSelectedImage are indices to two images contained in the image list that will be<br />

used to represent the node’s normal and selected state respectively.<br />

By calling function CTreeCtrl::InsertItem(…) repeatedly, we could create a tree structure with<br />

desired number of nodes.<br />

119

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!