Partial implementation of Windows MouseCursor.
authorFraser <frassle@gmail.com>
Fri, 21 Feb 2014 00:24:24 +0000 (00:24 +0000)
committerthefiddler <stapostol@gmail.com>
Sun, 27 Apr 2014 08:53:32 +0000 (10:53 +0200)
Very buggy, but starting to show results.

Source/OpenTK/Platform/Windows/WinGLNative.cs

index 94ca0c6..6fc532d 100644 (file)
@@ -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);
+                            }
+                        }
+                    }
+                }
             }
         }