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

……<br />

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

{<br />

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

hIcon=GetIconFromFile(szFileName, SHGFI_LARGEICON);<br />

if(hIcon != NULL)<br />

{<br />

pilNormal->Add(hIcon);<br />

bUseDefaultNormalIcon=FALSE;<br />

nNumImages++;<br />

}<br />

else<br />

{<br />

hIcon=GetIconFromFile(szFileName, SHGFI_SHELLICONSIZE);<br />

if(hIcon != NULL)<br />

{<br />

pilNormal->Add(hIcon);<br />

bUseDefaultNormalIcon=FALSE;<br />

nNumImages++;<br />

}<br />

}<br />

In rare cases, some files may have small embedded or registered icon but no corresponding big icon, or<br />

vice versa. In any case, the embedded or registered icon has the highre priority to be used. The newly<br />

obtained icon is added to the image list by calling function CImageList::Add(…).<br />

14.8 Simple Explorer, Step 6: Clicking and Double Clicking<br />

Sample 14.8\Exoplorer is based on sample 14.7\Explorer. In this sample, when the user clicks or<br />

double clicks the left button on a directory node contained in the tree control, the current working directory<br />

will be changed and the contents of the list view will also be updated.<br />

If the double clicking expands a node, we need to pay attention to the newly revealed nodes: if any<br />

directory contains sub-directories, we need to add new nodes so that the node button will be automatically<br />

enabled.<br />

Tree Control Messages<br />

Mouse clicking events are sent through WM_NOTIFY messages. For tree control, various activities of tree<br />

items can be handled by processing this message. In <strong>MFC</strong>, this message can be mapped to a member<br />

function by using macro ON_NOTIFY_REFLECT. This macro has two parameters, the first specifies the event<br />

type, the second specifies the member function name. There are many types of events, for example, mouse<br />

button clicking event is defined as NM_CLICK, and the node expanding event is defined as<br />

TVN_ITEMEXPANDING. Fortunately, in <strong>MFC</strong>, message mapping for these events can be easily implemented<br />

through using Class Wizard. In sample 14.8\Explorer, we trap mouse clicking and node expanding events<br />

to functions CDirView:: OnClick(…) and CDirView::OnItemExpanding(…) respectively.<br />

Obtaining Full Path<br />

When the user clicks mouse on a node, first we need to find out the path represented by that node.<br />

Although the directory name is stored as the item text for each node, it is not a full path. We need a full<br />

path in order to change the current working directory. In the sample, function CDirView::GetDir(…) is<br />

implemented to obtain the full path represented by any item. Within this function, we keep on retrieving the<br />

item’s parent node until root is reached, and combining the obtained directory names to form a full path.<br />

Finding out the Clicked Item<br />

After receiving NM_CLICK notification, we can call function CTreeCtrl::HitTest(…) to find out the<br />

handle of the item that was clicked. We need to pass the current mouse position to this function. The<br />

447

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

Saved successfully!

Ooh no, something went wrong!