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

Actually, in the callback function, one of the above functions is called to perform the comparison<br />

according to parameter lParamSort:<br />

int CALLBACK CExplorerView::CompareFunc<br />

(<br />

LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort<br />

)<br />

{<br />

CListCtrl &lc=<br />

(<br />

(CExplorerDoc *)<br />

(<br />

((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetActiveDocument()<br />

)<br />

)->GetCExplorerView()->GetListCtrl();<br />

switch((int)lParamSort)<br />

{<br />

case 0:<br />

{<br />

return CompareByName(lParam1, lParam2, lc);<br />

break;<br />

}<br />

case 1:<br />

{<br />

return CompareBySize(lParam1, lParam2, lc);<br />

break;<br />

}<br />

case 2:<br />

{<br />

return CompareByType(lParam1, lParam2, lc);<br />

break;<br />

}<br />

case 3:<br />

{<br />

return CompareByDate(lParam1, lParam2, lc);<br />

break;<br />

}<br />

}<br />

}<br />

return 0;<br />

Please note that the callback function must be either a global function or a static member function. So<br />

within it we cannot call CListView::GetListCtrl() directly to obtain the list control. Instead, we must<br />

first obtain the current instance of list view, then use it to call function CListView::GetListCtrl() and<br />

obtain the list control. This is why at the beginning of the callback function the current active document is<br />

first obtained, from which the current active list view (and the list control) is obtained.<br />

Using Parameter to Find an Item<br />

Within the function that implements comparison, the only information we know about an item is its<br />

parameter. This is not enough for making comparison. We need to obtain the item and get more<br />

information (In our sample, this includes file name, extension, type, and updated time) before proceeding to<br />

compare the two items.<br />

An item can be obtained from its parameter by calling function CListCtrl::FindItem(…). In order to<br />

call this function, we need to stuff a LV_FINDINFO type object specifying what information is provided for<br />

item searching. To search an item by its parameter, we need to set LVFI_PARAM bit of member flags of the<br />

structure, and assign the parameter to member lParam. In the sample, a static member function<br />

CExplorerView::FindItem(…) is implemented, it can be called from any static member function to find an<br />

item using its parameter:<br />

int CExplorerView::FindItem(LPARAM lParam, CListCtrl &lc)<br />

{<br />

LV_FINDINFO lvfi;<br />

451

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

Saved successfully!

Ooh no, something went wrong!