gpt4 book ai didi

c++ - 将 WPARAM 传递到 DragQueryFile 不兼容?

转载 作者:可可西里 更新时间:2023-11-01 10:32:56 25 4
gpt4 key购买 nike

我有点困惑。当一个文件被拖到一个带有 WS_EX_ACCEPTFILES 标记的窗口上时,它会将一个 PostMessage 放入 WndProc 函数中,该函数将 UINT 消息设置为 WM_DROPFILES,并且根据

https://msdn.microsoft.com/en-us/library/windows/desktop/bb774303(v=vs.85).aspx

WPARAM = (WPARAM) (HDROP) hDrop;所以我是否错误地假设我可以使用 WPARAM 来初始化 HDROP 或只是将它传递到 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,然后迭代提供缓冲区的丢弃文件路径。

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 interfaceregister it as drop target handler为您的应用。

关于c++ - 将 WPARAM 传递到 DragQueryFile 不兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43823771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com