MFC編程之添加拖動文件獲取文件路徑功能

轉載請註明來源 : http://blog.csdn.net/enjoy5512

通過前一篇的學習,我們知道了怎麼獲取一個文件選擇對話框,但是如果文件在桌面或者同目錄下的時候,通過一級一級找文件目錄也挺麻煩的,於是我便想着在原來的基礎上添加文件拖動到對話框,直接獲取文件路徑的方法


1 . 還是在上一個工程的基礎上,做一點點修改就可以了.首先在對話框上右鍵->屬性->擴展樣式->接受文件
這裏寫圖片描述
然後在對話框上右鍵建立類嚮導
這裏寫圖片描述


2 .然後在類嚮導裏選擇Class Info選項,在Message Filter選項中選擇Windows
這裏寫圖片描述


3 . 然後選擇Meesage Maps選項,在Message裏面找到WM_DROPFILES消息
這裏寫圖片描述


4 . 雙擊WM_DROPFILES,添加WM_DROPFILES的消息響應函數
這裏寫圖片描述


5 . 確定後,回到desDlg.cpp,在文件最後可以看到剛剛添加的WM_DROPFILES消息響應函數
這裏寫圖片描述


6 . 在WM_DROPFILES的消息響應函數裏添加文件拖動到對話框上時的消息響應函數代碼

void CDesDlg::OnDropFiles(HDROP hDropInfo) 
{
    // TODO: Add your message handler code here and/or call default
    char szFilePath[200] = {0};                               //保存文件路徑

    DragQueryFile(hDropInfo,0,szFilePath,sizeof(szFilePath)); //獲取文件路徑
    SetDlgItemText(IDC_EDIT_PATH,szFilePath);                 //更新EDIT框
    DragFinish(hDropInfo);                                    //拖放結束後,釋放內存 

    CDialog::OnDropFiles(hDropInfo);
}

結果截圖:
這裏寫圖片描述


附關鍵函數解析(摘自百度百科):
DragQueryFile是一個成功拖放操作後獲取被拖放文件的名稱等信息的函數。


DragQueryFile 函數
Retrieves the names of dropped files that result from a successful drag-and-drop operation.


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 theiFileparameter is 0xFFFFFFFF,DragQueryFilereturns a count of the files dropped. If the value of theiFileparameter is between zero and the total number of files dropped,DragQueryFilecopies the file name with the corresponding value to the buffer pointed to by the lpszFileparameter.
文件索引編號(用於指明所要查詢文件的序號, 如果拖進多個文件,則索引編號從零開始),如果iFile值爲 0xFFFFFFFF 時,返回的是拖曳到窗體上的文件的個數。如果iFile值在0和拖拽文件總數之間時,DragQueryFile拷貝與文件名存儲緩衝區大小適應的文件名稱到緩衝區中。

lpszFile
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,DragQueryFilereturns the required size, in characters, of the buffer.
函數返回時,用於存儲拖拽文件名稱的緩衝區指針。文件名稱是一個以空終止“\0”結尾的字符串。如果此參數是NULL,DragQueryFile函數返回拖拽的文件名的長度。函數DragQueryFile得到的文件名,是帶完整路徑的文件名。

cch
Size, in characters, of thelpszFilebuffer.
存儲拖拽文件名稱緩衝區的大小,即lpszFile指針所指緩衝區的字符數。


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.
如果函數拷貝文件名稱到緩衝區中,返回值就是拷貝的字符數,不包括終止的NULL字符。
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.
如果文件索引值是0xFFFFFFFF,則返回值是被拖拽的文件總數,注意文件索引變量的值將保持不變,依然爲0xFFFFFFFF。
If the index value is between zero and the total number of dropped files and thelpszFilebuffer address is NULL, the return value is the required size, in characters, of the buffer, not including the terminating null character.
如果文件索引值在0和拖拽文件總數之間時,並且lpszFile值爲NULL時,返回值是存儲此被拖拽文件的名稱所需要的緩衝區大小值,此值是不包括終止NULL字符的字符數。

發佈了42 篇原創文章 · 獲贊 37 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章