使用 Windows 窗体应用程序时如何将鼠标光标更改为自定义光标?

发布于 2024-11-24 12:22:06 字数 413 浏览 1 评论 0原文

UserControl 中,我想将鼠标光标从箭头更改为手形图标。
我目前所做的是:

this.Cursor = Cursors.Hand;

这非常好,它给了我一个看起来像这样的鼠标光标:

在此处输入图像描述

但我的问题来了...这显示了一只手指指向的手。
我需要的是一只“抓”手,更像是这样的:

在此处输入图像描述

我该怎么做?如何加载图标文件 (.ico)、光标文件 (.cur) 或图像文件 (.png),并将其用作鼠标光标?

In a UserControl I want to change the mouse cursor from the arrow, to a hand icon.
What I currently do is this:

this.Cursor = Cursors.Hand;

This is very nice, it gives me a mouse cursor looking like this:

enter image description here

But here comes my problem... this shows a hand with a pointing finger.
What I need is a "grabbing" hand, more like this one:

enter image description here

How do I do this?, How can I load an icon file (.ico), a cursor file (.cur), or image file (.png), and use it as the mouse cursor?

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

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

发布评论

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

评论(4

GRAY°灰色天空 2024-12-01 12:22:06

如果您有光标文件:

Cursor myCursor = new Cursor("myCursor.cur");
myControl.Cursor = myCursor;

否则您必须创建一个:

有关 自定义光标

If you have a cursor file:

Cursor myCursor = new Cursor("myCursor.cur");
myControl.Cursor = myCursor;

otherwise you have to create one:

some more information about custom cursors

自此以后,行同陌路 2024-12-01 12:22:06

将自定义游标与 WinForms Cursor 类一起使用时需要注意的是,当使用流、文件名和资源构造函数重载时,提供的 .cur 文件必须为黑色且颜色为白色。

这意味着如果 .cur 文件包含除黑色和白色之外的任何颜色,则此方法将不起作用。

Cursor myCursor = new Cursor("myCursor.cur");
myControl.Cursor = myCursor;

有一种方法可以通过使用 Windows 句柄构造函数重载来解决此限制:

使用 Windows API 创建句柄:

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr LoadCursorFromFile(string fileName);

然后将其传递给适当的 Cursor 构造函数,如下所示:

IntPtr handle = LoadCursorFromFile("myCursor.cur");
Cursor myCursor = new Cursor(handle);
myControl.Cursor = myCursor;

我希望这可以防止其他人刮擦他们的导致抛出 ArgumentException ,指出:图像格式无效。图像文件可能已损坏。当使用其他Cursor构造函数时,会重载包含颜色的.cur文件。

A caveat to using custom cursors with the WinForms Cursor class is that when using the stream, filename, and resource constructor overloads the supplied .cur file must be black and white in color.

Meaning that this will not work if the .cur files contains any colors besides black and white.

Cursor myCursor = new Cursor("myCursor.cur");
myControl.Cursor = myCursor;

There is a way around this limitation by using the Windows handle constructor overload:

Create the handle by using the Windows API:

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr LoadCursorFromFile(string fileName);

Then pass it to the appropriate Cursor constructor like this:

IntPtr handle = LoadCursorFromFile("myCursor.cur");
Cursor myCursor = new Cursor(handle);
myControl.Cursor = myCursor;

I hope this prevents others from scratching their heads to an ArgumentException being thrown stating: Image format is not valid. The image file may be corrupted. when using the other Cursor constructor overloads with a .cur file that contains color.

终陌 2024-12-01 12:22:06

您是否尝试过 System.Windows.Forms.Cursor curs = new System.Windows.Forms.Cursor(file_name); ?

Have you tried System.Windows.Forms.Cursor curs = new System.Windows.Forms.Cursor(file_name); ?

锦欢 2024-12-01 12:22:06

我测试了这个方法。没关系。这是我的申请:

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern IntPtr LoadCursorFromFile(string fileName);
    Cursor myCursor;
    private void tsbtn_ZoomIn_Click(object sender, EventArgs e)
    {
        IntPtr handle = LoadCursorFromFile("view_zoom_in.cur");
        myCursor = new Cursor(handle);
        zg1.Cursor = myCursor;
    }

I tested this method. It's OK. This is my apply:

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern IntPtr LoadCursorFromFile(string fileName);
    Cursor myCursor;
    private void tsbtn_ZoomIn_Click(object sender, EventArgs e)
    {
        IntPtr handle = LoadCursorFromFile("view_zoom_in.cur");
        myCursor = new Cursor(handle);
        zg1.Cursor = myCursor;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文