Drag & Drop in TreeControl

Posted on 2003-03-19
Medium Priority
Last Modified: 2013-11-20
I have an MFC app in "explorer style" and I would like to implement a drag & drop functionality. That is if you drag an item in the tree view then I will perform certain actions.

The problem right now is that while I see the selected item while I move the mouse, the image being dragged is always only the first one I selected after I started the program.

Example, I have 5 tree items and after launching the app I select the 2nd item and drag it. I correctly see the image of the 2nd item being dragged. However if I select and drag the 4th item then I still will see the 2nd image - unless I restart the program.

I do delete the ImageList so I don't understand why this is happening. This is the code that I am using:

void CLeftView::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)

     bIsDragDrop = TRUE;

     CTreeCtrl& treeCtrl = GetTreeCtrl();
     HTREEITEM hSelectedNode = treeCtrl.GetSelectedItem();

     CPoint point;

     pImageList = treeCtrl.CreateDragImage(hSelectedNode);
     pImageList->BeginDrag(0, CPoint(0,0));
     pImageList->DragEnter(this, point);

     *pResult = 0;

void CLeftView::OnMouseMove(UINT nFlags, CPoint point)
     if (bIsDragDrop) {
     CTreeView::OnMouseMove(nFlags, point);

void CLeftView::OnLButtonUp(UINT nFlags, CPoint point)
     if (bIsDragDrop) {
          bIsDragDrop = FALSE;


            // "delete pImageList;" yields the same result
          ASSERT(pImageList->GetSafeHandle() == NULL);
     CTreeView::OnLButtonUp(nFlags, point);

Thank you.
Question by:FlorianJaeger
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Accepted Solution

AlexFM earned 500 total points
ID: 8167230
Take a look at this article, it makes some manipulation with second BeginDrag parameter:


Author Comment

ID: 8167301
Thanks for the example, turns out I forgot:


in the MouseButtonUp ... !


Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question