Windows 窗体应用程序中未抗锯齿的手形光标

发布于 2024-11-07 19:58:24 字数 291 浏览 3 评论 0原文

好的,那么您知道 Windows Vista 和 Windows 7 MS 中如何更改手形光标(当您将鼠标悬停在超链接上时显示的光标),并向其添加更多细节,使其具有抗锯齿效果并且边缘美观平滑吗?

那么,为什么 Windows 窗体应用程序中的情况与此不同呢?

我厌倦了看着一个看起来像穴居人画的蹩脚手形光标。

有没有一种方法可以以编程方式告诉它显示系统中实际安装的那个?我查看了 Windows 目录中的 Cursors 文件夹,发现旧的手形光标根本不存在!那么为什么 WinForms 仍然使用旧的呢?我怎样才能“升级”它?

Okay, so you know how in Windows Vista and Windows 7 MS changed the Hand Cursor (the one that shows up when you hover over a hyperlink), and added more detail to it so it's antialiased and nice and smooth around the edges?

Well, why isn't it like that in Windows Forms apps?

I'm sick off looking at a crappy hand cursor that looks like it was drawn by a caveman.

Is there a way to programmatically tell it to display the one that's actually installed in the system? I looked in the Cursors folder in my Windows directory, and the old hand cursor isn't even there! So why is WinForms still using the old one? How can I 'upgrade' it?

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

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

发布评论

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

评论(5

权谋诡计 2024-11-14 19:58:24

是的,WinForms 控件仍然使用 Windows 98/2000 附带的老式手形光标。它缺乏 Aero 光标所具有的抗锯齿效果。这是因为 .NET Framework 包含其自己的硬编码游标,它使用该游标而不是系统默认游标。我认为这是因为 .NET 的早期版本针对的是 Windows 95 等操作系统,这些操作系统没有与此光标捆绑在一起,但还没有进行考古学来证明这一点。

幸运的是,强制它使用正确的方法很容易。您只需告诉操作系统您希望它使用默认的手形光标,然后无论用户在哪个版本的 Windows 上运行您的程序,即使他们已经更改了默认的鼠标光标,它都是正确的主题。

最简单的方法是对现有控件进行子类化,覆盖 WndProc 函数 拦截 WM_SETCURSOR消息,并告诉它使用系统IDC_HAND光标。您只需要一点 P/Invoke 魔法。

以下代码是使用 LinkLabel 控件的示例:

public class LinkLabelEx : LinkLabel
{
    private const int WM_SETCURSOR = 0x0020;
    private const int IDC_HAND = 32649;

    [DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
    private static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    private static extern IntPtr SetCursor(IntPtr hCursor);

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_SETCURSOR)
        {
            // Set the cursor to use the system hand cursor
            SetCursor(LoadCursor(IntPtr.Zero, IDC_HAND));

            // Indicate that the message has been handled
            m.Result = IntPtr.Zero;
            return;
        }

        base.WndProc(ref m);
    }
}

Yes, the WinForms controls still use the old-school hand cursor, as shipped with Windows 98/2000. It lacks the anti-aliasing effects that the one included with the Aero cursors does. This is because the .NET Framework includes its own hard-coded cursor, which it uses instead of the system default. I presume this is because early versions of .NET were targeting operating systems like Windows 95 that didn't come bundled with this cursor, but haven't done the archaeology to prove it.

Fortunately, it's easy enough to force it to use the right one. You just have to tell the operating system you want it to use the default hand cursor, and then it will be correct no matter what version of Windows the user runs your program on, and even if they've changed their mouse cursors from the default theme.

The simplest way of doing that is to subclass the existing control, override the WndProc function to intercept the WM_SETCURSOR message, and tell it to use the system IDC_HAND cursor. You just need a little bit of P/Invoke magic.

The following code is an example of how that might look using the LinkLabel control:

public class LinkLabelEx : LinkLabel
{
    private const int WM_SETCURSOR = 0x0020;
    private const int IDC_HAND = 32649;

    [DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
    private static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    private static extern IntPtr SetCursor(IntPtr hCursor);

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_SETCURSOR)
        {
            // Set the cursor to use the system hand cursor
            SetCursor(LoadCursor(IntPtr.Zero, IDC_HAND));

            // Indicate that the message has been handled
            m.Result = IntPtr.Zero;
            return;
        }

        base.WndProc(ref m);
    }
}
最美不过初阳 2024-11-14 19:58:24

请原谅我复活一年前的话题!

在搞乱了原始解决方案并查看了 反映 LinkLabel 源代码,我“终于”找到了一种快速而干净的方法:

using System.Runtime.InteropServices;

namespace System.Windows.Forms {
    public class LinkLabelEx : LinkLabel {
        private const int IDC_HAND = 32649;

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

        private static readonly Cursor SystemHandCursor = new Cursor(LoadCursor(IntPtr.Zero, IDC_HAND));

        protected override void OnMouseMove(MouseEventArgs e) {
            base.OnMouseMove(e);

            // If the base class decided to show the ugly hand cursor
            if(OverrideCursor == Cursors.Hand) {
                // Show the system hand cursor instead
                OverrideCursor = SystemHandCursor;
            }
        }
    }
}

这个类实际上做了我们想要的事情:它显示正确的系统手形光标而不闪烁,并且仅在控件的 LinkArea 上执行此操作。

Excuse me for resurrecting a year-old thread!!!

After messing around with the original solution and taking a look at the reflected LinkLabel source code, I "finally" found a quick yet clean way of doing it :

using System.Runtime.InteropServices;

namespace System.Windows.Forms {
    public class LinkLabelEx : LinkLabel {
        private const int IDC_HAND = 32649;

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

        private static readonly Cursor SystemHandCursor = new Cursor(LoadCursor(IntPtr.Zero, IDC_HAND));

        protected override void OnMouseMove(MouseEventArgs e) {
            base.OnMouseMove(e);

            // If the base class decided to show the ugly hand cursor
            if(OverrideCursor == Cursors.Hand) {
                // Show the system hand cursor instead
                OverrideCursor = SystemHandCursor;
            }
        }
    }
}

This class actually does what we want: It shows the proper system hand cursor without flickering and does this only on the LinkArea of the control.

霓裳挽歌倾城醉 2024-11-14 19:58:24

这篇文章解决了其他文章的问题:

  • 它尊重链接位置,并在光标位于链接上时显示手
  • 鼠标移动时不会闪烁

您需要将光标更改为系统手形光标。为此,您需要处理 WM_SETCURSOR 并检查 OverrideCursor 是否为 Cursors.Hand,然后通过调用 将其更改为系统光标设置光标

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class MyLinkLabel : LinkLabel
{
    const int IDC_HAND = 32649;
    const int WM_SETCURSOR = 0x0020;
    const int HTCLIENT = 1;
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
    [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
    static extern IntPtr SetCursor(HandleRef hcursor);
    static readonly Cursor SystemHandCursor = 
        new Cursor(LoadCursor(IntPtr.Zero, IDC_HAND));
    protected override void WndProc(ref Message msg)
    {
        if (msg.Msg == WM_SETCURSOR)
            WmSetCursor(ref msg);
        else
            base.WndProc(ref msg);
    }
    void WmSetCursor(ref Message m)
    {
        if (m.WParam == (IsHandleCreated ? Handle : IntPtr.Zero) &&
           (unchecked((int)(long)m.LParam) & 0xffff) == HTCLIENT) {
            if (OverrideCursor != null) {
                if (OverrideCursor == Cursors.Hand)
                    SetCursor(new HandleRef(SystemHandCursor, SystemHandCursor.Handle));
                else
                    SetCursor(new HandleRef(OverrideCursor, OverrideCursor.Handle));
            }
            else {
                SetCursor(new HandleRef(Cursor, Cursor.Handle));
            }
        }
        else {
            DefWndProc(ref m);
        }
    }
}

This post solves problems of the other posts:

  • It respects to the link location and shows the hand just when cursor is on link
  • It doesn't flicker on mouse move

You need to change the cursor to system hand cursor. To do so, you need to handle WM_SETCURSOR and check if OverrideCursor is Cursors.Hand then change it to the system cursor by calling SetCursor:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class MyLinkLabel : LinkLabel
{
    const int IDC_HAND = 32649;
    const int WM_SETCURSOR = 0x0020;
    const int HTCLIENT = 1;
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
    [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
    static extern IntPtr SetCursor(HandleRef hcursor);
    static readonly Cursor SystemHandCursor = 
        new Cursor(LoadCursor(IntPtr.Zero, IDC_HAND));
    protected override void WndProc(ref Message msg)
    {
        if (msg.Msg == WM_SETCURSOR)
            WmSetCursor(ref msg);
        else
            base.WndProc(ref msg);
    }
    void WmSetCursor(ref Message m)
    {
        if (m.WParam == (IsHandleCreated ? Handle : IntPtr.Zero) &&
           (unchecked((int)(long)m.LParam) & 0xffff) == HTCLIENT) {
            if (OverrideCursor != null) {
                if (OverrideCursor == Cursors.Hand)
                    SetCursor(new HandleRef(SystemHandCursor, SystemHandCursor.Handle));
                else
                    SetCursor(new HandleRef(OverrideCursor, OverrideCursor.Handle));
            }
            else {
                SetCursor(new HandleRef(Cursor, Cursor.Handle));
            }
        }
        else {
            DefWndProc(ref m);
        }
    }
}
属性 2024-11-14 19:58:24

很抱歉收回这篇旧帖子,但我对此也有某种解决方案。
如果您需要在应用程序范围内应用系统光标而不接触旧控件,请在应用程序启动时使用它:

    private static void TrySetCursorsDotHandToSystemHandCursor()
    {
        try
        {
            typeof(Cursors).GetField("hand", BindingFlags.Static | BindingFlags.NonPublic)
                           .SetValue(null, SystemHandCursor);
        }
        catch { }
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

    private static readonly Cursor SystemHandCursor = new Cursor(LoadCursor(IntPtr.Zero, 32649 /*IDC_HAND*/));

Sorry for getting this old post back, but i also have some kind of solution for this.
If you need to apply the systemcursor application wide without touching old controls, use this at applicationstart:

    private static void TrySetCursorsDotHandToSystemHandCursor()
    {
        try
        {
            typeof(Cursors).GetField("hand", BindingFlags.Static | BindingFlags.NonPublic)
                           .SetValue(null, SystemHandCursor);
        }
        catch { }
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

    private static readonly Cursor SystemHandCursor = new Cursor(LoadCursor(IntPtr.Zero, 32649 /*IDC_HAND*/));
屌丝范 2024-11-14 19:58:24

在不创建新控件的情况下执行此操作需要更改控件的光标并创建自定义链接标签,否则它将无法工作
我们通过添加一个标签来创建自定义链接标签,更改字体下划线并更改其前景色并添加点击事件

Private Const IDC_HAND As Integer = 32649

<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Shared Function LoadCursor(ByVal hInstance As IntPtr, ByVal lpCursorName As Integer) As IntPtr
End Function

Private Shared ReadOnly SystemHandCursor As Cursor = New Cursor(LoadCursor(IntPtr.Zero, IDC_HAND))
    
'add the cursor to custom linklabel
CustomLinkLabel1.Cursor = SystemHandCursor

抱歉只有 vb .net 代码,您可以使用在线转换器
编辑:缺少一些代码

Doing that without creating a new control wee need to change the Control's Cursor AND create a custom linklabel or else it wouldn't work
we create the custom linklabel by adding a label changing the font underline and changing it's fore color and add an click event

Private Const IDC_HAND As Integer = 32649

<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Shared Function LoadCursor(ByVal hInstance As IntPtr, ByVal lpCursorName As Integer) As IntPtr
End Function

Private Shared ReadOnly SystemHandCursor As Cursor = New Cursor(LoadCursor(IntPtr.Zero, IDC_HAND))
    
'add the cursor to custom linklabel
CustomLinkLabel1.Cursor = SystemHandCursor

sorry only have vb .net code you might use an online converter
EDIT: some code was missing

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