【问题标题】:Change Cursor HotSpot in WinForms / .NET在 WinForms / .NET 中更改光标热点
【发布时间】:2015-08-09 07:02:34
【问题描述】:

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

【问题讨论】:

    标签: .net winforms


    【解决方案1】:

    你当然可以。这是我的实用函数,您可以根据需要进行编辑:)

        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);
        }
    

    【讨论】:

    • 哦,伙计,我花了几个小时更改 .ico 文件中的位,试图用适当的热点制作一个多彩的 .cur - 现在我可以只使用初始 png。真是一种解脱。
    【解决方案2】:

    由于这是一个 .NET 问题,而不是专门的 C# 问题,这里是 Nick 的部分代码的 VB.NET 转换(为其他人省去麻烦)。

    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
    

    【讨论】:

      【解决方案3】:

      看看this post on MSDN。似乎有几个可能的解决方案(使用 P/Invoke),您应该能够复制粘贴并使用它们。

      【讨论】:

        猜你喜欢
        • 2011-02-23
        • 1970-01-01
        • 2013-11-02
        • 1970-01-01
        • 2012-09-22
        • 1970-01-01
        • 2017-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多