MFC implementation dialog to edit control drag and drop files
- 2020-09-16 07:43:01
- OfStack
This article example for you to share MFC implementation dialog box edit control drag and drop file specific code, for your reference, specific content is as follows
steps
1. Overload the CEdit class
2. Add edit box control
First, override the CEdit class, define a derived CDragEdit class, and override its WM_CREATE method to add DragAcceptFile(TRUE). Methods.
// CDragEdit.h
#pragma once
class CDragEdit : public CEdit
{
DECLARE_DYNAMIC(CDragEdit)
public:
CDragEdit();
virtual ~CDragEdit();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDropFiles(HDROP hDropInfo);
};
Then, use the class wizard to add the corresponding function of WM_DROPFILE message to the CDragEdit class.
//CDragEdit.cpp
int CDragEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEdit::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your own creation code here
DragAcceptFiles(TRUE);
return 0;
}
void CDragEdit::OnDropFiles(HDROP hDropInfo)
{
UINT count;
TCHAR filePath[MAX_PATH] = { 0 };
count = DragQueryFile(hDropInfo, -1, NULL, 0);
if (1 == count)
{
DragQueryFile(hDropInfo, 0, filePath, sizeof(filePath));
this->SetWindowTextW(filePath);
UpdateData(FALSE);
DragFinish(hDropInfo); // After successful drag and drop, free memory
}
else
{
CString szFilePath;
for (UINT i = 0; i < count; i++)
{
int pahtLen = DragQueryFile(hDropInfo, i, filePath, sizeof(filePath));
szFilePath = szFilePath + filePath + _T("\r\n");
}
this->SetWindowTextW(szFilePath);
UpdateData(FALSE);
DragFinish(hDropInfo);
}
//MFCDlg.h
#program once
#include "CDragEdit.h"
class CMFDlg : public CDialogEx
{
..........................................
public:
CDragEdit m_DragEdit;
}
Finally, set the textbox control property [Accept Files] and the value [multiline] to True.