Drag and Drop between Tree controls

Environment: VC6 SP2, NT4 SP5

This article demonstrates drag and drop between tree controls using OLE Drag and Drop. It also allows an item to be dropped on same tree control.When an item is dropped all the children of that item are also dropped on the target. The following key combination can be used:

  • Left button, to copy an item
  • Ctrl+Left Button,to copy an item
  • Shift+Left Button,to move an item

There are two classes :

  • CDragDropTreeCtrl, for Tree control. This class also has a helper function AddItem() for adding an item to the tree control.
  • CTreeDropTarget, which provides functionality of Drop target

The CDragDropTreeCtrl registers the tree control as the drop target and starts the drag operation in respose to the TVN_BEGINDRAG message.

The CTreeDropTarget is derived from COleDropTarget and serves as drop target. This class overides the following functions:

  • OnDragEnter().
  • OnDragOver(). This function highlights and expands the item under the mouse pointer and also scrolls the tree control appropriately
  • OnDrop(). This function creates the item being dragged in the destination tree control when the user releases the mouse button.
What you have to do You need to do the following things:
  • #include afxole.h in stdafx.h
  • Call AfxOleInit() in InitInstance().
  • Create an object of CDragDropTreeCtrl in your class.


Download demo project - 18 Kb
Download source - 5 Kb


  • How to delete, when adding new items?

    Posted by Legacy on 07/16/2003 12:00am

    Originally posted by: Stephen Krug

    Has anybody changed the source code, that when moving items also deleting of old items is done?

    Pls give message, if you can show me, where to change



  • Selected and nonselected switched

    Posted by Legacy on 05/08/2003 12:00am

    Originally posted by: Colleen Hillis

    Someone may have said this before, but my selected and non-selected colors were switched. I think it is because of the GetItemImages function:

    GetItemImages(HTREEITEM hSrcTItem, int &nSelItemImage, int &nNonSelItemImage)

    m_pSourceTreeCtrl->GetItemImage(hSrcTItem, nSelItemImage, nNonSelItemImage);

    //Should be
    m_pSourceTreeCtrl->GetItemImage(hSrcTItem, nNonSelItemImage, nSelItemImage);


  • How drag&drop between treeview and listview

    Posted by Legacy on 10/29/2002 12:00am

    Originally posted by: Vu Ngoc Son

    I have read your article, it's very good. My problem is how to drag and drop between treeView and listview. Would you please show me the way to do that. thanks very much !

  • Nice & useful sample.How to do Multiple selection and drag

    Posted by Legacy on 08/26/2002 12:00am

    Originally posted by: Shilpa

    I want to select nodes at random and want to drag and drop them.Reply as soon as possible

  • Using drag image as cursor?

    Posted by Legacy on 07/31/2000 12:00am

    Originally posted by: Colin Harrison

    Does anyone know how I might go about using a tree control's drag image as the cursor during an Ole drag and drop operation? I've tried extracting the image with a call to CreateDragImage, and then using SetCursor during the calls to GiveFeedback of a class drived from COleDataSource.


    Colin Harrison

  • Dragging from another source

    Posted by Legacy on 01/19/2000 12:00am

    Originally posted by: Ieuan Williams

    Another problem with the approach is that it accepts any drag operation from any source, such as some text from Word, or cells from Excel. This causes an exception on exit.

    A better solution might be to register a clipboard format, unique to each running process, and then on registering a drop source pass the registered clipboard format in. Then on OnDragEnter check for this clipboard format, therefore making sure that you're only accepting input from your own process.

    To register a unique clipboard format for a process use:

    BOOL CMyApp::InitInstance()
    // Register the clipboard format:-
    CString strClipFormat;
    strClipFormat.Format ( "MyApp:%08x", GetCurrentProcessId () );
    m_ProcessClipboardFormat = RegisterClipboardFormat ( strClipFormat );

    if ( ! m_ProcessClipboardFormat )
    TRACE1( "Failed to register clipboard format '%s'!\n", strClipFormat );
    return FALSE;

    Then to start off a drag drop operation do the following:-

    STGMEDIUM Medium;
    COleDataSource Source;

    // Setup the medium:-
    Medium.tymed = NULL;
    Medium.pUnkForRelease = NULL;

    // Set the data source cache:-
    Source.CacheData ( theApp.m_ProcessClipboardFormat, & Medium );

    // Set the drag drop source:-
    ASSERT ( ! m_pDragSourceView );
    m_pDragSourceView = this;

    // Now do the drag drop operation:-
    Source.DoDragDrop ( DROPEFFECT_LINK, NULL, NULL );

    Then on OnDragEnter simply do the following check:

    if ( pDataObject->IsDataAvailable ( theApp.m_ProcessClipboardFormat ) )

    That should, in theory, allow you to control what's dropped into your application.

  • Doesn't work between process

    Posted by Legacy on 01/14/2000 12:00am

    Originally posted by: Kim, Min-Jang

    This example works well between tree controls in same process.

    But I execute this example twice (2 example instances), then I drag an item on one instance and drop on the other instance. You'll see it doesn't work!

    The reason I thouht is static variable "m_shWndTreeCtrl".
    Between process drag & drop, this variable dosen't have appropriate handle.

    Between process, the static variable dosen't shared.
    So perfect implementation must based on COleDataObject.

    Maybe.. perfect drag & drop between control seems more complex and harder than I thought :-)

    Have a nice day~!

  • Endless loop

    Posted by Legacy on 11/10/1999 12:00am

    Originally posted by: Geert Delmeiren

    I ran the demo project and found a way to run into and endless loop:

    1. Drag item "Child4" and drop it in the SAME tree on itself.
    [The result is not what I expect it to be.]

    2. Repeat the same action again and you run into the endless loop.

    If you drop the item on itself in another tree it's OK.

    I didn't have the time yet to search for a solution.
    Maybe you have.

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

Top White Papers and Webcasts

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there's simply not enough quality talent to go around. In response, companies often resort to inferior solutions -- hiring substandard developers and engineers, recruiting talent on a part-time or temporary basis, poaching people from competitors, or burdening an already stressed IT staff for more of their labor. Fortunately, there's a better solution. Read this white paper to learn the business value of …

Most Popular Programming Stories

More for Developers

RSS Feeds

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