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

void MCTreeCtrl::OnBegindrag(NMHDR *pNMHDR, LRESULT *pResult)<br />

{<br />

CPoint pt;<br />

UINT nFlags;<br />

……<br />

NM_TREEVIEW *pNMTreeView=(NM_TREEVIEW *)pNMHDR;<br />

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

ScreenToClient(&pt);<br />

ASSERT(m_bIsDragging == FALSE);<br />

m_bIsDragging=TRUE;<br />

m_hTreeDragSrc=HitTest(pt, &nFlags);<br />

m_hTreeDragTgt=NULL;<br />

Next, we need to obtain dragging image list for this node. The dragging image list is created by calling<br />

function CTreeCtrl::CreateDragImage(…). After this, the address of the image list object is stored in<br />

variable m_pilDrag. If the image list is created successfully, we call several member functions of<br />

CImageList to display the dragging image and enter dragging mode. If not, we should not start dragging,<br />

and need to set the content of pResult to a non-zero value, this will stop dragging:<br />

……<br />

}<br />

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

m_pilDrag=CreateDragImage(m_hTreeDragSrc);<br />

if(m_pilDrag != NULL)<br />

{<br />

m_pilDrag->DragShowNolock(TRUE);<br />

m_pilDrag->SetDragCursorImage(0, CPoint(0, 0));<br />

m_pilDrag->BeginDrag(0, CPoint(0,0));<br />

m_pilDrag->DragMove(pt);<br />

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

SetCapture();<br />

*pResult=0;<br />

}<br />

else<br />

{<br />

m_bIsDragging=FALSE;<br />

*pResult=1;<br />

}<br />

WM_MOUSEMOVE<br />

Then we need to implement WM_MOUSEMOVE message handler. Whenever the mouse cursor moves to a<br />

new place, we need to call function CImageList::DragMove(…) to move the dragging image so that the<br />

image will always follow the mouse’s movement. We need to check if the mouse hits a new node by<br />

calling function CTreeCtrl::HitTest(…). If so, we must leave dragging mode by calling function<br />

CImageList:: DragLeave(…), highlight the new node by calling function<br />

CTreeCtrl::SelectDropTarget(…), and enter dragging mode again by calling function<br />

CTreeCtrl::DragEnter(…). The reason for doing this is that when dragging is undergoing, the tree control<br />

window is locked and no update could be implemented successfully. The following is the implementation<br />

of this message handler:<br />

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

{<br />

HTREEITEM<br />

hitem;<br />

UINT<br />

flags;<br />

if(m_bIsDragging)<br />

{<br />

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

m_pilDrag->DragMove(point);<br />

if((hitem=HitTest(point, &flags)) != NULL)<br />

{<br />

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

129

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

Saved successfully!

Ooh no, something went wrong!