在WinForms/.NET中更改光标热点

在WinForms/.NET中更改光标热点,.net,winforms,.net,Winforms,我正在运行时从图像资源创建游标。新光标的热点始终设置为16x16(32x32图像)。是否可以在运行时更改热点,或者我需要创建.cur文件?当然可以。以下是我的实用程序函数,请根据需要进行编辑:) 公共结构图标信息 { 公共图书馆; 公共int xHotspot; 公共场所; 公共IntPtr hbmMask; 公共IntPtr hbmColor; } [DllImport(“user32.dll”)] [返回:Marshallas(UnmanagedType.Bool)] 公共静态外部bool

我正在运行时从图像资源创建游标。新光标的热点始终设置为16x16(32x32图像)。是否可以在运行时更改热点,或者我需要创建.cur文件?

当然可以。以下是我的实用程序函数,请根据需要进行编辑:)

公共结构图标信息
{
公共图书馆;
公共int xHotspot;
公共场所;
公共IntPtr hbmMask;
公共IntPtr hbmColor;
}
[DllImport(“user32.dll”)]
[返回:Marshallas(UnmanagedType.Bool)]
公共静态外部bool GetIconInfo(IntPtr hIcon,ref-IconInfo pIconInfo);
[DllImport(“user32.dll”)]
公共静态外部IntPtr CreateIconIndirect(参考IConinInfo图标);
/// 
///从位图创建光标,无需调整大小并使用指定的
///热点
/// 
公共静态光标CreateCursorNoResize(位图bmp、int-xHotSpot、int-yHotSpot)
{
IntPtr ptr=bmp.GetHicon();
IconInfo tmp=新的IconInfo();
GetIconInfo(ptr,参考tmp);
tmp.xHotspot=xHotspot;
tmp.yHotspot=yHotspot;
tmp.fIcon=false;
ptr=CreateIconIndirect(参考tmp);
返回新光标(ptr);
}
/// 
//从位图创建一个32×32游标,中间的热点
/// 
公共静态光标CreateCursor(位图bmp)
{
int xHotSpot=16;
int-yHotSpot=16;
IntPtr ptr=((位图)大小图像(bmp,32,32)).GetHicon();
IconInfo tmp=新的IconInfo();
GetIconInfo(ptr,参考tmp);
tmp.xHotspot=xHotspot;
tmp.yHotspot=yHotspot;
tmp.fIcon=false;
ptr=CreateIconIndirect(参考tmp);
返回新光标(ptr);
}

看一看。似乎有两种可能的解决方案(使用P/Invoke),您应该能够复制粘贴并使用。

因为这是一个.NET问题,而不是一个C#问题,下面是Nick部分代码的VB.NET转换(以避免其他人的麻烦)


哦,老兄,我花了几个小时来更改.ico文件中的位,试图用一个合适的热点制作一个彩色的.cur-现在我可以只使用初始PNG了。真是松了一口气。
    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);
    }
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