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

Columns<br />

Figure 5-12. Columns in a list control<br />

After columns are created, we need to add list items. For each list item, we need to insert a sub-item in<br />

each column. For example, if there are three columns and 4 list items, we need to add totally 12 sub-items.<br />

To add a sub-item, we need to stuff an LV_ITEM type object then call function CListCtrl::<br />

InsertItem(…), which has the following format:<br />

int CListCtrl::InsertItem(const LV_ITEM* pItem);<br />

The following is the format of structure LV_ITEM:<br />

typedef struct _LV_ITEM {<br />

UINT mask;<br />

int iItem;<br />

int iSubItem;<br />

UINT state;<br />

UINT stateMask;<br />

LPSTR pszText;<br />

int cchTextMax;<br />

int iImage;<br />

LPARAM lParam;<br />

} LV_ITEM;<br />

The usage of this structure is similar to that of structure TV_ITEM. For each item, we need to use this<br />

structure to add every sub-item for it. Usually only the sub-items contained in the first column will have an<br />

associated image (when being displayed in report style), so we need to set image for each item only once.<br />

Member iItem and iSubItem specify item index and column index respectively.<br />

Sample<br />

Sample 5.15\CCtl demonstrates how to use list control. It is a dialog-based application generated by<br />

Application Wizard. In this sample, a four-item list is implemented, which can be displayed in one of the<br />

four styles. When it is displayed in report style, the control has four columns. The first column lists four<br />

shapes: square, rectangle, circle, triangle. The second column lists the formula for calculating their<br />

perimeter, and the third column lists the formula for calculating their area.<br />

Creating Image Lists<br />

In the dialog template, the list control has an ID of IDC_LIST. In order to access this control, a<br />

CListCtrl type variable m_listCtrl is added to class CCCtldlg through using Class Wizard.<br />

Four icon resources are added to the application for creating image lists. Their IDs are<br />

IDI_ICON_SQUARE, IDI_ICON_RECTANGLE, IDI_ICON_CIRCLE and IDI_ICON_TRIANGLE respectively. In the<br />

previous samples, we created image list from bitmap resource all the time. Actually, it can also be created<br />

from icon resources as well.<br />

132

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

Saved successfully!

Ooh no, something went wrong!