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 14. Views<br />

When the list items are displayed in “Report” style, one thing we can implement is to sort all the files<br />

by different attributes. For example, if we click on “Name” column, all the files should be sorted by their<br />

names; if we click on “Size” column, all files should be sorted by their sizes; if we click on “Type” column,<br />

all the files should be sorted by their extensions; if we click on “Updated” column, all the files should be<br />

sorted by their updated dates and times.<br />

Sort Related Functions<br />

We can call function CListCtrl::SortItems(…) to implement item sorting. This function has two<br />

parameters:<br />

BOOL CListCtrl::SortItems(PFNLVCOMPARE pfnCompare, DWORD dwData);<br />

The function’s first parameter is a little special, which is the pointer to a callback function provided by<br />

the programmer. The callback function will be used to perform actual comparison. This is because when<br />

comparing two items, class CListCtrl has no way of knowing which item should precede the other. In<br />

order to provide our own rules of making comparison, we need to implement the callback function.<br />

The callback function has the following format:<br />

int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);<br />

In order to compare two items, we need to provide each item with a parameter, which is an LPARAM<br />

type value. When two items are compared, their parameters will be passed to the callback function, which<br />

will return different values indicating which item should precede the other. If the first item (whose<br />

parameter is lParam1) should precede the second item (whose parameter is lparam2), the function needs to<br />

return -1; if the first item should follow the second item, the function needs to return 1; if the two items are<br />

equal, the function needs to return 0.<br />

When calling function CListView::SortItems(…), we can pass different pre-defined values to<br />

parameter dwData, which will be further passed to parameter lParamSort of the callback function. This<br />

provides us with a way of specifying different types of sorting methods.<br />

Adding Parameters to Items<br />

By now, when creating an item, we did not specify any parameter for it. Actually, any item in the list<br />

control can store a 32-bit parameter which can be used to distinguish one item from another. Of course the<br />

item index can also be used for this purpose. However, since the relative positions of two items can change<br />

frequently, the index of an item is also not fixed. Since the only information passed to the callback function<br />

about an item is its parameter, we must make it unique for any item.<br />

In the sample, function CBrowserView::ChangeDir() is customized as follows: when a new item is<br />

added to the list control, we set its parameter to its initial index and use it as the identification of this item.<br />

This parameter will not change throughout its lifetime:<br />

……<br />

……<br />

lvi.mask=LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;<br />

lvi.iItem=i;<br />

lvi.iSubItem=0;<br />

lvi.pszText=(LPSTR)(const char *)szFileName;<br />

lvi.lParam=(LPARAM)i;<br />

Functions Implementing Comparisons<br />

Four static member functions are implemented for doing different types of comparisons:<br />

static int CExplorerView::CompareByName(LPARAM, LPARAM, CListCtrl &);<br />

static int CExplorerView::CompareBySize(LPARAM, LPARAM, CListCtrl &);<br />

static int CExplorerView::CompareByType(LPARAM, LPARAM, CListCtrl &);<br />

static int CExplorerView::CompareByDate(LPARAM, LPARAM, CListCtrl &);<br />

450

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

Saved successfully!

Ooh no, something went wrong!