3,248
社区成员




bool CImageItem::DragDropTo2(QString path, int DropStyle)
{
OleInitialize(NULL); // needed for Drag and Drop
CIDropSource* src = new CIDropSource;
if (src == nullptr) return false;
CIDataObject* obje = new CIDataObject(src);
if (obje == nullptr)
{
OleUninitialize();
delete src;
return false;
}
FORMATETC fmtetc = { CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stgmed = { TYMED_HGLOBAL, { 0 }, 0 };
STGMEDIUM dest = { TYMED_HGLOBAL, { 0 }, 0 };
stgmed.hGlobal = GlobalAlloc(GMEM_MOVEABLE, strlen(path.toLatin1().data()) + 1);
char *pChar = (char *)GlobalLock(stgmed.hGlobal);
strcpy(pChar, path.toLatin1().data());
GlobalUnlock(stgmed.hGlobal);
obje->CopyMedium(&dest, &stgmed, &fmtetc);
DWORD dwEffect;
DWORD dwResult;
dwResult = DoDragDrop(obje, src, DROPEFFECT_COPY | DROPEFFECT_MOVE, &dwEffect);
if (dwResult == DRAGDROP_S_DROP)
{
//通过调试输出,这里是成功的。
OutputDebugString(L"[3dh] ok");
if (dwEffect & DROPEFFECT_MOVE)
{
//这里并没有过来。。。。
OutputDebugString(L"[3dh] ok2");
}
}
else if (dwResult == DRAGDROP_S_CANCEL)
{
OutputDebugString(L"[3dh] no");
}
//正常来说这里应该有上面两个指针的Release()。但是如果Release会崩掉。。。这是问题2.。。。
if (obje != null)
obje->Release() //崩溃。
if (src != null)
src->.RElease();
OleUninitialize();
return true;
}
2097: /*
2098: * Predefined Clipboard Formats
2099: */
2100: #define CF_TEXT 1
2101: #define CF_BITMAP 2
2102: #define CF_METAFILEPICT 3
2103: #define CF_SYLK 4
2104: #define CF_DIF 5
2105: #define CF_TIFF 6
2106: #define CF_OEMTEXT 7
2107: #define CF_DIB 8
2108: #define CF_PALETTE 9
2109: #define CF_PENDATA 10
2110: #define CF_RIFF 11
2111: #define CF_WAVE 12
2112: #define CF_UNICODETEXT 13
2113: #define CF_ENHMETAFILE 14
2114: #if(WINVER >= 0x0400)
2115: #define CF_HDROP 15
2116: #define CF_LOCALE 16
2117: #define CF_MAX 17
2118: #endif /* WINVER >= 0x0400 */
IDropSource *pDropSource;
IDataObject *pDataObject;
FORMATETC fmtetc = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stgmed = { TYMED_HGLOBAL, { 0 }, 0 };
stgmed.hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT | GMEM_DDESHARE, MAX_PATH * 2);
LPDROPFILES pDropFiles = (LPDROPFILES)::GlobalLock(stgmed.hGlobal);
pDropFiles->pFiles = sizeof(DROPFILES);
pDropFiles->fWide = TRUE;
LPBYTE pData = (LPBYTE)pDropFiles + sizeof(DROPFILES);
memcpy(pData, path, sizeof(WCHAR)*wcslen(path));
::GlobalUnlock(stgmed.hGlobal);
if (CDropSource::CreateDropSource(&pDropSource) != S_OK) return false;
if (CDataObject::CreateDataObject(&fmtetc, &stgmed, 1, &pDataObject) != S_OK)
{
delete pDropSource;
return false;
}
DWORD dwEffect;
DWORD dwResult;
dwResult = DoDragDrop(pDataObject, pDropSource, DROPEFFECT_COPY, &dwEffect);
pDataObject->Release();
pDropSource->Release();
void CHMI_manView::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
// check select
CMainFrame* pMain=(CMainFrame*)AfxGetMainWnd();
CListCtrl& RightCtrl=this->GetListCtrl();
// check selection
if(!pMain->CheckCurrentSelection(FALSE,TRUE,TRUE))//mul;no ':' no dir
{//only multi files ;no drive;no dir
*pResult = 0;
return;
}
char Selection[40*MaxSelectNum];
// get selection
if(!pMain->GetCurrentSelection(Selection,FALSE))
{// as src
*pResult = 0;
return;
}
// selected list-items
int iItem=m_pIndx[0];
//
HGLOBAL hGlobal=GlobalAlloc(GMEM_SHARE,strlen(Selection)+1);
LPSTR pszGlobal=(LPSTR)GlobalLock(hGlobal);
ASSERT(pszGlobal);
strcpy(pszGlobal,Selection);
GlobalUnlock(hGlobal);
COleDataSource* pSource=new COleDataSource();
pSource->CacheGlobalData(CF_TEXT,hGlobal);
// save for checking
SetDragItem(iItem);
// drag image
if(pMain->m_pDragImage) delete pMain->m_pDragImage;
// create only 1 ??
CPoint pthot(0,0);
pMain->m_pDragImage=RightCtrl.CreateDragImage(iItem,&pthot);
// pos of item
CRect rc;
RightCtrl.GetItemRect(iItem,&rc,LVIR_BOUNDS);
// pos of cursor
CPoint pt,hpt;
GetCursorPos(&pt);
RightCtrl.ScreenToClient(&pt);// pt is in DeskTop
// hotpoint
hpt.x=pt.x-rc.left+12;
hpt.y=pt.y-rc.top;
// Start dragging the image.
pMain->m_pDragImage->BeginDrag(0,hpt);
// do dragdrop
DROPEFFECT de;
de=pSource->DoDragDrop(DROPEFFECT_COPY | DROPEFFECT_MOVE );//| DROPEFFECT_SCROLL);
// delete as many items as selected in list
if(de==DROPEFFECT_MOVE)
{
ListReMoveItems();
}
// reset all
SetDragItem(-1);
pMain->m_pDragImage->EndDrag();
//
if(pSource) delete pSource;
*pResult = 0;
}
注意
COleDataSource* pSource=new COleDataSource();