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

CFileFind ff;<br />

BOOL bWorking;<br />

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

while(bWorking)<br />

{<br />

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

AfxMessageBox(ff.GetFileName());<br />

}<br />

Note we can also use wildcard characters when calling function CFileFind::FindFile(…) to match<br />

file name with specific patterns. If we do not pass any parameter to it, it will be equal to passing “*.*” to<br />

the function. In this case all the files and directories will be enumerated. If function CFileFind::<br />

FindNextFile() returns a non-zero value, we can call several other member functions of class CFileFind<br />

to obtain the properties of the enumerated file such as file name, file path, file attributes, created time and<br />

updated time.<br />

Adding Directory Nodes<br />

In order to add directory node to the tree view window, we need to implement a loop and enumerate<br />

directories for each available drive, then add the enumerated directories to the tree.<br />

Besides directories, we also need to enumerate sub-directories for the expanded nodes. This is because<br />

our tree control supports buttons (we have set TVS_HASBUTTONS style), which indicates whether an item has<br />

child items or not. For a node that contains child items, its button will contain a “+” sign when the node is<br />

in collapsed state, indicating that the node is expandable. For a node that does not contain child items, there<br />

will be no such type of buttons.<br />

So the directory enumeration can be added to the loop of enumerating all drives: after an existing drive<br />

is found, we can change the current working directory to the root directory of this drive, then enumerate all<br />

the first-level directories and all the sub-directories of each first-level directory.<br />

Function CDirView::AddDirs(…) is implemented in the sample, it will be used to add directory items<br />

to a specified node. It has two parameters, the first is the handle of the target tree item, and the second is a<br />

Boolean type variable indicating if we should further add sub-directories for each added directory node.<br />

The following is the format of this function:<br />

void CDirView::AddDirs(HTREEITEM hTreeItem, BOOL bFindChild);<br />

Before calling this function, we need to change the current working directory to the directory we want<br />

to examine. So in function CDirView::OnInitialUpdate(…), after one drive node is added to the tree view<br />

window, we change the current working directory to root directory of that drive, and call function<br />

CDirView::AddDirs(…) to add nodes for the directories. The following is the modified portion of function<br />

CDirView::OnInitialUpdate(…):<br />

……<br />

……<br />

for(drive=1; drive

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

Saved successfully!

Ooh no, something went wrong!