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
