We help IT Professionals succeed at work.

drag and drop

jhav1594 used Ask the Experts™
Hi All,
I have a custom dialogbox which has a systreeview32 class on the left and a syslistview32 class on the right of the window.

when i click on the of the folder in the tree, it display's all the files (in that folder) in the listview on the right.
I want to be able to drag all the files selected on the listview and drop it into any of the folders on the tree view.

how can i do that.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Author of the Year 2009

These two codeguru links should provide the basics
   Class for drag-drop enabled TreeView
   ListView Control

I implemented this functionality in an SDI with two Views and a splitter (not a dialog-based app) and there may be some differences, but it seemed pretty straight-forward.  First, get the functionality for dragging a single item (handl the LVN_BEGINDRAG message, etc), then it's easy to handle multiples.
IT Professional
Top Expert 2005
Refer MSDN sample 'MFCDRAG'. MFCDRAG shows how to implement drag and drop between a CListCtrl and CTreeCtrl. The program creates a splitter window containing four panes. Two panes contain CListCtrls and the other two contain CTreeCtrls. You can drag an item from any window to another.

MFCDRAG shows how to implement drag and drop between a CListCtrl and CTreeCtrl : http://support.microsoft.com/kb/q148738/

Surya BobbadhiTechnical Consultant


 There are two ways to perform drag and drop

1) Using OLE Drag and Drop
2) Using Drag and Drop functionality directly from TreeControl and List control.

1) If you are developing an application that can act as a data source for an OLE drag-and-drop operation, you must call DoDragDrop when you detect that the user has started an OLE drag-and-drop operation.

The DoDragDrop function enters a loop in which it calls various methods in the IDropSource and IDropTarget interfaces. (For a successful drag-and-drop operation, the application acting as the data source must also implement IDropSource, while the target application must implement IDropTarget.)

The DoDragDrop function determines the window under the current cursor location. It then checks to see if this window is a valid drop target.
If the window is a valid drop target, DoDragDrop calls IDropTarget::DragEnter. This method supplies an effect code indicating what would happen if the drop actually occurred. For a list of valid drop effects, see the DROPEFFECT enumeration.
DoDragDrop calls IDropSource::GiveFeedback with the effect code so that the drop source interface can provide appropriate visual feedback to the user. The pDropSource pointer passed into DoDragDrop specifies the appropriate IDropSource interface.
DoDragDrop tracks mouse cursor movements and changes in the keyboard or mouse button state.
If the user moves out of a window, DoDragDrop calls IDropTarget::DragLeave.
If the mouse enters another window, DoDragDrop determines if that window is a valid drop target and then calls IDropTarget::DragEnter for that window.
If the mouse moves but stays within the same window, DoDragDrop calls IDropTarget::DragOver.
If there is a change in the keyboard or mouse button state, DoDragDrop calls IDropSource::QueryContinueDrag and determines whether to continue the drag, to drop the data, or to cancel the operation based on the return value.
If the return value is S_OK, DoDragDrop first calls IDropTarget::DragOver to continue the operation. This method returns a new effect value and DoDragDrop then calls IDropSource::GiveFeedback with the new effect so appropriate visual feedback can be set. For a list of valid drop effects, see the DROPEFFECT enumeration. IDropTarget::DragOver and IDropSource::GiveFeedback are paired so that as the mouse moves across the drop target, the user is given the most up-to-date feedback on the mouse's position.
If the return value is DRAGDROP_S_DROP, DoDragDrop calls IDropTarget::Drop. The DoDragDrop function returns the last effect code to the source, so the source application can perform the appropriate operation on the source data, for example, cut the data if the operation was a move.
If the return value is DRAGDROP_S_CANCEL, the DoDragDrop function calls IDropTarget::DragLeave.

2)A tree control (CTreeCtrl) sends a notification when the user starts to drag an item. The control sends a TVN_BEGINDRAG notification message when the user begins dragging an item with the left mouse button and a TVN_BEGINRDRAG notification message when the user begins dragging with the right button. You can prevent a tree control from sending these notifications by giving the tree control the TVS_DISABLEDRAGDROP style.

You obtain an image to display during a drag operation by calling the CreateDragImage member function. The tree control creates a dragging bitmap based on the label of the item being dragged. Then the tree control creates an image list, adds the bitmap to it, and returns a pointer to the CImageList object.

You must provide the code that actually drags the item. This typically involves using the dragging capabilities of the image list functions and processing the WM_MOUSEMOVE and WM_LBUTTONUP (or WM_RBUTTONUP) messages sent after the drag operation has begun. For more information about the image list functions, see CImageList in the MFC Reference and Image Lists in the Platform SDK. For more information about dragging a tree control item, see Dragging the Tree View Item, also in the Platform SDK.

If items in a tree control are to be the targets of a drag-and-drop operation, you need to know when the mouse cursor is on a target item. You can find out by calling the HitTest member function. You specify either a point and integer, or the address of a TVHITTESTINFO structure that contains the current coordinates of the mouse cursor. When the function returns, the integer or structure contains a flag indicating the location of the mouse cursor relative to the tree control. If the cursor is over an item in the tree control, the structure contains the handle of the item as well.

You can indicate that an item is the target of a drag-and-drop operation by calling the SetItem member function to set the state to the TVIS_DROPHILITED value. An item that has this state is drawn in the style used to indicate a drag-and-drop target.