zntg/addons/drop/drop_windows.c

64 lines
1.5 KiB
C
Raw Permalink Normal View History

2021-06-01 19:14:51 +00:00
#include <Windows.h>
#include <shellapi.h>
#include <stdint.h>
#include "_cgo_export.h"
#define droppedFilePathSize 32767
BOOL dropHookInitialized = FALSE;
2021-06-01 19:14:51 +00:00
HHOOK nextHook;
LRESULT CALLBACK DragAndDropHook(
_In_ int nCode,
_In_ WPARAM wParam,
_In_ LPARAM lParam)
{
if (nCode < 0 || wParam == 0)
return CallNextHookEx(nextHook, nCode, wParam, lParam);
LPMSG message = (LPMSG)lParam;
switch (message->message)
{
case WM_DROPFILES:
{
wchar_t droppedFilePath[droppedFilePathSize];
clearDrop();
2021-06-01 19:14:51 +00:00
HDROP drop = (HDROP)message->wParam;
UINT numberOfFiles = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
for (UINT fileIndex = 0; fileIndex < numberOfFiles; fileIndex++)
{
UINT length = DragQueryFileW(drop, fileIndex, droppedFilePath, droppedFilePathSize);
addDroppedFile(&droppedFilePath[0], length);
2021-06-01 19:14:51 +00:00
}
POINT position;
DragQueryPoint(drop, &position);
dropFinished(position.x, position.y);
2021-06-01 19:14:51 +00:00
}
break;
}
return CallNextHookEx(nextHook, nCode, wParam, lParam);
}
void initDropHook(HWND windowHandle)
{
if (dropHookInitialized == TRUE)
return;
DWORD threadId = GetWindowThreadProcessId(windowHandle, NULL);
nextHook = SetWindowsHookEx(WH_GETMESSAGE, DragAndDropHook, NULL, threadId);
dropHookInitialized = TRUE;
}
2021-06-01 19:14:51 +00:00
void SetDragAndDropHook(void *window)
{
HWND windowHandle = (HWND)window;
initDropHook(windowHandle);
2021-06-01 19:14:51 +00:00
DragAcceptFiles(windowHandle, TRUE);
}