在基于对话框的程序中,默认是没有这个消息的,按下Ctrl+W,弹出类向导对话框,选择Class Info标签,在Message fileter下拉列表中选择Window,然后再点击Message Maps标签,就出现WM_DROPFILES消息了,添加该消息的响应函数:
void CTestDlg::OnDropFiles(HDROP hDropInfo)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnDropFiles(hDropInfo);
}
另外,要让对话框能够接受文件拖拽,还需要设置对话框属性。在对话框上点击右键,选择Properties->Extended Styles,点选Accept files选项即可。
UINT DragQueryFile(
HDROP hDrop,
UINT iFile,
LPTSTR lpszFile,
UINT cch
);
参数解释:
hDrop: HDROP标识符,即响应函数中的hDropInfo参数
iFile: 待查询的文件索引号,从0开始。可以同时拖拽多个文件,因此就需要一个索引号来进行区分。如果该参数为0xFFFFFFFF,则该函数返回拖拽的文件的个数
lpszFile: 用于存放文件名的缓冲区首地址
cch: 缓冲区长度
返回值:文件名长度
VOID DragFinish(
HDROP hDrop
);
void CTestDlg::OnDropFiles(HDROP hDropInfo)
{
// TODO: Add your message handler code here and/or call default
UINT count;
char filePath[200];
if(count)
{
for(UINT i=0; i<count; i++)
{
int pathLen = DragQueryFile(hDropInfo, i, filePath, sizeof(filePath));
AfxMessageBox(filePath);
}
}
}