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.

Downloads

Download demo project - 18 Kb
Download source - 5 Kb


Comments

  • 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

    Thanks

    Stephen

    Reply
  • 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);
    ...
    }

    Thanks,
    Colleen

    Reply
  • 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 !

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

    Reply
  • 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.

    Cheers,

    Colin Harrison

    Reply
  • 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.

    Reply
  • 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~!

    Reply
  • 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.
    Geert.

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • On-demand Event Event Date: September 17, 2014 Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this webcast and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the future state of your servers, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds