drag and drop

Posted on 2006-10-19
Last Modified: 2013-11-20
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.
Question by:jhav1594
LVL 49

Expert Comment

ID: 17771371
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.
LVL 22

Accepted Solution

mahesh1402 earned 500 total points
ID: 17771704
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 :


Expert Comment

ID: 17773184

 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.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
GIF file bit manipulation for color change 5 107
ODBC Connection Logging, ADO.NET 6 55
sum28 challenge 31 105
wait notify demo infinite loop 3 94
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now