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

}<br />

SelectDropTarget(hitem);<br />

m_hTreeDragTgt=hitem;<br />

m_pilDrag->DragEnter(this, point);<br />

}<br />

}<br />

CTreeCtrl::OnMouseMove(nFlags, point);<br />

WM_LBUTTONUP<br />

Finally we need to implement WM_LBUTTONUP message handler. In this handler, we must first leave<br />

dragging mode and end dragging. This can be implemented by calling functions CImageList::<br />

DragLeave(...) and CImageList::EndDrag() respectively. Then, we need to delete dragging image list<br />

object:<br />

……<br />

void MCTreeCtrl::OnLButtonUp(UINT nFlags, CPoint point)<br />

{<br />

if(m_bIsDragging)<br />

{<br />

ASSERT(m_pilDrag != NULL);<br />

m_pilDrag->DragLeave(this);<br />

m_pilDrag->EndDrag();<br />

delete m_pilDrag;<br />

m_pilDrag=NULL;<br />

The following code fragment shows how to judge if the source node can be copied to become the child<br />

of the target node:<br />

……<br />

……<br />

if<br />

(<br />

)<br />

m_hTreeDragSrc != m_hTreeDragTgt &&<br />

GetChildItem(m_hTreeDragTgt) != NULL &&<br />

!IsDescendent(m_hTreeDragTgt, m_hTreeDragSrc)<br />

If the source and target are the same node, or target node does not have any child node, or source node<br />

is the parent node (including indirect parent) of the target node, the copy should not be implemented.<br />

Otherwise, we need to call function MCTreeCtrl::CopyItem(…) to implement node copy:<br />

……<br />

}<br />

{<br />

CopyItemTo(m_hTreeDragTgt, m_hTreeDragSrc);<br />

//DeleteItem(m_hTreeDragTgt);<br />

}<br />

else ::MessageBeep(0);<br />

::ReleaseCapture();<br />

m_bIsDragging=FALSE;<br />

SelectDropTarget(NULL);<br />

}<br />

CTreeCtrl::OnLButtonUp(nFlags, point);<br />

If we want the node to be moved instead of being copied, we can delete the source node after copying<br />

it. The source node and all its child nodes will be deleted by calling function CTreeCtrl::DeleteItem(…).<br />

Functions CWnd::SetCapture() and ::ReleaseCapture() are also called in MCTreeCtrl::<br />

OnBegindrag(…) and MCTreeCtrl::OnLButtonUp(…) respectively to set and release the window capture. By<br />

doing this, we can still trap mouse messages even if it moves outside the client window when dragging is<br />

undergoing.<br />

That’s all we need to do for implementing drag-n-drop copying. By compiling and executing the<br />

sample application at this point, we will be able to copy nodes through mouse dragging. With minor<br />

130

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

Saved successfully!

Ooh no, something went wrong!