Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

579 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