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

}<br />

ff.Close();<br />

If we execute the sample, we need to wait for a while before the procedure of building directory tree is<br />

completed. This waiting time is especially long for a system containing many drives and directories. This is<br />

why we only add fist and second level directories to the tree view window at the beginning. If we build the<br />

whole directory map before bringing up the window, the user will experience a very long waiting time. We<br />

will add new nodes to the tree only when a node is expanded and its sub-level contents need to be revealed.<br />

14.6 Simple Explorer, Step 4: Displaying Files<br />

In the sample, all files will be listed in the list view that is located at the right pane of the client splitter<br />

window. Like what is implemented in class CDirView, we need to enumerate files under a directory and add<br />

corresponding nodes to the list control in order to display the files.<br />

The list view window will display all the directories and files contained in the selected directory. If the<br />

currently selected directory changes, we must destroy the list view and create a new one. For this purpose,<br />

in the sample, a new function CExplorerView::ChangeDir() is implemented, which can be used to create<br />

the list view from the currently selected directory.<br />

Image Lists<br />

Before adding any file to the list view, we need to prepare image lists. This procedure is almost the<br />

same with that of tree view. The only difference between the two is that for list view we have more choices.<br />

This is because the items contained in a list view can be displayed in different styles, and for each style we<br />

can use a different type of images.<br />

A list view can display items in one of the four styles: big icon (default style), small icon, list, report.<br />

We can prepare two image lists, one for big icon style, one for other three styles.<br />

We can display different file types using different icons, this is how the files are displayed in real<br />

“Explorer” application. Under Windows, each type of files can register both big and small icons in the<br />

system, and “Explorer” will use the registered icons for file displaying. To get the registered icons, we need<br />

to call some special functions. We will implement this method in later sections. Here, we will prepare our<br />

own icons for displaying files. In the sample, two sets of image resources are included in the applications,<br />

one of them will be used for displaying directories and the other for displaying files. Their IDs are<br />

IDB_BITMAP_CLOSEFOLDERBIG, IDB_BITMAP_CLOSEFOLDER, IDB_BITMAP_FILEBIG and IDB_BITMAP_FILE.<br />

In the sample, big icon image list is created from IDB_BITMAP_CLOSEFOLDERBIG and<br />

IDB_BITMAP_FILEBIG. Small icon image list is created from IDB_BITMAP_CLOSEFOLDER and<br />

IDB_BITMAP_FILE. The creation of image list is the same with what we did for the tree view. When an<br />

image list is selected into the list control, we must specify the type of image list. The following portion of<br />

function CExplorerView::ChangeDir() shows how the image lists are selected into the list control in the<br />

sample:<br />

……<br />

……<br />

GetListCtrl().SetImageList(pilSmall, LVSIL_SMALL);<br />

GetListCtrl().SetImageList(pilNormal, LVSIL_NORMAL);<br />

Here pointer pilSmall and pilNormal point to two different image lists. We use LVSIL_SMALL and<br />

LVSIL_NORMAL to specify the type of the image list.<br />

Adding Columns<br />

First we need to add columns to the list control. The columns will appear in the list control window<br />

when the items contained in it are displayed in “Report” style. For each item, usually the small icon<br />

associated with the item and item label will be displayed at the left most column (column 0). For other<br />

columns, we can display text to list other properties of the item.<br />

442

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

Saved successfully!

Ooh no, something went wrong!