将 WPARAM 传递给 DragQueryFile 不兼容? [英] Passing WPARAM into DragQueryFile not compatible?
问题描述
我有点困惑.当一个文件被拖到一个带有 WS_EX_ACCEPTFILES 标志的窗口上时,它会将 PostMessage 放入 WndProc 函数中,该函数将 UINT 消息设置为 WM_DROPFILES,并且根据
I'm kinda confused. When a file is dragged onto a window with WS_EX_ACCEPTFILES flagged it places a PostMessage into the WndProc function, which sets UINT message to WM_DROPFILES and, according to
https://msdn.microsoft.com/en-us/library/windows/desktop/bb774303(v=vs.85).aspx
WPARAM = (WPARAM) (HDROP) hDrop;所以我假设我可以使用 WPARAM 来初始化 HDROP 或者只是将它传递给 DragQueryFile 是错误的吗??
the WPARAM = (WPARAM) (HDROP) hDrop; So am I wrong to assume that I can use the WPARAM to initialize the HDROP or just pass it into the DragQueryFile ??
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
return 0;
case WM_DROPFILES:
TCHAR* FilePath;
HDROP hDrop = wParam; //wParam cannot be used to ini. an entity of type HDROP
//HDROP hdrop = (HDROP)wParam; initialization of hDrop is skipped by case label
DragQueryFile(wParam, 0, FilePath, 0); //wParam not compatible
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
感谢每一种帮助.
推荐答案
您需要将 wparam 转换为 HDROP,然后迭代提供缓冲区的已删除文件路径.
You need to cast wparam to HDROP and then iterate over dropped file paths supplying buffer.
case WM_DROPFILES:
{
auto const drop_handle{reinterpret_cast< ::HDROP >(wParam)};
auto const dropped_files_count
{
::DragQueryFileW(drop_handle, 0xFFFFFFFF, nullptr, 0)
};
::std::vector< wchar_t > buffer;
for(::UINT dropped_file_index{0}; dropped_files_count != dropped_file_index; ++dropped_file_index)
{
auto const file_path_symbols_count_excluding_terminating_null
{
::DragQueryFileW(drop_handle, dropped_file_index, nullptr, 0)
};
if(0 < file_path_symbols_count_excluding_terminating_null)
{
auto const buffer_size{file_path_symbols_count_excluding_terminating_null + 1};
buffer.resize(buffer_size);
auto const copied_symbols_count_excluding_terminating_null
{
::DragQueryFileW(drop_handle, dropped_file_index, buffer.data(), buffer_size)
};
if(copied_symbols_count_excluding_terminating_null == file_path_symbols_count_excluding_terminating_null)
{
buffer.back() = L'\0'; // just in case....
// buffer now contains file path...
}
}
}
break;
}
但是请注意,即使处理 WM_DROPFILES
应该可以工作,处理拖放的首选方法是实现 IDropTarget
接口 和 将其注册为应用程序的放置目标处理程序.
However note that even though handling WM_DROPFILES
should work, the preffered way to handle drag-and drop is to implement IDropTarget
interface and register it as drop target handler for your application.
这篇关于将 WPARAM 传递给 DragQueryFile 不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!