windows shell api封装好接口了。
1 开启drag file支持
调用 DragAcceptFiles(hwnd, TRUE),表示要支持drag file。这样系统才会给你发消息。
2 拖拽文件到窗口,将收到 WM_DROPFILES 消息。
3 获取文件路径
在WM_DROPFILES中, WPARAM是一个 HDROP 指针。
调用DragQueryFile()获取文件路径
UINT DragQueryFile(
[in] HDROP hDrop,
[in] UINT iFile,
[out] LPSTR lpszFile,
UINT cch
);
4 释放HDROP
调用 DragFinish (HDROP) 释放内存
diff --git a/win_main.c b/win_main.c
index 8607cc8..7c0445a 100644
--- a/win_main.c
+++ b/win_main.c
@@ -104,6 +104,23 @@ LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
case WM_DEVICECHANGE:
monitor_device(wParam, lParam);
return 0;
+ case WM_DROPFILES:
+ {
+ HDROP hdl = (HDROP)wParam;
+ char file[260];
+ int cnt;
+ int i;
+
+ cnt = DragQueryFile(hdl, -1, NULL, 0);
+
+ for (i = 0; i < cnt; i++) {
+ DragQueryFile(hdl, i, file, sizeof(file));
+ dbg("drag %d: %s\n", i, file);
+ }
+
+ DragFinish(hdl);
+ }
+ return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
@@ -158,6 +175,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
+ DragAcceptFiles(hwnd, TRUE);
+
while (1) {
status = GetMessage(&msg, NULL, 0, 0);
if (status == 0)