MFC窗口支持文件拖入

修改对话框属性为Accept Files
或者在初始化时直接通过函数DragAcceptFiles()使得窗口支持文件拖曳
然后,增加对话框窗口的WM_DROPFILES消息响应函数

单个文件:
[cpp]  view plain  copy
  1. OnDropFiles(HDROP hDropInfo)  
  2. {  
  3.     char * pFilePath = new char[256];  
  4.     ::DragQueryFile(hDropInfo, 0, pFilePath, 256);  // 获取拖放第一个文件的完整文件名  
  5.     CString m_FilePath;  
  6.     m_FilePath.Format("%s", pFilePath);  
  7.     delete[] pFilePath;  
  8. /* 
  9.     CString m_FilePath; 
  10.     ::DragQueryFile(hDropInfo, 0, m_FilePath.GetBuffer(256), 256); 
  11.     m_FilePath.ReleaseBuffer(); 
  12. */  
  13.     ::DragFinish(hDropInfo);   // 注意这个不能少,它用于释放Windows 为处理文件拖放而分配的内存  
  14. }  


多个文件:
[cpp]  view plain  copy
  1. OnDropFiles(HDROP hDropInfo)  
  2. {  
  3.     int  nFileCount = ::DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 256);   //获取拖入的文件数量  
  4.   
  5.   
  6.     char * pFilePath = new char[256];  
  7.     for (int i=0; i<nFileCount; i++)  
  8.     {  
  9.         UINT nChars = ::DragQueryFile(hDropInfo, i, pFilePath,256);   // 获取拖入的第i个文件的文件名  
  10.         CString m_FilePath(pFilePath, nChars);   
  11.     }  
  12.     delete[] pFileFilePath;  
  13. /* 
  14.     CString * m_FilePath = new CString[nFileCount]; 
  15.     for (int i=0; i<nFileCount; i++) 
  16.     { 
  17.         UINT nChars = ::DragQueryFile(hDropInfo, i, m_FilePath[i].GetBuffer(256),256);   // 获取拖入的第i个文件的文件名 
  18.         m_FilePath[i].ReleaseBuffer(); 
  19.     } 
  20. */  
  21.     ::DragFinish (hDropInfo);  
  22. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值