Supporting Drag and Drop

Although this book can't really cover OLE drag-and-drop operations in any detail, let's take a brief look at the basic steps used by ENUMDESK (and MFCENUM) to support the IDropSource interface. All applications containing data that can be dropped into another application must implement this interface. Our samples support IDropSource so that the user can pick up an object in a window and drop it on Windows Explorer or on the desktop.

In addition to the standard IUnknown OLE interface member functions, you must implement two IDropSource functions: QueryContinueDrag, which determines whether a drag operation should continue; and GiveFeedback, which enables a source application to provide feedback during a drag-and-drop operation.

In ENUMDESK and MFCENUM, the QueryContinueDrag function determines whether the user has pressed the Esc key or released the mouse button (signaling a drop). If Esc has been pressed, the drop procedure is canceled; if the mouse button has been released, the drop is finished.

STDMETHODIMP CDropSource::QueryContinueDrag (
BOOL fEscapePressed, DWORD grfKeyState)
{
if (fEscapePressed)
return DRAGDROP_S_CANCEL;
else if (! (grfKeyState & MK_LBUTTON) && ! (grfKeyState & MK_RBUTTON))
return DRAGDROP_S_DROP;
else
return NOERROR;
}

The GiveFeedback implementation in the samples simply directs the system to use the default cursor for the drop operation:

STDMETHODIMP CDropSource::GiveFeedback (DWORD dwEffect)
{
return DRAGDROP_S_USEDEFAULTCURSORS;
}