Programming with Microsoft Visual C++.NET 6ed [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Programming with Microsoft Visual C++.NET 6ed [Electronic resources] - نسخه متنی

George Shepherd, David Kruglinski

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید








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 MSDN Library 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 must 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've 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:






















Function


Description


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 24-3 illustrates the MFC drag-and-drop process.


Figure 24-3: MFC OLE drag-and-drop processing.

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



  1. The user presses the left mouse button in the source view window.



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



  3. The handler stores formats in a COleDataSource object.



  4. The handler calls COleDataSource::DoDragDrop for the data source.



  5. The 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 that 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. The 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.





/ 319