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 />

modifications to the above message handlers, we can easily implement both node copy and move as<br />

follows: when CTRL key is held down, the node can be copied through drag-n-drop, when there is no key<br />

held down, node will be moved.<br />

5.15 List Control<br />

A list control is another type of control that can be used to manage a list of objects. Rather than storing<br />

items in a tree structure, a list control simply organize them into an array. There is no parent or child node<br />

in a list control.<br />

A list control can be viewed in different styles: 1) Normal icon style ⎯ each item is represented by a<br />

big icon. 2) Small icon style ⎯ each item is represented by a small icon. 3) List style ⎯ all items are<br />

represented by small icons contained in a vertical list. 4) Report style ⎯ the details of all items are listed in<br />

several vertical lists.<br />

In <strong>MFC</strong>, list control is supported by class CListCtrl. Implementing list control is similar to<br />

implementing tree control: the list control resource can be created in dialog template, then the list control<br />

can be initialized in the dialog’s initialization stage. Each item in the list control can be associated with one<br />

or more images, they will be used to represent the item in different styles. Usually we need to associate two<br />

images for an item: one big image for normal style, and a small image for other three styles. In general<br />

case, we need to prepare two image lists to create a list control.<br />

LV_COLUMN and LV_ITEM<br />

The procedure of initializing list control is similar to that of tree control. First we need to create two<br />

image lists: one for normal icon style; one for small icon style. Then we need to call function<br />

CListCtrl::SelectImageList(…) to associate the image lists with the list control. The following is the<br />

format of this function:<br />

CImageList *CListCtrl::SetImageList(CImageList *pImageList, int nImageList);<br />

Here pImageList is a pointer to the image list, and nImageList specifies the type of image list: it could<br />

be LVSIL_NORMAL or LVSIL_SMALL, representing which style the image list will be used for.<br />

After the image list is set, we need to add columns for the list control (Figure 5-12). This can be<br />

implemented by calling function CListCtrl::InsertColumn(…), which has the following format:<br />

int CListCtrl::InsertColumn(int nCol, const LV_COLUMN* pColumn);<br />

The function has two parameters. The first one indicates which column is to be added (0 based index),<br />

and the second one is a pointer to LV_COLUMN type object:<br />

typedef struct _LV_COLUMN {<br />

UINT mask;<br />

int fmt;<br />

int cx;<br />

LPSTR pszText;<br />

int cchTextMax;<br />

int iSubItem;<br />

} LV_COLUMN;<br />

Here, member mask indicates which of the other members contain valid values, this is the same with<br />

structure LV_ITEM. Member fmt indicates the text alignment for the column, it can be LVCFMT_LEFT,<br />

LVCFMT_RIGHT, or LVCFMT_CENTER. Member cx indicates the width of the column, and iSubItem indicates<br />

its index. Member pszText is a pointer to the text string that will be displayed for each column. Finally,<br />

cchTextMax specifies the size of buffer pointed by pszText.<br />

131

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

Saved successfully!

Ooh no, something went wrong!