1) VC++ 6.0 新建一个基于对话框的MFC的工程,取名MfcDropFiles;
2) 去除默认的控件,包括确定/取消按钮,以及一个静态文本;
3) 在对话框空白区域拖放一个ListBox控件,ID为ID_LIST_FILE,设置属性Accept files;
4)为MfcDropFilesDlg添加消息WM_DROPFILES
afx_msg void OnDropFiles(HDROP hDropInfo); ON_MESSAGE(WM_DROPFILES,OnDropFiles) void CMfcDropFilesDlg::OnDropFiles( HDROP hDropInfo ) { int DropCount = DragQueryFile(hDropInfo, -1, NULL, 0); for(int i=0; i < DropCount; i++) { TCHAR szFullFileName[MAX_PATH]; DragQueryFile(hDropInfo, i, szFullFileName, MAX_PATH); m_ListBox.AddString(szFullFileName); } DragFinish(hDropInfo); CDialog::OnDropFiles(hDropInfo); }
5) 实现窗口可拖放改变大小,控件随窗口大小一起改变
对话框窗口大小可改变:设置对话框属性Styles->Border为Resizing
定义成员变量m_rect记录对话框窗口的大小,自定义函数ChangeSize,重载WM_SIZE消息,具体代码如下:
// MfcDropFilesDlg.h
private: CRect m_rect; void ChangeSize(UINT nID,int cx,int cy); // Generated message map functions //{{AFX_MSG(CMfcDropFilesDlg) ... afx_msg void OnDropFiles(HDROP hDropInfo); afx_msg void OnSize(UINT nType, int cx, int cy); //}}AFX_MSG DECLARE_MESSAGE_MAP()
// MfcDropFilesDlg.cpp
BEGIN_MESSAGE_MAP(CMfcDropFilesDlg, CDialog) //{{AFX_MSG_MAP(CMfcDropFilesDlg) ... ON_MESSAGE(WM_DROPFILES,OnDropFiles) ON_WM_SIZE() //}}AFX_MSG_MAP END_MESSAGE_MAP() void CMfcDropFilesDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); if(SIZE_MINIMIZED != nType) { ChangeSize(IDC_LIST_FILE, cx, cy); GetClientRect(&m_rect); } } void CMfcDropFilesDlg::ChangeSize( UINT nID,int cx,int cy ) { CRect rect; CWnd* pWnd = GetDlgItem(nID); if (!pWnd) { return; } pWnd->GetWindowRect(&rect); // 获取控件的区域大小 ScreenToClient(&rect); // 将控件大小转换为在对话框中的区域坐标 // 调整控件大小 rect.left = rect.left * cx/ m_rect.Width(); rect.right = rect.right * cx / m_rect.Width(); rect.top = rect.top * cy/ m_rect.Height(); rect.bottom = rect.bottom * cy / m_rect.Height(); // 执行控件大小调整 pWnd->MoveWindow(rect); }
6)运行结果
/*****************************************************************************************************************************/
Syntax
UINT DragQueryFile( HDROP hDrop,
UINT iFile,
LPTSTR lpszFile,
UINT cch
);
Parameters
hDrop
Identifier of the structure containing the file names of the dropped files.
iFile
Index of the file to query. If the value of the iFile parameter is 0xFFFFFFFF, DragQueryFile returns a count of the files dropped.
If the value of the iFile parameter is between zero and the total number of files dropped, DragQueryFile copies the file name with the corresponding value to the buffer pointed to by the lpszFile parameter.
lpszFile
The address of a buffer to receive the file name of a dropped file when the function returns. This file name is a null-terminated string. If this parameter is NULL, DragQueryFile returns the required size, in characters, of the buffer.
cch
Size, in characters, of the lpszFile buffer.
Return Value
When the function copies a file name to the buffer, the return value is a count of the characters copied, not including the terminating null character.
If the index value is 0xFFFFFFFF, the return value is a count of the dropped files. Note that the index variable itself returns unchanged, and will therefore remain 0xFFFFFFFF.
If the index value is between zero and the total number of dropped files and the lpszFile buffer address is NULL, the return value is the required size, in characters, of the buffer, not including the terminating null character.
/*****************************************************************************************************************************/
代码下载:(仅供参考)
百度云:http://pan.baidu.com/s/1bpMlgvH 密码:g0fu