0

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

当我通过使用 ToString() 方法转换游标来检查当前控件游标和新游标是否相同时,我得到:

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

转换系统默认光标没有问题,只有在转换自定义光标时才会出现。有人请告诉我为什么这个异常只在自定义光标上引发?

这是我得到错误的地方... **if (m_cursorAction.ToString() != newCursor.ToString()) m_cursorAction = newCursor;**

这是我的光标实现代码:

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
}
4

2 回答 2

2

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

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

于 2013-06-21T05:49:54.473 回答
0

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

// 65569 = Hand Pointer:
int handle = 65569;

if (handle == System.Windows.Forms.Cursor.Current.Handle.ToInt32())
    MessageBox.Show("Hand Pointer Cursor");
于 2016-11-22T23:29:29.277 回答