Question

I have two link labels in my windows forms program which links to my website. I got rid of the underlines and the ugly blue colour and tried to fix them up a little bit. But the biggest problem still remains and It's just so disturbing for me, I don't know why.

The hand cursor when you hover over them is that old Windows 98 hand/link cursor. Is there any way to change it to the system cursor? I've checked some other links about this problem, but I couldn't get it to work so I decided to ask here.

Here's my code to get rid of the underline btw: linkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;

Was it helpful?

Solution

Unfortunately the LinkLabel class is hard-coded to use Cursors.Hand as the hover cursor.

However, you can work around it by adding a class like this to your project:

public class MyLinkLabel : LinkLabel
{
    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);
        OverrideCursor = Cursors.Cross;
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        OverrideCursor = null;
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        OverrideCursor = Cursors.Cross;
    }
}

and using that instead of LinkLabel on your form. (This sets the cursor to a cross for testing purposes, but you can change it to whatever you want.)

I should say that the real LinkLabel code has much more complex logic to do with changing the cursor according to whether or not the link is enabled, but you might not care about that.

OTHER TIPS

Set the Cursor property to Arrow in the properties pane of the LinkLabel in Visual Studio

Update I prefer Hamido-san's answer here. His solution works properly when the LinkLabel is set to AutoSize = false and works with a LinkArea.

Old solution:

public class LnkLabel : LinkLabel
{
    const int WM_SETCURSOR =    32,
              IDC_HAND     = 32649;

    [DllImport("user32.dll")]
    public static extern int LoadCursor(int hInstance, int lpCursorName);

    [DllImport("user32.dll")]
    public static extern int SetCursor(int hCursor);

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_SETCURSOR)
        {
            int cursor = LoadCursor(0, IDC_HAND);

            SetCursor(cursor);

            m.Result = IntPtr.Zero; // Handled

            return;
        }

        base.WndProc(ref m);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top