如何从 DROP FILES 结构中提取拖放文件名
How do i extract dragged & dropped filenames from a DROPFILES struct
我有一个 window,没有启用 WS_EX_ACCEPTFILES 的控件,当从资源管理器中拖动文件时事件成功触发。我需要做的是将文件提取到矢量中。根据我的阅读,wParam 应该包含文件在 DROPFILES 结构中的位置,但我不知道如何访问它们。
LRESULT CALLBACK StaticWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR
uIdSubclass, DWORD_PTR dwRefData)
{
if (uMsg == WM_DROPFILES)
{
// extract files here
vector<string> files;
}
return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}
我不需要将它们发送到任何控件,因为我的 window 用于 openGL 应用程序,我只需要将它们保留在列表中,如何实现?
根据 WM_DROPFILES
documentation:
Parameters
hDrop
A handle to an internal structure describing the dropped files. Pass this handle to DragFinish
, DragQueryFile
, or DragQueryPoint()
to retrieve information about the dropped files.
lParam
Must be zero.
只需将 wParam
类型转换为 HDROP
并根据需要调用拖放功能,例如:
LRESULT CALLBACK StaticWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
if (uMsg == WM_DROPFILES)
{
HDROP hDrop = reinterpret_cast<HDROP>(wParam);
// extract files here
vector<string> files;
char filename[MAX_PATH];
UINT count = DragQueryFileA(hDrop, -1, NULL, 0);
for(UINT i = 0; i < count; ++i)
{
if (DragQueryFileA(hDrop, i, filename, MAX_PATH))
files.push_back(filename);
}
DragFinish(hDrop);
return 0;
}
return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}
或者:
LRESULT CALLBACK StaticWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
if (uMsg == WM_DROPFILES)
{
HDROP hDrop = reinterpret_cast<HDROP>(wParam);
// extract files here
vector<string> files;
string filename;
UINT count = DragQueryFileA(hDrop, -1, NULL, 0);
for(UINT i = 0; i < count; ++i)
{
UINT size = DragQueryFileA(hDrop, i, NULL, 0);
if (size > 0)
{
filename.resize(size);
DragQueryFileA(hDrop, i, &filename[0], size+1);
files.push_back(filename);
}
}
DragFinish(hDrop);
return 0;
}
return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}
我有一个 window,没有启用 WS_EX_ACCEPTFILES 的控件,当从资源管理器中拖动文件时事件成功触发。我需要做的是将文件提取到矢量中。根据我的阅读,wParam 应该包含文件在 DROPFILES 结构中的位置,但我不知道如何访问它们。
LRESULT CALLBACK StaticWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR
uIdSubclass, DWORD_PTR dwRefData)
{
if (uMsg == WM_DROPFILES)
{
// extract files here
vector<string> files;
}
return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}
我不需要将它们发送到任何控件,因为我的 window 用于 openGL 应用程序,我只需要将它们保留在列表中,如何实现?
根据 WM_DROPFILES
documentation:
Parameters
hDrop
A handle to an internal structure describing the dropped files. Pass this handle to
DragFinish
,DragQueryFile
, orDragQueryPoint()
to retrieve information about the dropped files.lParam
Must be zero.
只需将 wParam
类型转换为 HDROP
并根据需要调用拖放功能,例如:
LRESULT CALLBACK StaticWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
if (uMsg == WM_DROPFILES)
{
HDROP hDrop = reinterpret_cast<HDROP>(wParam);
// extract files here
vector<string> files;
char filename[MAX_PATH];
UINT count = DragQueryFileA(hDrop, -1, NULL, 0);
for(UINT i = 0; i < count; ++i)
{
if (DragQueryFileA(hDrop, i, filename, MAX_PATH))
files.push_back(filename);
}
DragFinish(hDrop);
return 0;
}
return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}
或者:
LRESULT CALLBACK StaticWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
if (uMsg == WM_DROPFILES)
{
HDROP hDrop = reinterpret_cast<HDROP>(wParam);
// extract files here
vector<string> files;
string filename;
UINT count = DragQueryFileA(hDrop, -1, NULL, 0);
for(UINT i = 0; i < count; ++i)
{
UINT size = DragQueryFileA(hDrop, i, NULL, 0);
if (size > 0)
{
filename.resize(size);
DragQueryFileA(hDrop, i, &filename[0], size+1);
files.push_back(filename);
}
}
DragFinish(hDrop);
return 0;
}
return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}