11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5. Common Controls<br />

dialog box. To implement tree control in a view, we can implement the view using class CTreeView. To<br />

implement tree control in a dialog box, we need to use CTreeCtrl class. In this section we will focus on<br />

dialog box implementation of tree control.<br />

Like other common controls, we can add tree control resources to the dialog template when designing<br />

application’s resource. The tree control will have an ID, which could be used to access the control (by<br />

either calling function CWnd::GetDlgItem(…) or adding CTreeCtrl type member variable).<br />

Image List<br />

We can associate a bitmap image with each node contained in the tree control. This will make the tree<br />

control more intuitive. For example, in a file manager application, we may want to use different images to<br />

represent different file types: folder, executable file, DLL file, etc. Before using the images to implement<br />

the tree control, we must first prepare them. For tree control (also list control and tab control), these images<br />

must be managed by Image List, which is supported by class CImageList in <strong>MFC</strong>.<br />

Class CImageList can keep and manage a collection of images with the same size. Each image in the<br />

list is assigned a zero-based index. After an image list is created successfully, it can be selected into the tree<br />

control. We can associate a node with any image contained in the image list. Here image drawing is<br />

handled automatically.<br />

If we provide mask bitmaps, only the unmasked portion of the images will be drawn for representing<br />

nodes. A mask bitmap must contain only black and white colors. Besides preparing mask bitmaps by<br />

ourselves, we can also generate mask bitmaps from the normal images.<br />

To use class CImageList, first we need to declare a CImageList type variable. If we create an image<br />

list dynamically by using “new” operator, we need to release the memory when it is no longer in use.<br />

Before adding images to the list, we need to call function CImageList::Create(…) to initialize it. This<br />

function has several versions, the following is one of them:<br />

BOOL CImageList::Create(int cx, int cy, UINT nFlags, int nInitial, int nGrow);<br />

Here cx and cy indicate the dimension of all images, nInitial represents the number of initial bitmaps<br />

that will be included in the image list, nGrow specifies the number of bitmaps that can be added later.<br />

Parameter nFlags indicates bitmap types, it could be ILC_COLOR, ILC_COLOR4, ILC_COLOR8, etc., which<br />

specify the bitmap format of the images. For example, ILC_COLOR indicates default bitmap format,<br />

ILC_COLOR4 indicates 4-bit DIB format (16-color), ILC_COLOR8 indicates 8-bit DIB format (256-color). We<br />

can combine ILC_MASK with any of these bitmap format flags to let the image be drawn with transparency.<br />

The images can be added by calling function CImageList::Add(…). Again, this function has three<br />

versions:<br />

int CImageList::Add(CBitmap *pbmImage, CBitmap *pbmMask);<br />

int CImageList::Add(CBitmap *pbmImage, COLORREF crMask);<br />

int CImageList::Add(HICON hIcon);<br />

The image list can be created from either bitmaps or icons. For the first version of this function, the<br />

second parameter is a pointer to the mask bitmap that will be used to implement transparent background<br />

drawing. The second version allows us to specify a background color that can be used to generate a mask<br />

bitmap from the normal image. Here parameter crMask will be used to create the mask bitmap: all pixels in<br />

the source bitmap that have the same color with crMask will be masked when the bitmap is being drawn,<br />

and their colors will be set to the current background color. We can choose a background color by calling<br />

function CImageList::SetBkColor(…).<br />

To use image list with a tree control, we need to call function CTreeCtr::SetImageList(…) to assign it<br />

to tree control. Then, when creating a node for the tree control, we can use the bitmap index to associate<br />

any node with this image.<br />

Adding Nodes<br />

At the beginning, the tree control does not contain any node. Like other common controls, we can<br />

initialize it in function CDialog::OnInitDialog(). To add a node to the tree, we need to call function<br />

CTreeCtrl::InsertItem(…).<br />

118

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

Saved successfully!

Ooh no, something went wrong!