Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
UnityDragAndDropHook.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Text;
4using AOT;
5
6namespace B83.Win32;
7
8public static class UnityDragAndDropHook
9{
10 public delegate void DroppedFilesEvent(List<string> aPathNames, POINT aDropPoint);
11
12 private static uint threadId;
13
14 private static IntPtr mainWindow = IntPtr.Zero;
15
16 private static IntPtr m_Hook;
17
18 private static string m_ClassName = "UnityWndClass";
19
20 public static event DroppedFilesEvent OnDroppedFiles;
21
22 [MonoPInvokeCallback(typeof(EnumThreadDelegate))]
23 private static bool EnumCallback(IntPtr W, IntPtr _)
24 {
25 if (Window.IsWindowVisible(W) && (mainWindow == IntPtr.Zero || (m_ClassName != null && Window.GetClassName(W) == m_ClassName)))
26 {
27 mainWindow = W;
28 }
29 return true;
30 }
31
32 public static void InstallHook()
33 {
35 if (threadId != 0)
36 {
38 }
39 IntPtr moduleHandle = WinAPI.GetModuleHandle(null);
40 m_Hook = WinAPI.SetWindowsHookEx(HookType.WH_GETMESSAGE, Callback, moduleHandle, threadId);
41 WinAPI.DragAcceptFiles(mainWindow, fAccept: true);
42 }
43
44 public static void UninstallHook()
45 {
47 WinAPI.DragAcceptFiles(mainWindow, fAccept: false);
48 m_Hook = IntPtr.Zero;
49 }
50
51 [MonoPInvokeCallback(typeof(HookProc))]
52 private static IntPtr Callback(int code, IntPtr wParam, ref MSG lParam)
53 {
54 if (code == 0 && lParam.message == WM.DROPFILES)
55 {
56 WinAPI.DragQueryPoint(lParam.wParam, out var pos);
57 uint num = WinAPI.DragQueryFile(lParam.wParam, uint.MaxValue, null, 0u);
58 StringBuilder stringBuilder = new StringBuilder(1024);
59 List<string> list = new List<string>();
60 for (uint num2 = 0u; num2 < num; num2++)
61 {
62 int num3 = (int)WinAPI.DragQueryFile(lParam.wParam, num2, stringBuilder, 1024u);
63 list.Add(stringBuilder.ToString(0, num3));
64 stringBuilder.Length = 0;
65 }
66 WinAPI.DragFinish(lParam.wParam);
68 {
70 }
71 }
72 return WinAPI.CallNextHookEx(m_Hook, code, wParam, ref lParam);
73 }
74}
static DroppedFilesEvent OnDroppedFiles
static IntPtr Callback(int code, IntPtr wParam, ref MSG lParam)
static bool EnumCallback(IntPtr W, IntPtr _)
delegate void DroppedFilesEvent(List< string > aPathNames, POINT aDropPoint)
static IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId)
static uint DragQueryFile(IntPtr hDrop, uint iFile, StringBuilder lpszFile, uint cch)
static void DragQueryPoint(IntPtr hDrop, out POINT pos)
static uint GetCurrentThreadId()
static void DragAcceptFiles(IntPtr hwnd, bool fAccept)
static IntPtr GetModuleHandle(string lpModuleName)
static bool UnhookWindowsHookEx(IntPtr hhk)
static void DragFinish(IntPtr hDrop)
static IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, ref MSG lParam)
static int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount)
static bool IsWindowVisible(IntPtr hWnd)
static bool EnumThreadWindows(uint dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam)
WM
Definition: WM.cs:6
delegate IntPtr HookProc(int code, IntPtr wParam, ref MSG lParam)
delegate bool EnumThreadDelegate(IntPtr Hwnd, IntPtr lParam)