DragQueryFile

DragQueryFile是一個成功拖放操作後獲取被拖放檔案的名稱等信息的函式。

基本介紹

  • 中文名:DragQueryFile
  • 區分:包含被拖拽檔案名稱稱結構的句柄
  • 獲取:被拖放檔案的名稱
  • 作用:用於指明所要查詢檔案的序號
DragQueryFile 函式,Syntax 語法,Parameters 參數,Return Value 返回值,相關拓展,

DragQueryFile 函式

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

Syntax 語法

UINT DragQueryFile(
HDROPhDrop,
UINTiFile,
LPTSTRlpszFile,
UINTcch
);

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 thelpszFileparameter.
檔案索引編號(用於指明所要查詢檔案的序號, 如果拖進多個檔案,則索引編號從零開始),如果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字元的字元數。
Windows 95/98/Me:DragQueryFileis supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.
Function Information
Minimum DLL Version
shell32.dllversion 4.0or later
Custom Implementation
No
Header
shellapi.h
Import library
shell32.lib
Minimum operating systems
Windows NT 3.1, Windows 95
Unicode
Implemented as ANSI and Unicode versions.

相關拓展

VC實現檔案管理器拖拽(Drag-and-Drop)
在日常的程式中,為了操作的方便,經常需要使用滑鼠拖拽的方式把檔案管理器中的檔案拖拽到我們自己寫的程式中,以下就簡單介紹以下實現該操作的方法。
其實檔案管理器的拖拽方式實現起來很簡單,主要通過幾個函式來實現,訊息WM_DROPFILES的回響函式OnDropFiles,還有三個API函式:DragQueryFile,DragQueryPoint和DragFinish。 在啟動拖拽動作時,作業系統會分配一塊記憶體存儲拖拽的檔案的信息,並通過一個HDROP類型的句柄把該塊記憶體的地址傳遞給函式OnDropFiles函式。
檔案管理器拖拽方式的實質就是處理WM_DROPFILES訊息,要讓視窗或控制項回響該訊息,需要先設定Accept Files屬性,可以通過在視窗或控制項的屬性頁中勾選Accept Files,也可以通過調用CWnd類的成員函式DragAcceptFiles(TRUE)或API函式DragAcceptFiles(HWND hWnd,BOOL fAccept),可以在OnCreate或OnInitDialog之類的函式中調用,它們的效果都是一樣的。
設定了Accept Files屬性後,就可以添加WM_DROPFILES訊息的回響函式OnDropFiles了,ClassWizard不支持該函式,所以需要手動添加此函式,然後在該函式中調用上述的三個API函式,其代碼如下:
void DragDemo::OnDropFiles(HDROP hDrop)
{
// 定義一個緩衝區來存放讀取的檔案名稱信息
char szFileName[MAX_PATH + 1] = {0};
// 通過設定iFiles參數為0xFFFFFFFF,可以取得當前拖動的檔案數量,
// 當設定為0xFFFFFFFF,函式間忽略後面兩個參數。
UINT nFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
// 通過循環依次取得拖動檔案的File Name信息,並把它添加到ListBox中
for(UINT i=0; i<nFiles; i++)
{
DragQueryFile(hDrop, i, szFileName, MAX_PATH);
m_listCtrl.AddString(szFileName);
}
// 結束此次拖拽操作,並釋放分配的資源
DragFinish(hDrop);
}
其中三個API函式的具體用法可以參照MSDN。由於本例的操作是直接拖拽的對話框上,所以不需要通過DragQueryPoint來取得滑鼠鬆開時的位置,在實際的實現中,最好是為控制項派生出一個類,在該類中定義訊息回響函式OnDropFiles,這樣就不需要查詢滑鼠的位置,也不需要設定滑鼠的指針樣式。

相關詞條

熱門詞條

聯絡我們