Advanced MFC Programming
Advanced MFC Programming Advanced MFC Programming
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
- Page 83 and 84: Chapter 3. Splitter Window WM_LBUTT
- Page 85 and 86: Chapter 4. Button Chapter 4 Buttons
- Page 87 and 88: Chapter 4. Button Use string text a
- Page 89 and 90: Chapter 4. Button function, first t
- Page 91 and 92: Chapter 4. Button Now we can remove
- Page 93 and 94: Chapter 4. Button } ( m_bBmpCheck ?
- Page 95 and 96: Chapter 4. Button using bit-wise AN
- Page 97 and 98: Chapter 4. Button Overriding Functi
- Page 99 and 100: Chapter 4. Button …… First pBit
- Page 101 and 102: Chapter 4. Button Using Class MCBit
- Page 103 and 104: Chapter 4. Button The button’s ID
- Page 105 and 106: Chapter 4. Button However, since me
- Page 107 and 108: Chapter 4. Button Here, we use subc
- Page 109 and 110: Chapter 5. Common Controls command
- Page 111 and 112: Chapter 5. Common Controls ((CSpinB
- Page 113 and 114: Chapter 5. Common Controls } The in
- Page 115 and 116: Chapter 5. Common Controls …… B
- Page 117 and 118: Chapter 5. Common Controls The “S
- Page 119 and 120: Chapter 5. Common Controls button
- Page 121 and 122: Chapter 5. Common Controls In this
- Page 123 and 124: Chapter 5. Common Controls (CBN_CLO
- Page 125 and 126: Chapter 5. Common Controls compare
- Page 127 and 128: Chapter 5. Common Controls } ptrEdi
- Page 129 and 130: Chapter 5. Common Controls ……
- Page 131 and 132: Chapter 5. Common Controls void CWn
- Page 133: Chapter 5. Common Controls Five loc
- Page 137 and 138: Chapter 5. Common Controls After cr
- Page 139 and 140: Chapter 5. Common Controls typedef
- Page 141 and 142: Chapter 5. Common Controls Using th
- Page 143 and 144: Chapter 5. Common Controls Paramete
- Page 145 and 146: Chapter 5. Common Controls void MCT
- Page 147 and 148: Chapter 5. Common Controls modifica
- Page 149 and 150: Chapter 5. Common Controls In funct
- Page 151 and 152: Chapter 5. Common Controls Changing
- Page 153 and 154: Chapter 5. Common Controls m_tabCtr
- Page 155 and 156: Chapter 5. Common Controls Custom R
- Page 157 and 158: Chapter 6. Dialog Box Chapter 6 Dia
- Page 159 and 160: Chapter 6. Dialog Box void CMLDialo
- Page 161 and 162: Chapter 6. Dialog Box } Function CP
- Page 163 and 164: Chapter 6. Dialog Box Everything is
- Page 165 and 166: Chapter 6. Dialog Box } MINMAXINFO;
- Page 167 and 168: Chapter 6. Dialog Box Sample Sampel
- Page 169 and 170: Chapter 6. Dialog Box } brush=(HBRU
- Page 171 and 172: Chapter 6. Dialog Box wndA.GetWindo
- Page 173 and 174: Chapter 6. Dialog Box rectStaticGrp
- Page 175 and 176: Chapter 6. Dialog Box } TOOLTIPTEXT
- Page 177 and 178: Chapter 6. Dialog Box ) { } if ( )
- Page 179 and 180: Chapter 6. Dialog Box pWndCtrl=GetW
- Page 181 and 182: Chapter 6. Dialog Box Sample 6.8-2\
- Page 183 and 184: Chapter 6. Dialog Box } // handle t
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