解决C# winForm自定义鼠标样式的两种实现方法详解
2015-08-06 12:26
731 查看
第一种:(调用系统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));
}
相关文章推荐
- 201508061158_《移动开发之——样式链表》
- 纯CSS3画出小黄人并实现动画效果
- QTabWidget 样式表必知必会
- css选择器优先级判定
- HTML <a> 标签 (修改样式,链接邮箱Email地址)
- WPF 后台读取样式文件
- 网页制作实践步骤三 登录界面
- Ajax清除浏览器js、css、图片缓存的方法
- 几个CSS的黑科技
- 常用css
- css的选择器
- CSS3样式工具箱
- CSS3响应式布局
- alert提示框样式
- CSS重置样式
- CSS选择器的权重计算规则
- 用css改变默认的checkbox样式
- 在引入的css或者js文件后面加参数的作用
- 关于css position 的 static、relative、absolute、fixed、inherit
- CSS3响应式滑动菜单