11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14. Views<br />

memset(&lvfi, 0, sizeof(LV_FINDINFO));<br />

lvfi.flags|=LVFI_PARAM;<br />

lvfi.lParam=lParam;<br />

}<br />

return lc.FindItem(&lvfi);<br />

Here a LV_FINDINFO type object is stuffed, with LVFI_PARAM bit of member flags set to “1” and the<br />

item parameter assigned to member lParam. Then the object is passed to function CListCtrl::<br />

FindItem(…) to search the item in the list control. Function CExplorerView::FindItem(…)’s second<br />

parameter is a CListCtrl type reference, this is because within static member function, we must use the<br />

instance of an object to call any of its non-static functions. This function returns the current index of the<br />

corresponding item.<br />

Comparing Two Items by File Names<br />

The procedure of comparing two items is described in the following paragraphs.<br />

First we pass the parameters of the items to function CExplorerView::FindItem(…) to retrieve their<br />

current indices. After the indices are obtained, we stuff a LV_ITEM type object, set LVIF_IMAGE bit of<br />

member mask to “1” and call function CListCtrl::GetItem(…). Since in the sample, a directory item is<br />

always associated with the default image (In the image list, the image index is 0), we can use an item’s<br />

image index to tell if it represents a directory or a file. For different situations, the comparing function will<br />

return different values (In the sample, a directory always preceeds a file item):<br />

……<br />

……<br />

int CExplorerView::CompareByName(LPARAM lParam1, LPARAM lParam2, CListCtrl &lc)<br />

{<br />

nItem1=FindItem(lParam1, lc);<br />

nItem2=FindItem(lParam2, lc);<br />

memset(&lvi1, 0, sizeof(LV_ITEM));<br />

memset(&lvi2, 0, sizeof(LV_ITEM));<br />

lvi1.mask|=LVIF_IMAGE;<br />

lvi1.iItem=nItem1;<br />

lvi2.mask|=LVIF_IMAGE;<br />

lvi2.iItem=nItem2;<br />

lc.GetItem(&lvi1);<br />

lc.GetItem(&lvi2);<br />

szName1=lc.GetItemText(nItem1, 0);<br />

szName2=lc.GetItemText(nItem2, 0);<br />

if(lvi1.iImage == 0 && lvi2.iImage != 0)return -1;<br />

if(lvi1.iImage != 0 && lvi2.iImage == 0)return 1;<br />

In case both items are directories or files, we need to further compare their names. Since file names<br />

under Windows are case insensitive, we neglect character case when performing the comparison. The<br />

comparison is done within a for loop, which starts from the first characters and ends under one of the<br />

following situations: 1) The two compared characters are different, in which case the character that has the<br />

greater value belongs to the item that should follow the other. 2) One of the strings reaches its end. In this<br />

case the item with longer file name should follow the other. If two strings are exactly the same, the function<br />

returns 0:<br />

……<br />

nSize=min(szName1.GetLength(), szName2.GetLength());<br />

szName1.MakeLower();<br />

szName2.MakeLower();<br />

for(i=0; i szName2[i])return 1;<br />

}<br />

if(i == nSize)<br />

{<br />

452

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

Saved successfully!

Ooh no, something went wrong!