Drag and drop was the ultimate justification for the data object code you've been looking at. OLE supports this feature with its IDropSource and IDropTarget interfaces plus some library code that manages the drag-and-drop process. The MFC library offers good drag-and-drop support at the view level, so we'll use it. Be aware that drag-and-drop transfers are immediate and independent of the clipboard. If the user cancels the operation, there's no "memory" of the object being dragged.
Drag-and-drop transfers should work consistently between applications, between windows of the same application, and within a window. When the user starts the operation, the cursor should change to an arrow_rectangle combination. If the user holds down the Ctrl key, the cursor turns into a plus sign (+), which indicates that the object is being copied rather than moved.
MFC also supports drag-and-drop operations for items in compound documents. This is the next level up in MFC OLE support, and it's not covered in this chapter. Look up the OCLIENT example in the online
documentation under Visual C++ Samples.
The Source Side of the Transfer
When your source program starts a drag-and-drop operation for a data object, it calls COleDataSource::DoDragDrop. This function internally creates an object of MFC class COleDropSource, which implements the IOleDropSource interface. DoDragDrop is one of those functions that don't return for a while. It returns when the user drops the object or cancels the operation or when a specified number of milliseconds have elapsed.
If you're programming drag-and-drop operations to work with a CRectTracker object, you should call DoDragDrop only when the user clicks inside the tracking rectangle, not on its border. CRectTracker::HitTest gives you that information. When you call DoDragDrop, you need to set a flag that tells you whether the user is dropping the object into the same view (or document) that it was dragged from.
The Destination Side of the Transfer
If you want to use the MFC library's view class drag-and-drop support, you must add a data member of class COleDropTarget to your derived view class. This class implements the IDropTarget interface, and it holds an IDropSource pointer that links back to the COleDropSource object. In your view's OnInitialUpdate function, you call the Register member function for the embedded COleDropTarget object.
After you have made your view a drop target, you must override four CView virtual functions, which the framework calls during the drag-and-drop operation. Here's a summary of what they should do, assuming that you're using a tracker.
OnDragEnter | Adjusts the focus rectangle and then calls OnDragOver |
OnDragOver | Moves the dotted focus rectangle and sets the drop effect (determines cursor shape) |
OnDragLeave | Cancels the transfer operation; returns the rectangle to its original position and size |
OnDrop | Adjusts the focus rectangle and then calls the DoPaste helper function to get formats from the data object |
Figure 26-4 illustrates the MFC drag-and-drop process.
Figure 26-4. MFC OLE drag-and-drop processing.
Here's a summary of what's going on: