MFC Drag and Drop

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

The Drag-and-Drop Sequence

Figure 26-4 illustrates the MFC drag-and-drop process.

click to view at full size.

Figure 26-4. MFC OLE drag-and-drop processing.

Here's a summary of what's going on:

  1. User presses the left mouse button in the source view window.

  2. Mouse button handler calls CRectTracker::HitTest and finds out that the cursor was inside the tracker rectangle.

  3. Handler stores formats in a COleDataSource object.

  4. Handler calls COleDataSource::DoDragDrop for the data source.

  5. User moves the cursor to the view window of the target application.

  6. OLE calls IDropTarget::OnDragEnter and OnDragOver for the COleDropTarget object, which calls the corresponding virtual functions in the target's view. The OnDragOver function is passed a COleDataObject pointer for the source object, which the target tests for a format it can understand.

  7. OnDragOver returns a drop effect code, which OLE uses to set the cursor.

  8. OLE calls IDataSource::QueryContinueDrag on the source side to find out whether the drag operation is still in progress. The MFC COleDataSource class responds appropriately.

  9. User releases the mouse button to drop the object in the target view window.

  10. OLE calls IDropTarget::OnDrop, which calls OnDrop for the target's view. Because OnDrop is passed a COleDataObject pointer, it can retrieve the desired format from that object.

  11. When OnDrop returns in the target program, DoDragDrop can return in the source program.


Programming Microsoft Visual C++
Programming Microsoft Visual C++
ISBN: 1572318570
EAN: 2147483647
Year: 1997
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net