手机
当前位置:查字典教程网 >编程开发 >C#教程 >解决C# winForm自定义鼠标样式的两种实现方法详解
解决C# winForm自定义鼠标样式的两种实现方法详解
摘要:第一种:(调用系统API)首先引入两个命名空间复制代码代码如下:usingSystem.Runtime.InteropServices;us...

第一种:(调用系统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(0, 0, 0, 0));

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(0, 0));

}

【解决C# winForm自定义鼠标样式的两种实现方法详解】相关文章:

BarCode条形码基于C# GDI+ 的实现方法详解

解析C#彩色图像灰度化算法的实现代码详解

解读在C#中winform程序响应键盘事件的详解

string类的使用方法详解

两路归并的数组与链表的实现方法

WinForm子窗体访问父窗体控件的实现方法

解决用Aspose.Words,在word文档中创建表格的实现方法

解决C# 截取当前程序窗口指定位置截图的实现方法

共享锁using范围的实现方法

解析C#中@符号的几种使用方法详解

精品推荐
分类导航