[Win] Fixed MouseCursor.Default

It is now possible to switch from a custom cursor back to
MouseCursor.Default.
This commit is contained in:
thefiddler 2014-04-28 09:37:16 +02:00
parent 4f9a2f78d6
commit 509f356ed4

View file

@ -1203,47 +1203,58 @@ namespace OpenTK.Platform.Windows
{ {
if (value != cursor) if (value != cursor)
{ {
var stride = value.Width * bool destoryOld = cursor != MouseCursor.Default;
(Bitmap.GetPixelFormatSize(System.Drawing.Imaging.PixelFormat.Format32bppArgb) / 8); IntPtr oldCursor = IntPtr.Zero;
Bitmap bmp; if (value == MouseCursor.Default)
unsafe
{ {
fixed (byte* pixels = value.Rgba) oldCursor = Functions.SetCursor(Functions.LoadCursor(CursorName.Arrow));
cursor = value;
}
else
{
var stride = value.Width *
(Bitmap.GetPixelFormatSize(System.Drawing.Imaging.PixelFormat.Format32bppArgb) / 8);
Bitmap bmp;
unsafe
{ {
bmp = new Bitmap(value.Width, value.Height, stride, fixed (byte* pixels = value.Rgba)
System.Drawing.Imaging.PixelFormat.Format32bppArgb, {
new IntPtr(pixels)); bmp = new Bitmap(value.Width, value.Height, stride,
System.Drawing.Imaging.PixelFormat.Format32bppArgb,
new IntPtr(pixels));
}
}
using (bmp)
{
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
cursor = value;
curson_handle = icon;
oldCursor = Functions.SetCursor(icon);
}
}
} }
} }
var iconInfo = new IconInfo(); if (destoryOld && oldCursor != IntPtr.Zero)
var bmpIcon = bmp.GetHicon();
var success = Functions.GetIconInfo(bmpIcon, out iconInfo);
if (success)
{ {
iconInfo.xHotspot = value.X; Functions.DestroyIcon(oldCursor);
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);
}
}
} }
} }
} }