解决C# winForm自定义鼠标样式的两种实现方法详解
来源:互联网 发布:知乎每周精选多久一期 编辑:程序博客网 时间:2024/05/20 15:31
第一种:(调用系统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));
}
0 0
- 解决C# winForm自定义鼠标样式的两种实现方法详解
- C# winForm 自定义鼠标样式的两种方法
- [转]C# winForm 自定义鼠标样式的两种方法
- C__winForm自定义鼠标样式的两种方法
- winform 快捷键的两种实现方法
- unity 改变鼠标样式的两种方法
- WinForm中更改自定义鼠标样式
- C# 加载自定义鼠标样式
- Android中自定义标题栏样式的两种方法
- C# Winform 自定义鼠标拖动窗体类
- 自定义鼠标样式的做法
- C# 自定义鼠标样式(使用图片)
- C# winform程序实现分页的三种方法
- C# WinForm中进度条的实现方法
- C# WinForm国际化实现的简单方法
- ·详解两种C#自动实现DLL(OCX)控件注册的方法
- 详解两种C#自动实现DLL(OCX)控件注册的方法
- 详解两种C#自动实现DLL(OCX)控件注册的方法
- And that’s what the lowest level of the Libevent API does for you
- 活着,就图个随缘自在
- [小技巧] svn: 不能解析 URL
- MFC模态对话框与非模态对话框
- java泛型入门
- 解决C# winForm自定义鼠标样式的两种实现方法详解
- Matlab--文件操作
- Python contextlib——上下文管理器
- 我的ROS入门(二):ubuntu-15.04-desktop-amd64安装ROS
- CodeForces 2A-Winner
- python wmi 学习
- 9.5位操作(四)——解释代码(n&(n-1))==0的具体含义
- 如何知道android应用是第一次运行
- Java中关于String的split(String regex, int limit) 方法