很早以前我提供了该资源下载,但是有很多人说没有看懂.所以我今天特别讲解下.
在.cpp文件头声明该消息引用作用的控件ID.
ON_NOTIFY(LVN_BEGINDRAG, IDC_LIST4, OnHdnBegindragList4)
然后实现Drag的具体事件响应:
void CMusicDragAidDlg::OnHdnBegindragList4(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
COleDataSource datasrc;
HGLOBAL hgDrop;
DROPFILES *pDrop;
CStringList lsDraggedFiles;
POSITION pos;
int nSelItem;
CString sFile;
UINT uBuffSize = 0;
TCHAR *pszBuff;
FORMATETC etc = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
*pResult = 0;
pos = m_list.GetFirstSelectedItemPosition();
while ( NULL != pos )
{
nSelItem = m_list.GetNextSelectedItem ( pos );
sFile = saveroute + "/tmp"+HexToDem(sel_tmpDir)+"/" + m_list.GetItemText(nSelItem, 10);
CFileFind find;
if(!find.FindFile(sFile))
{
return;
}else{
lsDraggedFiles.AddTail ( sFile );
uBuffSize += lstrlen ( sFile ) + 1;
}
}
uBuffSize = sizeof(DROPFILES) + sizeof(TCHAR) * (uBuffSize + 1);
hgDrop = GlobalAlloc ( GHND | GMEM_SHARE, uBuffSize );
if ( NULL == hgDrop )
return;
pDrop = (DROPFILES *) GlobalLock ( hgDrop );
if ( NULL == pDrop )
{
GlobalFree ( hgDrop );
return;
}
pDrop->pFiles = sizeof(DROPFILES);
#ifdef _UNICODE
pDrop->fWide = TRUE;
#endif;
// Copy all the filenames into memory after the end of the DROPFILES struct.
pos = lsDraggedFiles.GetHeadPosition();
pszBuff = (TCHAR *) (LPBYTE(pDrop) + sizeof(DROPFILES));
while ( NULL != pos )
{
lstrcpy ( pszBuff, (LPCTSTR) lsDraggedFiles.GetNext ( pos ) );
pszBuff = 1 + _tcschr ( pszBuff, '\0' );
}
GlobalUnlock ( hgDrop );
// Put the data in the data source.
datasrc.CacheGlobalData ( CF_HDROP, hgDrop, &etc );
HGLOBAL hgBool;
hgBool = GlobalAlloc ( GHND | GMEM_SHARE, sizeof(bool) );
if ( NULL == hgBool )
{
GlobalFree ( hgDrop );
return;
}
etc.cfFormat = g_uCustomClipbrdFormat;
datasrc.CacheGlobalData ( g_uCustomClipbrdFormat, hgBool, &etc );
DROPEFFECT dwEffect = datasrc.DoDragDrop ( DROPEFFECT_COPY/* | DROPEFFECT_MOVE*/ );
// list.
switch ( dwEffect )
{
case DROPEFFECT_COPY:
{
// 记录一次拖拽记录
move_drag_log(m_list.GetItemText(nSelItem, 11), "");
break;
}
} // end switch
}