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 5. Common Controls<br />

TV_INSERTSTRUCT tvInsertStruct;<br />

char szBuf[256];<br />

HTREEITEM hTreeItem;<br />

}<br />

tvInsertStruct.item.mask=<br />

(<br />

TVIF_CHILDREN |<br />

TVIF_HANDLE |<br />

TVIF_IMAGE |<br />

TVIF_SELECTEDIMAGE |<br />

TVIF_TEXT |<br />

TVIF_STATE<br />

);<br />

tvInsertStruct.item.pszText=szBuf;<br />

tvInsertStruct.item.cchTextMax=sizeof(szBuf);<br />

tvInsertStruct.item.hItem=hTreeDragSrc;<br />

GetItem(&tvInsertStruct.item);<br />

tvInsertStruct.hParent=hTreeDragTgt;<br />

tvInsertStruct.hInsertAfter=TVI_LAST;<br />

hTreeItem=InsertItem(&tvInsertStruct);<br />

if(tvInsertStruct.item.cChildren != 0)<br />

{<br />

hTreeDragSrc=GetChildItem(hTreeDragSrc);<br />

while(TRUE)<br />

{<br />

if(hTreeDragSrc == NULL)break;<br />

CopyItemTo(hTreeItem, hTreeDragSrc);<br />

hTreeDragSrc=GetNextItem(hTreeDragSrc, TVGN_NEXT);<br />

}<br />

}<br />

This function copies node hTreeDragSrc along with all its descendent nodes, and make them the child<br />

nodes of hTreeDragTgt. First we call function CTreeCtrl::GetItem(…) to retrieve source node’s<br />

information. We must pass a TV_ITEM type pointer to this function, and the corresponding object will be<br />

filled with the information of the specified node. Here, we use member item of structure TV_INSERTSTRUCT<br />

to receive a node’s information (Variable tvInsertStruct is declared by TV_INSERTSTRUCT, it will be used<br />

to create new nodes). When calling this function, member mask of TV_ITEM structure specifies which<br />

member should be filled with the node’s information. In our case, we want to know the handle of this node,<br />

the associated images, the text of the label, the current state (expanded, highlighted, etc.), and if the node<br />

has any child node. So we need to set the following bits of member mask: TVIF_CHILDREN, TVIF_HANDLE,<br />

TVIF_IMAGE, TVIF_SELECTEDIMAGE, TVIF_TEXT and TVIFF_STATE. Note we must provide our own buffer to<br />

receive the label text. In the function, szBuf is declared as a char type array and its address is stored in<br />

pszText member of TV_ITEM. Then we use tvInsertStruct to create a new node. Since we have already<br />

stuffed item member with valid information, here we only need to assign the target handle (stored in<br />

hTreeDragTgt) to hParent, and assign TVI_LAST to hInsertAfter. This will make the new node to<br />

become the child of the target node, and be added to the end of all child nodes under the target node. Next<br />

we check if this node has any child node. If so, we find out all the child nodes and call this function<br />

recursively to copy all the child nodes. For this step, we use the newly created node as the target node, this<br />

will ensure that the original tree structure will not change after copying.<br />

In the final step, we call function CTreeCtrl::GetChileItem(…) to find out a node’s first child node,<br />

then call function CTreeCtrl::GetNextitem(…) repeatedly to get the rest child nodes. The two functions<br />

will return NULL if no child node is found.<br />

TVN_BEGINDRAG<br />

Now we need to implement TVN_BEGINDRAG message handler. First, we need to obtain the node that<br />

was clicked by the mouse cursor. To obtain the current position of mouse cursor, we can call API function<br />

::GetCursorPos(…). Since this position is measured in the screen coordinate system, we need to further<br />

call function CWnd::ScreenToClient(…) to convert the coordinates to the coordinate system of the tree<br />

control window. Then we can set variable m_bIsDragging to TRUE, and call function<br />

CTreeCtrl::HitTest(…) to find out if the mouse cursor is over any node:<br />

128

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

Saved successfully!

Ooh no, something went wrong!