From 8f9311ec8d9e69f2d271111a4da6f97d260c407f Mon Sep 17 00:00:00 2001 From: Fraser Date: Fri, 21 Feb 2014 00:24:24 +0000 Subject: [PATCH] Partial implementation of Windows MouseCursor. Very buggy, but starting to show results. --- Source/OpenTK/Platform/Windows/WinGLNative.cs | 52 ++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/Source/OpenTK/Platform/Windows/WinGLNative.cs b/Source/OpenTK/Platform/Windows/WinGLNative.cs index 94ca0c6..6fc532d 100644 --- a/Source/OpenTK/Platform/Windows/WinGLNative.cs +++ b/Source/OpenTK/Platform/Windows/WinGLNative.cs @@ -102,6 +102,7 @@ namespace OpenTK.Platform.Windows KeyPressEventArgs key_press = new KeyPressEventArgs((char)0); MouseCursor cursor = MouseCursor.Default; + IntPtr curson_handle = IntPtr.Zero; int cursor_visible_count = 0; static readonly object SyncRoot = new object(); @@ -485,6 +486,11 @@ namespace OpenTK.Platform.Windows mouse_last_timestamp = timestamp; } + if (cursor != MouseCursor.Default) + { + Functions.SetCursor(curson_handle); + } + if (mouse_outside_window) { // Once we receive a mouse move event, it means that the mouse has @@ -1176,7 +1182,51 @@ namespace OpenTK.Platform.Windows } set { - Debug.Print("[Warning] WinGLNative.Cursor property not implemented"); + if (value != cursor) + { + var stride = value.Width * + (Bitmap.GetPixelFormatSize(System.Drawing.Imaging.PixelFormat.Format32bppArgb) / 8); + + Bitmap bmp; + unsafe + { + fixed (byte* pixels = value.Rgba) + { + bmp = new Bitmap(value.Width, value.Height, stride, + System.Drawing.Imaging.PixelFormat.Format32bppArgb, + new IntPtr(pixels)); + } + } + + var iconInfo = new IconInfo(); + var bmpIcon = bmp.GetHicon(); + var success = Functions.GetIconInfo(bmpIcon, out iconInfo); + + if (success) + { + iconInfo.xHotspot = value.X; + iconInfo.yHotspot = value.Y; + iconInfo.fIcon = false; + + var icon = Functions.CreateIconIndirect(ref iconInfo); + + if (icon != IntPtr.Zero) + { + // Currently using a custom cursor so destroy it + // once replaced + bool destoryOld = cursor != MouseCursor.Default; + + cursor = value; + curson_handle = icon; + var oldCursor = Functions.SetCursor(icon); + + if (destoryOld) + { + Functions.DestroyIcon(oldCursor); + } + } + } + } } } -- 2.7.4