Added Windows D&D support

This commit is contained in:
Vlad K 2017-05-09 05:23:22 +03:00
parent 18dd603696
commit 90e9c28cbd
2 changed files with 50 additions and 0 deletions

View file

@ -57,6 +57,8 @@ namespace OpenTK.Platform.Windows
using HKEY = System.IntPtr; using HKEY = System.IntPtr;
using PHKEY = System.IntPtr; using PHKEY = System.IntPtr;
using HDROP = System.IntPtr;
using LRESULT = System.IntPtr; using LRESULT = System.IntPtr;
using LPVOID = System.IntPtr; using LPVOID = System.IntPtr;
using LPCTSTR = System.String; using LPCTSTR = System.String;
@ -134,6 +136,25 @@ namespace OpenTK.Platform.Windows
{ {
#region Window functions #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 #region SetWindowPos
// WINUSERAPI BOOL WINAPI SetWindowPos(__in HWND hWnd, __in_opt HWND hWndInsertAfter, // WINUSERAPI BOOL WINAPI SetWindowPos(__in HWND hWnd, __in_opt HWND hWndInsertAfter,

View file

@ -150,6 +150,7 @@ namespace OpenTK.Platform.Windows
0, 0, ClientSize.Width, ClientSize.Height, 0, 0, ClientSize.Width, ClientSize.Height,
title, options, device, window.Handle), title, options, device, window.Handle),
window); window);
Functions.DragAcceptFiles(window.Handle, true);
exists = true; exists = true;
} }
@ -680,6 +681,29 @@ namespace OpenTK.Platform.Windows
OnClosed(EventArgs.Empty); 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 #endregion
#region WindowProcedure #region WindowProcedure
@ -754,6 +778,7 @@ namespace OpenTK.Platform.Windows
return IntPtr.Zero; return IntPtr.Zero;
case WindowMessage.LBUTTONDOWN: case WindowMessage.LBUTTONDOWN:
Console.WriteLine("ola");
HandleLButtonDown(handle, message, wParam, lParam); HandleLButtonDown(handle, message, wParam, lParam);
return IntPtr.Zero; return IntPtr.Zero;
@ -800,6 +825,10 @@ namespace OpenTK.Platform.Windows
HandleKillFocus(handle, message, wParam, lParam); HandleKillFocus(handle, message, wParam, lParam);
break; break;
case WindowMessage.DROPFILES:
HandleDropFiles(handle, message, wParam, lParam);
break;
#endregion #endregion
#region Creation / Destruction events #region Creation / Destruction events