9

I'm creating a cursor at runtime from a image resource. The HotSpot of the new Cursor is always set to 16x16 (32x32 image). Is it possible to change the HotSpot at runtime or will I need to create .cur files?

smack0007
  • 11,016
  • 7
  • 41
  • 48

3 Answers3

28

You sure can. Here are my utility functions, edit as you see fit :)

    public struct IconInfo
    {
        public bool fIcon;
        public int xHotspot;
        public int yHotspot;
        public IntPtr hbmMask;
        public IntPtr hbmColor;
    }
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
    [DllImport("user32.dll")]
    public static extern IntPtr CreateIconIndirect(ref IconInfo icon);

    /// <summary>
    /// Create a cursor from a bitmap without resizing and with the specified
    /// hot spot
    /// </summary>
    public 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;
        ptr = CreateIconIndirect(ref tmp);
        return new Cursor(ptr);
    }


    /// <summary>
    /// Create a 32x32 cursor from a bitmap, with the hot spot in the middle
    /// </summary>
    public static Cursor CreateCursor(Bitmap bmp)
    {
        int xHotSpot = 16;
        int yHotSpot = 16;

        IntPtr ptr = ((Bitmap)ResizeImage(bmp, 32, 32)).GetHicon();
        IconInfo tmp = new IconInfo();
        GetIconInfo(ptr, ref tmp);
        tmp.xHotspot = xHotSpot;
        tmp.yHotspot = yHotSpot;
        tmp.fIcon = false;
        ptr = CreateIconIndirect(ref tmp);
        return new Cursor(ptr);
    }
Nick
  • 13,238
  • 17
  • 64
  • 100
  • Oh, man, I've spent a few fruitless hours changing bits in .ico file trying to make a colorful .cur with a proper hotspot - now I can just use initial pngs. What a relief. – Usurer Sep 05 '16 at 12:40
2

Since this is a .NET question and not specifically a C# question, here is a VB.NET conversion of part of Nick's code (to save others the trouble).

Module IconUtility
Structure IconInfo
    Public fIcon As Boolean
    Public xHotspot As Integer
    Public yHotspot As Integer
    Public hbmMask As IntPtr
    Public hbmColor As IntPtr
End Structure

Private Declare Function GetIconInfo Lib "user32.dll" (hIcon As IntPtr, ByRef pIconInfo As IconInfo) As Boolean
Private Declare Function CreateIconIndirect Lib "user32.dll" (ByRef icon As IconInfo) As IntPtr

' Create a cursor from a bitmap without resizing and with the specified hot spot
Public Function CreateCursorNoResize(bmp As System.Drawing.Bitmap, xHotSpot As Integer, yHotSpot As Integer) As Cursor
    Dim ptr As IntPtr = bmp.GetHicon
    Dim tmp As IconInfo = New IconInfo()
    GetIconInfo(ptr, tmp)
    tmp.xHotspot = xHotSpot
    tmp.yHotspot = yHotSpot
    tmp.fIcon = False
    ptr = CreateIconIndirect(tmp)
    Return New Cursor(ptr)
End Function
End Module
Mark T
  • 3,464
  • 5
  • 31
  • 45
0

Take a look at this post on MSDN. There seem to be a couple possible solutions (using P/Invoke) which you should be able to copy-paste and use.

lc.
  • 113,939
  • 20
  • 158
  • 187