Cursors.Hand 不显示链接选择光标

发布于 2024-09-15 23:04:35 字数 643 浏览 7 评论 0原文

我的项目中有以下代码,用于当用户将鼠标悬停在自定义按钮上时更改鼠标光标:

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

这工作正常,只是显示的光标是标准的白色手形光标。但在 Windows XP 的鼠标属性中,我将链接选择光标设置为动画彩色箭头。

为了调查这个问题,我在“鼠标属性”中将动画箭头设置为“忙碌”光标,并将“OnMouseEnter”中的代码更改为:

this.Cursor = Cursors.WaitCursor;

这按我的预期工作,并且显示了箭头。

似乎 Cursors.Hand 与鼠标属性中的链接选择光标不对应。但我找不到更适合在 Cursors 类中使用的内容。我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度° 2024-09-22 23:04:35

.NET框架为Cursor.Hand提供了自己的光标;它不会从操作系统加载用户选择的光标。

我只能想象这是因为运行.NET 的Windows NT 4 不提供“手”光标。这是 Windows 98 和 2000 中添加的一项功能。针对 Windows 95 或 NT 4 的应用程序会在需要时提供自己的手形光标。

好消息是解决方法相对简单。这是相当少量的互操作。您只需将 LoadCursorIDC_HAND 结合使用,然后将返回的句柄传递给 Cursor 类的构造函数。

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

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));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文