Article ID: 185572
Article Last Modified on 11/21/2006
135299 How to add OLE drag-and-drop functionality to a pre-existing MFC application
// OnDrop is called by OLE DLLs when an item is dropped in a window
// that is registered with the OLE DLLs.
//
BOOL COleEditDropTarget::OnDrop(CWnd* pWnd, COleDataObject* pDataObject,
DROPEFFECT dropEffect, CPoint point )
{
HGLOBAL hGlobal;
LPCSTR pData;
FORMATETC fmtetc =
{ CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stgmed;
TCHAR szFileName[_MAX_PATH + 1];
if(pDataObject->GetData(CF_HDROP, &stgmed, &fmtetc))
{
HDROP hdrop = (HDROP)GlobalLock(stgmed.hGlobal);
if (NULL != hdrop)
{
UINT nFiles = DragQueryFile(hdrop, (UINT)-1, NULL, 0);
for(UINT nNames = 0; nNames < nFiles; nNames++)
{
ZeroMemory(szFileName, _MAX_PATH + 1);
DragQueryFile
(hdrop, nNames, (LPTSTR)szFileName, _MAX_PATH + 1);
// Do something with szFileName.
}
GlobalUnlock(hdrop);
}
ReleaseStgMedium(&stgmed);
return TRUE;
} else {
// Get text data from ColeDataObject.
hGlobal=pDataObject->GetGlobalData(CF_TEXT);
// Get a pointer to data.
pData=(LPCSTR)GlobalLock(hGlobal);
ASSERT(pData!=NULL);
// Set text in the dropped window.
((CEdit*)pWnd)->SetWindowText(pData);
// Unlock memory.
GlobalUnlock(hGlobal);
return TRUE;
}
}
(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by
Robert Duke, Microsoft Corporation.
Additional query words: Drag Drop DragAcceptFiles CF_
Keywords: kbinfo kbclipboard kbdataobject kbdragdrop kbhowto KB185572