随笔 - 322,  文章 - 1,  评论 - 87,  阅读 - 257万

第一种:(调用系统API)

首先引入两个命名空间

using System.Runtime.InteropServices;
using System.Reflection;

导入API

[DllImport("user32.dll")]
public static extern IntPtr LoadCursorFromFile(string fileName);
[DllImport(
"user32.dll")]
public static extern IntPtr SetCursor(IntPtr cursorHandle);
[DllImport(
"user32.dll")]

 public static extern uint DestroyCursor(IntPtr cursorHandle);

 接下来使用自己的鼠标样式

private void Form1_Load(object sender, EventArgs e)
        {
            Cursor myCursor 
= new Cursor(Cursor.Current.Handle);
            IntPtr colorCursorHandle 
= LoadCursorFromFile("my.cur");//鼠标图标路径
              myCursor.GetType().InvokeMember("handle", BindingFlags.Public |
            BindingFlags.NonPublic 
| BindingFlags.Instance |
            BindingFlags.SetField, 
null, myCursor,
            
new object[] { colorCursorHandle });
            
this.Cursor = myCursor;
        }

第二种:(不用API方式的,鼠标样式只需要一张背景透明的图片就行了,png或gif格式的)

public void SetCursor(Bitmap cursor, Point hotPoint)
        {
            
int hotX = hotPoint.X;
            
int hotY = hotPoint.Y;
            Bitmap myNewCursor 
= new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
            Graphics g 
= Graphics.FromImage(myNewCursor);
            g.Clear(Color.FromArgb(
0000));
            g.DrawImage(cursor, cursor.Width 
- hotX, cursor.Height - hotY, cursor.Width, 
            cursor.Height);

            
this.Cursor = new Cursor(myNewCursor.GetHicon());
           
            g.Dispose();
            myNewCursor.Dispose();
        }

在你想要改变鼠标样式的事件里头使用这个方法就行了,如:

private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap a
=(Bitmap)Bitmap.FromFile("myCur.png");
            SetCursor(a, 
new Point(00));
        }

posted on   aparche  阅读(5055)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
< 2010年8月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4
5 6 7 8 9 10 11

点击右上角即可分享
微信分享提示