views:

23

answers:

1

I have the following code in my project to change mouse cursor when the user is hovering over a custom button:

protected override void OnMouseEnter(EventArgs e)
{
    this.Cursor = Cursors.Hand;
    base.OnMouseEnter(e);
}

protected override void OnMouseLeave(EventArgs e)
{
    this.Cursor = Cursors.Default;
    base.OnMouseLeave(e);
}

This works fine, except that the cursor that is shown is the standard white hand cursor. But in Mouse Properties in Windows XP I have set the Link Select cursor to be an animated colorful arrow.

To investigate the problem I set the animated arrow as the Busy cursor in Mouse Properties and changed the code in OnMouseEnter to:

this.Cursor = Cursors.WaitCursor;

This works as I expected and the arrow was shown.

It seems like Cursors.Hand does not correspond to the Link Select cursor in Mouse Properties. But I can't find anything more appropriate to use in the Cursors class. What am I doing wrong?

+2  A: 

The .NET framework provides its own cursor for Cursor.Hand; it doesn't load the user-selected cursor from the operating system.

I can only imagine that this is because Windows NT 4, on which .NET will run, does not provide a "hand" cursor. It was a feature added in Windows 98 and 2000. Applications which target Windows 95 or NT 4 provide their own hand cursor if they need one.

The good news is that the workaround is relatively simple. It's a fairly small amount of interop. You just need to use LoadCursor with IDC_HAND, then pass the returned handle to the constructor for the Cursor class.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

class Form1 : Form{
    enum IDC{
        HAND = 32649,
        // other values omitted
    }

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    static extern IntPtr LoadCursor(IntPtr hInstance, IDC cursor);

    public Form1(){
        Cursor = new Cursor(LoadCursor(IntPtr.Zero, IDC.HAND));
    }
}
P Daddy
Thanks for the explanation. And the work around worked perfectly.
Gieron