MFC实现拖拽文件



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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 MFC实现文件拖拽发送,可以按照以下步骤进行: 1. 在 MFC 对话框的资源视图中添加一个编辑框控件,用于显示拖拽文件路径。 2. 双击对话框类的头文件 (.h),在类定义中添加以下成员变量和方法: ```cpp CString m_strFilePath; // 保存拖拽文件路径 afx_msg void OnDropFiles(HDROP hDropInfo); // 拖拽文件的消息处理函数 ``` 3. 在对话框类的源文件 (.cpp) 中添加以下代码: ```cpp BEGIN_MESSAGE_MAP(CYourDialog, CDialogEx) ON_WM_DROPFILES() END_MESSAGE_MAP() CYourDialog::CYourDialog(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_YOUR_DIALOG, pParent) { // ... } void CYourDialog::OnDropFiles(HDROP hDropInfo) { // 获取拖拽文件数目 UINT nFiles = ::DragQueryFile(hDropInfo, 0xFFFFFFFF, nullptr, 0); // 获取第一个文件的路径长度 UINT nPathLen = ::DragQueryFile(hDropInfo, 0, nullptr, 0) + 1; // 获取第一个文件的路径 LPWSTR lpwstrFilePath = new WCHAR[nPathLen]; ::DragQueryFile(hDropInfo, 0, lpwstrFilePath, nPathLen); // 将文件路径保存到成员变量中 m_strFilePath = lpwstrFilePath; // 更新编辑框中的文本显示 GetDlgItem(IDC_EDIT_FILEPATH)->SetWindowTextW(m_strFilePath); delete[] lpwstrFilePath; // 调用基类的消息处理函数 CDialogEx::OnDropFiles(hDropInfo); } ``` 4. 在对话框的 OnInitDialog() 函数中添加以下代码,启用文件拖拽功能: ```cpp BOOL CYourDialog::OnInitDialog() { CDialogEx::OnInitDialog(); // 启用文件拖拽功能 DragAcceptFiles(TRUE); // ... return TRUE; } ``` 这样,当用户将文件拖拽到对话框上时,文件路径会显示在编辑框中。你可以进一步自定义发送文件的逻辑,例如在拖拽结束后将文件发送到指定的目标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值