Adding Drop Target Capabilities
To enable your application to become a drop target, follow these steps:
1.Initialize the OLE libraries. You should check the build version and call the OleInitialize function exactly as you would for a drop source.
2.Call the RegisterDragDrop function. OLE keeps a list of the windows that are drop targets. Every window that accepts dropped objects must register itself and its IDropTarget interface pointer. Then when the user drags the object over a drop target window, OLE has the IDropTarget interface pointer handy.
3.Implement the IDropTarget interface. OLE uses the IDropTarget interface pointer that you registered with RegisterDragDrop to keep you informed of the state of a drop operation.
When the cursor first enters a registered drop target window, OLE calls the IDropTarget::DragEnter member function. In this member function, you should ensure that your application can create the dragged object if it is dropped. Your application may also display visual feedback showing where the dropped object will appear, if appropriate.
When the cursor moves around inside a drop target window, OLE calls the IDropTarget::DragOver member function, just as Windows 95 sends WM_MOUSEMOVE messages. Here you should update any visual feedback that your application displays to reflect the current cursor position. When the cursor leaves a drop target window, OLE calls the IDropTarget::DragLeave member function. In your DragLeave member function, you should remove any feedback you displayed during DragOver or DragEnter.
OLE calls your IDropTarget::Drop member function when the user drops the object. To be precise, a drop occurs when the drop source returns the DRAGDROP_DROP value from the IDropSource::QueryContinueDrag member function. In your Drop member function, you should create an appropriate object from IDataObject that is passed as a parameter. The following example shows how to implement IDropTarget::Drop.
STDMETHODIMP CDropTarget::Drop (LPDATAOBJECT pDataObj,
DWORD grfKeyState, POINTL pointl, LPDWORD pdwEffect)
{
FORMATETC fmtetc;
SCODE sc = S_OK;
UndrawDragFeedback(); // removes any visual feedback
// QueryDrop returns TRUE if the app. can accept a drop based on
// the current key state, requested action, and cursor position.
if (pDataObj && QueryDrop(grfKeyState,pointl,FALSE,pdwEffect)) {
m_pDoc->m_lpSite = CSimpleSite::Create(m_pDoc);
m_pDoc->m_lpSite->m_dwDrawAspect = DVASPECT_CONTENT;
// Initialize the FORMATETC structure.
fmtetc.cfFormat = NULL;
fmtetc.ptd = NULL;
fmtetc.lindex = -1;
fmtetc.dwAspect = DVASPECT_CONTENT; // draws object's content
fmtetc.tymed = TYMED_NULL;
HRESULT hrErr = OleCreateFromData
(pDataObj,IID_IOleObject,OLERENDER_DRAW,
&fmtetc, &m_pDoc->m_lpSite->m_OleClientSite,
m_pDoc->m_lpSite->m_lpObjStorage,
(LPVOID FAR *)&m_pDoc->m_lpSite->m_lpOleObject);
if (hrErr == NOERROR)
// The object was created successfully.
else
// The object creation failed.
sc = GetScode(hrErr);
}
return ResultFromScode(sc);
}
4.Call the RevokeDragDrop function. Before a drop target window is destroyed, it must call RevokeDragDrop to allow OLE to remove the window from its list of drop targets.
5.Uninitialize the OLE libraries. Like a drop source, your application needs to uninitialize the OLE libraries before terminating.