您的位置:首页 > 其它

VC实现文件拖拽

2013-01-07 10:32 176 查看
在基于对话框的程序中,默认是没有这个消息的。

1、按下Ctrl+W,弹出类向导对话框,选择Class Info标签;

2、在Message fileter下拉列表中选择Window,然后再点击Message Maps标签;

3、这时就出现WM_DROPFILES消息了,添加该消息的响应函数。

[cpp] view plaincopyprint?void CDragDlg::OnDropFiles(HDROP hDropInfo)
{
// TODO: Add your message handler code here and/or call default

CDialog::OnDropFiles(hDropInfo);
}
[cpp] view plaincopyprint?UINT DragQueryFile(          HDROP hDrop,
UINT iFile,
LPTSTR lpszFile,
UINT cch
);
UINT DragQueryFile(          HDROP hDrop,
UINT iFile,
LPTSTR lpszFile,
UINT cch
);


6、参数解释:
hDrop: HDROP标识符,即响应函数中的hDropInfo参数
iFile: 待查询的文件索引号,从0开始。可以同时拖拽多个文件,因此就需要一个索引号来进行区分。如果该参数为0xFFFFFFFF,则该函数返回拖拽的文件的个数
lpszFile: 用于存放文件名的缓冲区首地址
cch: 缓冲区长度
返回值:文件名长度

7、查询完成后需要释放系统分配内存,使用下面这个函数:

[cpp] view plaincopyprint?VOID DragFinish( HDROP hDrop
);
[cpp] view plaincopyprint?void CDragDlg::OnDropFiles(HDROP hDropInfo)
{
// TODO: Add your message handler code here and/or call default
UINT count;
char filePath[200];
count = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
if(count)
{
for(UINT i=0; i<count; i++)
{
int pathLen = DragQueryFile(hDropInfo, i, filePath, sizeof(filePath));
AfxMessageBox(filePath);
}
}
DragFinish(hDropInfo);
CDialog::OnDropFiles(hDropInfo);
}
void CDragDlg::OnDropFiles(HDROP hDropInfo)
{
// TODO: Add your message handler code here and/or call default
UINT count;
char filePath[200];
count = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
if(count)
{
for(UINT i=0; i<count; i++)
{
int pathLen = DragQueryFile(hDropInfo, i, filePath, sizeof(filePath));
AfxMessageBox(filePath);
}
}
DragFinish(hDropInfo);
CDialog::OnDropFiles(hDropInfo);
}


9、同理,如果只有把文件拖拽到特定的控件中时才有响应,只需要把该控件的Accept files样式勾选上即可。

from:

/article/2960311.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: