解决C# winForm自定义鼠标样式的两种实现方法详解
程序员文章站
2023-12-21 09:59:40
第一种:(调用系统api)首先引入两个命名空间复制代码 代码如下:using system.runtime.interopservices;using system.ref...
第一种:(调用系统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));
}
首先引入两个命名空间
复制代码 代码如下:
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));
}