/// <summary> /// Create a 32x32 cursor from a bitmap, with the hot spot in the middle /// </summary> public static Cursor CreateCursor(Bitmap bmp, int x, int y) { int xHotSpot = x; int yHotSpot = y; if (!CursorIconHandler.Equals(IntPtr.Zero)) { DestroyIcon(CursorIconHandler); CursorIconHandler = IntPtr.Zero; } CursorIconHandler = bmp.GetHicon(); IconInfo tmp = new IconInfo(); GetIconInfo(CursorIconHandler, ref tmp); tmp.xHotspot = xHotSpot; tmp.yHotspot = yHotSpot; tmp.fIcon = false; DestroyIcon(CursorIconHandler); CursorIconHandler = CreateIconIndirect(ref tmp); Cursor ret = new Cursor(CursorIconHandler); if (CursorGenerated != null) { CursorGenerated.Dispose(); CursorGenerated = null; } CursorGenerated = ret; return(ret); }
/// <summary> /// Create a cursor from a bitmap without resizing and with the specified /// hot spot /// </summary> static Cursor CreateCursorNoResize(Bitmap bmp, int xHotSpot, int yHotSpot) { IntPtr ptr = bmp.GetHicon(); IconInfo tmp = new IconInfo(); GetIconInfo(ptr, ref tmp); tmp.xHotspot = xHotSpot; tmp.yHotspot = yHotSpot; tmp.fIcon = false; DestroyIcon(ptr); ptr = CreateIconIndirect(ref tmp); Cursor ret = new Cursor(ptr); DestroyIcon(ptr); if (CursorGenerated != null) { CursorGenerated.Dispose(); CursorGenerated = null; } CursorGenerated = ret; return(ret); }