Drag and Drop between Tree controls
Posted
by Vinayak Tadas
on January 5th, 2000
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
- 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.
- #include afxole.h in stdafx.h
- Call AfxOleInit() in InitInstance().
- Create an object of CDragDropTreeCtrl in your class.
Downloads
Download demo project - 18 KbDownload source - 5 Kb
Comments
How to delete, when adding new items?
Posted by Legacy on 07/16/2003 12:00amOriginally 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
ReplySelected and nonselected switched
Posted by Legacy on 05/08/2003 12:00amOriginally 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,
ReplyColleen
How drag&drop; between treeview and listview
Posted by Legacy on 10/29/2002 12:00amOriginally 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 !
ReplyNice & useful sample.How to do Multiple selection and drag
Posted by Legacy on 08/26/2002 12:00amOriginally posted by: Shilpa
I want to select nodes at random and want to drag and drop them.Reply as soon as possible
Farrukh iftikhar
Posted by smartyy_2 on 04/30/2008 12:40amIts done by only three steps. 1.in aspx.cs file protected void Page_Load(object sender, EventArgs e) { TreeNode sLeaf1 = new TreeNode(); sLeaf1.Text = "
" + "Leaf 21" + "";
}
2.in aspx file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
ReplyUsing drag image as cursor?
Posted by Legacy on 07/31/2000 12:00amOriginally 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
ReplyDragging from another source
Posted by Legacy on 01/19/2000 12:00amOriginally posted by: Ieuan Williams
Doesn't work between process
Posted by Legacy on 01/14/2000 12:00amOriginally 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~!
ReplyEndless loop
Posted by Legacy on 11/10/1999 12:00amOriginally posted by: Geert Delmeiren