自定义鼠标形状,可以按照以下步骤操作:
1.定义一个Cursor对象并使用LoadCursorFromFile函数从文件加载鼠标图标:
string cursorFile = "custom_cursor.cur";
Cursor customCursor = new Cursor(LoadCursorFromFile(cursorFile));
2.将自定义鼠标图标设置为当前鼠标指针:
this.Cursor = customCursor;
3.在窗体关闭时释放鼠标指针资源:
protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);
if (customCursor != null)
{
customCursor.Dispose();
}
}
除了上面的思路和方法,还可以使用图片
源码如下:
public void SetCursor(Bitmap cursor, Point hotPoint)
{
int hotX = hotPoint.X;
int hotY = hotPoint.Y;
// 这里为什么要 * 2
Bitmap cur = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
Graphics graphics = Graphics.FromImage(cur);
graphics.Clear(Color.FromArgb(0, 0, 0, 0));
graphics.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width, cursor.Height);
this.Cursor = new Cursor(cur.GetHicon());
graphics.Dispose();
cur.Dispose();
}
效果如下: