c# - 如何使用图像作为鼠标指针而不是使用 C# 的默认箭头?

标签 c# pointers mouse

浏览后,我有一个代码可以通过使用 C# 给出坐标来以编程方式控制鼠标的移动。我想做同样的事情,但鼠标指针应该用点图像代替。我无法得到它...所以请让我知道 C# 中是否有任何方法...

提前致谢:)

最佳答案

这是一个great tutorial它向您展示了如何做到这一点。

更新

正如 Hans Passant 提醒我的那样,有更好的方法。这是他的版本,摘自 this Stack Overflow Post :

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

static class NativeMethods {
    public static Cursor LoadCustomCursor(string path) {
        IntPtr hCurs = LoadCursorFromFile(path);
        if (hCurs == IntPtr.Zero) throw new Win32Exception();
        var curs = new Cursor(hCurs);
        // Note: force the cursor to own the handle so it gets released properly
        var fi = typeof(Cursor).GetField("ownHandle", BindingFlags.NonPublic | BindingFlags.Instance);
        fi.SetValue(curs, true);
        return curs;
    }
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern IntPtr LoadCursorFromFile(string path);
}

这是一个用法示例:

this.Cursor = NativeMethods.LoadCustomCursor(@"c:\windows\cursors\aero_busy.ani");

祝你好运!

关于c# - 如何使用图像作为鼠标指针而不是使用 C# 的默认箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9056135/

相关文章:

C - 传递结构指针会终止程序?如何

关于指针数组的C代码

c - 如何在 Objective-C 中处理指针数组

c# - 用于创建 API URL 的设计模式

c# - 为什么这段代码在将近一年的时间里运行良好却无法正常工作

c# - 编写一个在后台运行并每小时执行一次操作的程序

java - java中是否有一个标准的方法来处理鼠标事件的许多不同选项?

c# - 从 Excel 读取 NUnit 测试数据 - 如何访问测试数据?

c++ - 在哪里实现容忍度来决定何时在 Qt 中检测触摸屏按下事件和移动事件?

image - 使用鼠标事件进行 Angular 2/4 图像裁剪