CDropSource* pDropSource = NULL;
POSITION pos = m_listFiles.GetFirstSelectedItemPosition();
std::vector<tFileInfo*> vectFileInfos;
while ( pos )
{
int iItem = m_listFiles.GetNextSelectedItem( pos );
tFileInfo* pFileInfo = (tFileInfo*)m_listFiles.GetItemData( iItem );
vectFileInfos.push_back( pFileInfo );
}
CDropSource* pTempDropSource = new CDropSource;
HRESULT hr = pTempDropSource->QueryInterface( IID_IDropSource, (void FAR* FAR*)&pDropSource );
LPDATAOBJECT pIDataObject;
CDragDropDataObject* pTempObject;
FORMATETC fe;
STGMEDIUM stm;
memset((void far *)&stm,0,sizeof(stm));
stm.tymed = TYMED_HGLOBAL;
stm.hGlobal = NULL; // data will be provided in DataObject::GetData delayed rendering
pTempObject = new CDragDropDataObject;
hr = pTempObject->QueryInterface(IID_IDataObject,(void FAR* FAR*)&pIDataObject);
if(SUCCEEDED(hr))
{
memset((void far *)&fe,0,sizeof(fe));
fe.tymed = TYMED_HGLOBAL;
fe.ptd = NULL;
fe.lindex = -1;
fe.dwAspect = DVASPECT_CONTENT;
// set the File Contents format
/*
if(bSupportFileContents)
{
fe.cfFormat = g_cfFileContents;
pIDataObject->SetData(&fe,&stm,TRUE);
fe.cfFormat = g_cfFileGroupDescriptor;
pIDataObject->SetData(&fe,&stm,TRUE);
}
// set the ShellIDList format
if(bSupportFileName)
{
fe.cfFormat = g_cfFileName;
pIDataObject->SetData(&fe,&stm,TRUE);
}
// set the ShellIDList format
if(bSupportShellIDList)
{
fe.cfFormat = g_cfShellIDList;
pIDataObject->SetData(&fe,&stm,TRUE);
}
// set the CF_HDROP format
*/
//if(bSupportHDROP)
{
fe.cfFormat = CF_HDROP;
{
// for my sample I am assuming number of files as 2
int cnt = (int)vectFileInfos.size();
// allocate space for DROPFILE structure plus the number of file and one extra byte for final NULL terminator
stm.hGlobal = GlobalAlloc(GHND|GMEM_SHARE,(DWORD) (sizeof(DROPFILES)+(_MAX_PATH)*cnt+1));
/*
if(stm.hGlobal == NULL)
return hGlobal;
*/
LPDROPFILES pDropFiles;
pDropFiles = (LPDROPFILES)GlobalLock(stm.hGlobal);
// set the offset where the starting point of the file start
pDropFiles->pFiles = sizeof(DROPFILES);
// file contains wide characters
pDropFiles->fWide=FALSE;
int CurPosition = sizeof(DROPFILES);
for (int i = 0;i < cnt;i++)
{
tFileInfo* pFileInfo = vectFileInfos[i];
std::string strTempFile = CreateTempFile( pFileInfo );
m_mapDragDropTempFiles[pFileInfo] = strTempFile;
lstrcpy( (LPSTR)( (LPSTR)( pDropFiles ) + CurPosition ), strTempFile.c_str() );
// move the current position beyond the file name copied
// don't forget the Null terminator (+1)
CurPosition += (int)strTempFile.length() + 1;
}
// final null terminator as per CF_HDROP Format specs.
((LPSTR)pDropFiles)[CurPosition]=0;
GlobalUnlock(stm.hGlobal);
}
//extern HGLOBAL CreateHDrop();
//stm.hGlobal = CreateHDrop();
pIDataObject->SetData(&fe,&stm,TRUE);
}
pDropSource->AddListener( this );
// start the do drag drop
m_bDraggingSource = true;
DragAcceptFiles( FALSE );
DWORD dwEffect = 0;
hr = DoDragDrop( pIDataObject, pDropSource, DROPEFFECT_COPY | DROPEFFECT_MOVE | DROPEFFECT_LINK , &dwEffect );
if(!SUCCEEDED(hr))
{
// OutputDebugString(" Drag Drop failed \n");
}
pIDataObject->Release();
DragAcceptFiles();
m_bDraggingSource = false;
}
pDropSource->Release();