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

returned value should be the handle of the item that is currently under the mouse cursor. If the mouse is not<br />

over any item, the function will return NULL. Please note that when calling this function, the coordinates<br />

of the mouse cursor should be in the coordinate system of the client window. We need to call function<br />

CWnd::ScreenToClient(…) to make the conversion.<br />

When an Item Is Clicked<br />

Special attention needs to be paid to directory nodes labeled with “.” and “..”. When they are clicked,<br />

the current working directory should be changed differently. For the purpose of demonstration, in the<br />

sample, no change will be made if the user clicks any of the two types of directories. If the user clicks a<br />

normal directory node, we need to change the current working directory to the selected one and notify the<br />

list view to update its contents. This notification is made through calling function<br />

CBrowserDoc::ChangePath(), within which function CBrowserView::ChangeDir() is called. However, if<br />

the user clicks on the currently selected directory, no change will be made. The following is the message<br />

handler for mouse left button clicking:<br />

void CDirView::OnClick(NMHDR *pNMHDR, LRESULT *pResult)<br />

{<br />

CPoint pt;<br />

HTREEITEM hItem;<br />

UINT nFlags;<br />

CString szPath;<br />

char path[_MAX_PATH];<br />

}<br />

::GetCursorPos(&pt);<br />

ScreenToClient(&pt);<br />

hItem=GetTreeCtrl().HitTest(pt, &nFlags);<br />

if(hItem != NULL)<br />

{<br />

szPath=GetTreeCtrl().GetItemText(hItem);<br />

if(szPath.Find('.') == -1)<br />

{<br />

szPath=GetDir(hItem);<br />

if(szPath.GetLength())<br />

{<br />

if(_getcwd(path, _MAX_PATH) != NULL)<br />

{<br />

if(stricmp((LPCSTR)szPath, path) != 0)<br />

{<br />

if(_chdir((LPCSTR)szPath) == 0)<br />

{<br />

((CExplorerDoc *)GetDocument())->ChangePath();<br />

}<br />

}<br />

}<br />

}<br />

}<br />

}<br />

*pResult=0;<br />

When a Node Expands<br />

When a node is expanding, we need to check the newly revealed nodes to see if they always contain<br />

child nodes. If not, we need to add child nodes (if they have sub-directories) to them because this will<br />

enable node button automatically. In the sample, function CDirView::AddChildrenChildren() is<br />

implemented to let us add new nodes for all the child nodes of a given node. Within this function, we check<br />

each child node to see if it already has child items (which means the sub-directories have already been<br />

added for this node). If not, we call function CDirView::AddDirs(…) to add new nodes to it. A node’s child<br />

item can be enumerated by calling function CTreeCtrl::GetChildItem(…) first then calling<br />

CTreeCtrl::GetNextSiblingItem(…) repeatedly until it returns NULL value. Also, we can call function<br />

CTreeCtrl::ItemHasChildren(…) to examine if a node already has child nodes. The following is the<br />

implementation of function CDirView::AddChildrenChildren(…):<br />

448

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

Saved successfully!

Ooh no, something went wrong!