WM_DROPFILES : 当用户将文件拖放到已注册为丢弃文件的接收者的应用程序窗口中时发送该消息
我们可以利用这个消息获取文件名称,并将它们保存到容器里。
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); }