zntg/addons/drop/drop_windows.c

53 lines
1.4 KiB
C
Raw 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
HHOOK nextHook;
uint32_t nextDropID = 0;
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];
uint32_t dropID = nextDropID++;
HDROP drop = (HDROP)message->wParam;
POINT position;
DragQueryPoint(drop, &position);
droppedFilesPosition(dropID, position.x, position.y);
UINT numberOfFiles = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
for (UINT fileIndex = 0; fileIndex < numberOfFiles; fileIndex++)
{
UINT length = DragQueryFileW(drop, fileIndex, droppedFilePath, droppedFilePathSize);
droppedFilesFile(dropID, &droppedFilePath[0], length);
}
droppedFilesDrop(dropID);
}
break;
}
return CallNextHookEx(nextHook, nCode, wParam, lParam);
}
void SetDragAndDropHook(void *window)
{
HWND windowHandle = (HWND)window;
DragAcceptFiles(windowHandle, TRUE);
DWORD threadId = GetWindowThreadProcessId(windowHandle, NULL);
nextHook = SetWindowsHookEx(WH_GETMESSAGE, DragAndDropHook, NULL, threadId);
}