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

时间:2012-01-29 19:40:19

标签: c# pointers mouse

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

提前谢谢:)

1 个答案:

答案 0 :(得分:2)

这是一个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");
祝你好运!!