c#自定义鼠标形状
更改鼠标指针,需要使用到 Windows API:
1. 添加命名空间的引用:
using System.Runtime.InteropServices; using System.Reflection;
2. 声明 API 函数
[DllImport("user32.dll")] static extern IntPtr LoadCursorFromFile( string fileName );
3. 在Form_Load 事件中,加载自定义光标
private void Form1_Load(object sender, EventArgs e) { Cursor customCursor = new Cursor(Cursor.Current.Handle); IntPtr customCursorHandle = LoadCursorFromFile("你的自定义鼠标指针的路径"); customCursor.GetType().InvokeMember("handle", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetField, null, customCursor, new object[] { customCursorHandle }); this.Cursor = customCursor; }