Added Windows D&D support
authorVlad K <vperus@gmail.com>
Tue, 9 May 2017 02:23:22 +0000 (05:23 +0300)
committerVlad K <vperus@gmail.com>
Fri, 16 Jun 2017 19:08:24 +0000 (22:08 +0300)
src/OpenTK/Platform/Windows/API.cs
src/OpenTK/Platform/Windows/WinGLNative.cs

index c2580c7..c689f88 100644 (file)
@@ -57,6 +57,8 @@ namespace OpenTK.Platform.Windows
     using HKEY = System.IntPtr;
     using PHKEY = System.IntPtr;
 
+    using HDROP = System.IntPtr;
+
     using LRESULT = System.IntPtr;
     using LPVOID = System.IntPtr;
     using LPCTSTR = System.String;
@@ -134,6 +136,25 @@ namespace OpenTK.Platform.Windows
     {
         #region Window functions
 
+        [DllImport("shell32.dll")]
+        internal static extern bool DragAcceptFiles(
+            IntPtr handle,
+            [MarshalAs(UnmanagedType.Bool)] bool fAccept
+        );
+
+        [DllImport("shell32.dll")]
+        internal static extern uint DragQueryFile(
+            HDROP hDrop,
+            uint iFile,
+            IntPtr lpszFile,
+            uint cch
+        );
+
+        [DllImport("shell32.dll")]
+        internal static extern void DragFinish(
+            HDROP hDrop
+        );
+
         #region SetWindowPos
 
         // WINUSERAPI BOOL WINAPI SetWindowPos(__in HWND hWnd, __in_opt HWND hWndInsertAfter,
index 234c68a..112f528 100644 (file)
@@ -150,6 +150,7 @@ namespace OpenTK.Platform.Windows
                         0, 0, ClientSize.Width, ClientSize.Height,
                         title, options, device, window.Handle),
                     window);
+                Functions.DragAcceptFiles(window.Handle, true);
 
                 exists = true;
             }
@@ -680,6 +681,29 @@ namespace OpenTK.Platform.Windows
             OnClosed(EventArgs.Empty);
         }
 
+        void HandleDropFiles(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
+        {
+            IntPtr hDrop = wParam;
+            uint filesCounter = Functions.DragQueryFile(hDrop, 0xFFFFFFFF, IntPtr.Zero, 0);
+            for (uint i = 0; i < filesCounter; ++i)
+            {
+                // Don't forget about \0 at the end
+                uint fileNameSize = Functions.DragQueryFile(hDrop, i, IntPtr.Zero, 0) + 1;
+                byte [] byteArray = new byte [fileNameSize];
+                IntPtr str = Marshal.AllocHGlobal((int)fileNameSize);
+
+                Functions.DragQueryFile(hDrop, i, str, fileNameSize);
+                
+                Marshal.Copy(str, byteArray, 0, (int)(fileNameSize - 1));
+                string dropString = System.Text.Encoding.UTF8.GetString(byteArray);
+                OnDrop(dropString);
+
+                Marshal.FreeHGlobal(str);
+            }
+
+            Functions.DragFinish(hDrop);
+        }
+
         #endregion
 
         #region WindowProcedure
@@ -754,6 +778,7 @@ namespace OpenTK.Platform.Windows
                     return IntPtr.Zero;
 
                 case WindowMessage.LBUTTONDOWN:
+                    Console.WriteLine("ola");
                     HandleLButtonDown(handle, message, wParam, lParam);
                     return IntPtr.Zero;
 
@@ -800,6 +825,10 @@ namespace OpenTK.Platform.Windows
                     HandleKillFocus(handle, message, wParam, lParam);
                     break;
 
+                case WindowMessage.DROPFILES:
+                    HandleDropFiles(handle, message, wParam, lParam);
+                    break;
+
                 #endregion
 
                 #region Creation / Destruction events