The Wayback Machine - https://web.archive.org/web/20170628020916/http://www.codeguru.com/cpp/controls/treeview/dragdrop/article.php/c659/Drag-and-Drop-between-Tree-controls.htm

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

Top White Papers and Webcasts

  • On-Demand Webinar Your developers are shifting to a DevOps delivery model to increase the speed of delivering new code and features to your customers. But as a security and IT practitioner, you want security-first, enterprise-wide solutions that support your development teams without compromising on security. During this session, we discuss why it is important to: Secure and manage secrets used by machines (e.g. micro-services, applications, scripts, CI/CD tools, hosts, etc.) and privileged users …

  • Organizations today are under tremendous pressure to deliver a higher quality of products and services at lower costs, and to do so using existing resources. Any expenditure companies do make to help them achieve this goal is expected to deliver a measurable, hard-dollar ROI — and to deliver it quickly. The Internet of Things (IoT) is creating new opportunities for companies to enhance their products, gain business insights and differentiate their offerings. This whitepaper defines an ROI model for …

Most Popular Programming Stories

More for Developers

RSS Feeds

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