“自定义游标无法转换为字符串”将游标转换为字符串时引发异常

本文关键字:字符串 转换 游标 异常 自定义 | 更新日期: 2023-09-27 18:19:14

我正在尝试在某些条件下更改控件的光标。我已经创建了自己的自定义游标,并将其分配给 Cursor.Current 属性。到目前为止,一切正常。

当我通过使用 ToString(( 方法转换光标来检查当前控件光标和新光标是否基于它们的名称相同时,我得到:

自定义光标无法转换为字符串

转换

系统的默认光标没有问题,它仅在转换自定义光标时升高。有人请告诉我为什么仅在自定义光标上引发此异常?

这是我收到错误的地方... **if (m_cursorAction.ToString(( != newCursor.ToString((( m_cursorAction = 新光标;**

这是我的光标实现代码:

Bitmap bitmap = new Bitmap(140, 25);
Graphics g = Graphics.FromImage(bitmap);
using (Font f = new Font("SEGOE UI", 10))
    g.DrawString("Node 30", f, System.Drawing.Brushes.Black, 0, 0);
Cursor.Current = MyCursor.CreateCursor(bitmap, 140, 25);
**if (m_cursorAction.ToString() != newCursor.ToString())**
    m_cursorAction = newCursor;
public class MyCursor
{
    #region Class members
    private static IntPtr ptr;
    public struct IconInfo
    {
        public bool bIcon;
        public int xHotspot;
        public int yHotspot;
        public IntPtr hbmMask;
        public IntPtr hbmColor;
    }
    #endregion
    #region Class Public Methods
    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public static extern bool DestroyIcon(IntPtr handle);
    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
    public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public static extern IntPtr CreateIconIndirect([System.Runtime.InteropServices.In]ref IconInfo icon);
    [System.Runtime.InteropServices.DllImport("gdi32")]
    extern internal static bool DeleteObject(IntPtr hObject);
    /// <summary>
    /// Create a custom cursor with the given bitmap
    /// </summary>
    /// <param name="bmp">Bitmap for the cursor.</param>
    /// <param name="xHotSpot">x hot spot to the cursor.</param>
    /// <param name="yHotSpot">y hot spot to the cursor.</param>
    public static System.Windows.Forms.Cursor CreateCursor(System.Drawing.Bitmap bmp, int xHotSpot, int yHotSpot)
    {
        IntPtr bmpPtr= bmp.GetHicon();
        IconInfo icon = new IconInfo();
        GetIconInfo(bmpPtr, ref icon);
        icon.xHotspot = xHotSpot;
        icon.yHotspot = yHotSpot;
        icon.bIcon = false;
        DestroyIcon(bmpPtr);
        DeleteObject(bmpPtr);
        ptr = CreateIconIndirect(ref icon);
        System.Windows.Forms.Cursor cursor = new System.Windows.Forms.Cursor(ptr);            
        //delete the GDI objects and icon
        DeleteObject(icon.hbmColor);
        DeleteObject(icon.hbmMask);
        DestroyIcon(icon.hbmColor);
        DestroyIcon(icon.hbmMask);
        return cursor;
    }
    /// <summary>
    /// Destroy the custom cursor      
    /// </summary>
    public static void Destroy()
    {
        DestroyIcon(ptr);
        DeleteObject(ptr);
    }
    #endregion
}

“自定义游标无法转换为字符串”将游标转换为字符串时引发异常

因为它委托给 CursorConverter.ConvertTo ,旨在仅针对自定义游标引发该异常。

只是不要使用字符串比较来测试游标是否相等。 请改用专为比较游标而设计的 Cursor.Equals 方法。

对于自定义游标,如果您知道要查找的内容,一种方法是检查句柄:

// 65569 = Hand Pointer:
int handle = 65569;
if (handle == System.Windows.Forms.Cursor.Current.Handle.ToInt32())
    MessageBox.Show("Hand Pointer Cursor");