Enhanced Drag '& Drop

I modified the code for using drag & drop on tree controls. The idea was to add three features found on the ClassView tree control of the VC++ ide. With the following code you can disable the drag & drop interface for certain items on the tree (the root item in ClassView, for example), and you can decide whether an item is a valid drop target or not, and in case not, provide an alternative item as a drop target (behavior observed in ClassView when you drag classes between folders; only folders are drop targets). Also, I update the cursor shape to reflect the status of the drag & drop operation.

The member variables needed are:

	CImageList*     m_pDragImage;
	BOOL            m_bLDragging;
	HTREEITEM       m_hitemDrag,m_hitemDrop;
	HCURSOR         m_dropCursor,m_noDropCursor;

Member variables m_dropCursor and m_noDropCursor must be initialized to the desired cursors.

Here is my version of OnBeginDrag:
void CTreeCtrlX::OnBeginDrag(NMHDR* pNMHDR, LRESULT* pResult) 

	// TODO: Add your control notification handler code here
	*pResult = 0;

	m_hitemDrag = pNMTreeView->itemNew.hItem;
	m_hitemDrop = NULL;
	SelectItem( m_hitemDrag );
	if (!IsDropSource(m_hitemDrag))

	// get the image list for dragging 
	m_pDragImage = CreateDragImage(m_hitemDrag);  
	// CreateDragImage() returns NULL if no image list 
	// associated with the tree view control 
	if( !m_pDragImage )

	m_bLDragging = TRUE;
	m_pDragImage->BeginDrag(0, CPoint(15, 15));
	POINT pt = pNMTreeView->ptDrag;
	ClientToScreen( &pt );
	m_pDragImage->DragEnter(NULL, pt);

This member function calls IsDropSource() virtual function to decide if the item can be a source of a drag & drop operation. If IsDropSource() returns FALSE, the drag is cancelled. Here is the default IsDropSource() member function:

/* virtual */ BOOL CTreeCtrlX::IsDropSource(HTREEITEM hItem)
	return TRUE;  // all items are valid sources

I also modified the OnMouseMove() handler:

void CTreeCtrlX::OnMouseMove(UINT nFlags, CPoint point) 
	UINT flags;

	if (m_bLDragging)
		POINT pt = point;
		ClientToScreen( &pt );
		if ((hitem = HitTest(point, &flags)) != NULL)
			m_hitemDrop = GetDropTarget(hitem);
			m_hitemDrop = NULL;
		if (m_hitemDrop)

	CTreeCtrl::OnMouseMove(nFlags, point);

This version, calls GetDropTarget() virtual function to decide if the item behind the cursor is a valid drop target. This function should return NULL if the item is not a valid target for a drop. If the item can accept a drop, it should return its HTREEITEM handle. As an alternative, a different HTREEITEM can be returned to specify a different drop target. This can be useful, for example, if you don't accept drops on tree leafs, but want to direct the drop to the parent. Also I change the cursor to reflect the status of the operation (OnSetCursor is not called while mouse is captured, so the cursor can be safely changed here).

Here is the default implemmentation of the GetDropTarget() virtual function:

/* virtual */ HTREEITEM CTreeCtrlX::GetDropTarget(HTREEITEM hItem)
	// inhibit drop on the drop source or its parent
	if (hItem == m_hitemDrag || hItem == GetParentItem(m_hitemDrag))
		return NULL;
	return hItem;

Finally, here is the OnLButtonUp() handler:

void CTreeCtrlX::OnLButtonUp(UINT nFlags, CPoint point) 
	CTreeCtrl::OnLButtonUp(nFlags, point);

	if (m_bLDragging)
		m_bLDragging = FALSE;

		delete m_pDragImage;

		// Remove drop target highlighting

		if( m_hitemDrag == m_hitemDrop || m_hitemDrop == NULL)

		// If Drag item is an ancestor of Drop item then return
		HTREEITEM htiParent = m_hitemDrop;
		while( (htiParent = GetParentItem( htiParent )) != NULL ) 
			if( htiParent == m_hitemDrag ) return;

		Expand( m_hitemDrop, TVE_EXPAND ) ;

		HTREEITEM htiNew = CopyBranch(m_hitemDrag, m_hitemDrop, TVI_LAST ); 
		SelectItem( htiNew );


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's enterprise datacenter can be one of the most complex business environments with dozens (for smaller business), hundreds (for larger and midsize business), even thousands (for hyperscale businesses) of servers that must be managed and monitored. At this level, just managing the cords can be challenging — let alone keeping up with the growing need for more agility and scalability within the datacenter. Simply put, companies are aggressively looking for less complexity and more agility from their …

  • The future of cloud platforms is at hand. Even if your cloud applications are basic now, your next set of apps will require strong analytics services and tools, as well as features that ease enterprise integration. To fill these needs, consider using not only your current cloud provider but also specialists. Take advantage of not only the big cloud platforms, but also specialized providers in vertical industries; countries and regions; and functional domains including omnichannel, analytics, integration, and …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date