Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


drag and drop

Posted on 2006-10-19
Medium Priority
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 2000 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 : http://support.microsoft.com/kb/q148738/


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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Loops Section Overview

580 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