1.首先创建VS2010对话框应用程序
2.将对话框属性中的Accept File属性改为True
3.在类向导中添加WW_DROPFILE消息响应函数
4.在函数体中添加如下代码(获取一个文件):
TCHAR * pFilePathName = new TCHAR[2048];
//获取拖放文件的完整文件名
::DragQueryFileW(hDropInfo,0,pFilePathName,2048);
CString m_strFilePathName;
m_strFilePathName.Format(_T("%s"),pFilePathName);
//释放上述操作分配的空间
::DragFinish(hDropInfo);
delete[] pFilePathName;
5.在对话框中添加编辑框并添加相关变量str
6.在上述代码中添加:
str = m_strFilePathName;
UpdateData(FALSE);
7.在函数体重添加如下代码(获取多个文件路径):
CString strCount; TCHAR * lpszFileName = new TCHAR[1024]; // 获取拖入的文件数量 int nFileCount = ::DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 1024); strCount.Format(_T("您拖入了%i个文件"), nFileCount); AfxMessageBox(strCount); for (int i=0;i < nFileCount;i++) { // 获取拖入的第i个文件的文件名 UINT nChars = ::DragQueryFileW(hDropInfo, i, lpszFileName,1024); CString strTemp(lpszFileName, nChars); AfxMessageBox(strTemp); } ::DragFinish (hDropInfo); delete[] lpszFileName;