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

{<br />

bWorking=ff.FindNextFile();<br />

if(!ff.IsDots())<br />

{<br />

szFileName=ff.GetFileName();<br />

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

lvi.iItem=i;<br />

lvi.iSubItem=0;<br />

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

lvi.iImage=ff.IsDirectory() ? 0:1;<br />

……<br />

GetListCtrl().InsertItem(&lvi);<br />

Unlike tree control, there is no handle here to identify a special item. All the items are identified by<br />

their indices, this means if we display items in “list” or “report” style, the item located at the first row is<br />

item 0, the next row is item 1… and so on. When inserting an item, we need to specify the item index by<br />

using member iItem of LV_ITEM structure.<br />

We store file size (for directory, display nothing), file type (“File” or “Folder”), the updated time in a<br />

string array that will be used to add text for the sub-items. These attributes of file can be retrieved by<br />

calling functions CFileFind::GetLength(), CFileFind::IsDirectory() and CFileFind::<br />

GetLastWriteTime(…). When calling the third function to obtain the update time of a file, we get a CTime<br />

type variable. To store the time in a CString type variable in ASCII format, we need to call function<br />

CTime::Format(…). The following portion of function CExplorerView::ChangeDir() shows how the string<br />

array is created:<br />

……<br />

……<br />

}<br />

if(!ff.IsDirectory())<br />

{<br />

szText.Empty();<br />

szText.Format("%lu Bytes", ff.GetLength());<br />

szArray.Add(szText);<br />

szText.Empty();<br />

szText="File";<br />

}<br />

else<br />

{<br />

szArray.Add("");<br />

szText="Folder";<br />

}<br />

szArray.Add(szText);<br />

ff.GetLastWriteTime(time);<br />

szText.Empty();<br />

szText=time.Format("%d/%m/%Y %I:%M%p");<br />

szArray.Add(szText);<br />

}<br />

i++;<br />

The text of sub-items is added by calling function CListCtrl::SetItemText(…). This can also be<br />

implemented by stuffing LV_ITEM type object (specifying item and sub-item indices) and calling function<br />

CListCtrl::SetItem(…). The following portion of function CExplorerView::ChangeDir() shows how<br />

this is implemented in the sample:<br />

……<br />

for(i=0; i

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

Saved successfully!

Ooh no, something went wrong!