MFC实现拖拽文件

本文介绍如何在对话框中轻松实现文件拖放功能。通过添加OnDropFiles处理程序并启用DragAcceptFiles,使得用户能够直接将文件拖放到窗口上进行处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Dragging Files 



Dragging Files can be supported relatively easily as follows 


* Add a OnDropFiles(HDROP hDropInfo) handler to your Dialog. You'll probably have to add this manually, as ClassWizard doesn't seen to support the WM_DROPFILES message for dialogs. 

Make the following changes 

1) In your .h file add the OnDropFiles to the AFX_MSG section 

// Generated message map functions 
//{{AFX_MSG(A2hDialog) 
... 
afx_msg void OnDropFiles(HDROP hDropInfo); 
... 
//}}AFX_MSG 
DECLARE_MESSAGE_MAP() 


2) In your .cpp file add the ON_WM_DROPFILES() handler to the AFX_MSG_MAP section 

BEGIN_MESSAGE_MAP(MyDialog, CDialog) 
//{{AFX_MSG_MAP(MyDialog) 
... 
ON_WM_DROPFILES() 
... 
//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 


Strangely, after doing (1) and (2) the results become visible in the ClassWizard. 

3) 
Use ClassWizard to add an OnCreate() handler to your dialog. in this add the call 

this->DragAcceptFiles(TRUE); 

This enables Drag and drop of files on your Dialog's window. You can check this by dragging files over your Window. If the cursor is a "no entry" sign (circle with a line through it) it's not working. If the cursor changes to a file/folder icon with a "+" on it, you're in business. 

4) Manually add the OnDropFiles method to look something as follows :- 

void MyDialog::OnDropFiles(HDROP hDropInfo) 


HDROP m_hDropInfo = hDropInfo; 
CString Filename; 

if (m_hDropInfo) { 

int iFiles = DragQueryFile(m_hDropInfo, (UINT)-1, NULL, 0); 

for (int i=0; i<ifiles; i++) { 

char* pFilename = Filename.GetBuffer(_MAX_PATH); 
DragQueryFile(m_hDropInfo, i, pFilename, _MAX_PATH); 

// do whatever... 

} // for each files... 
} // if DropInfo 

DragFinish(m_hDropInfo); 

m_hDropInfo = 0; 

} // End of OnDropFiles

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值