[C#]自定义鼠标样式
工作中经常需要自定义鼠标样式来更丰富更形象地交互操作,系统自带的样式单一,难免会有些审美疲劳.取而代之,用一些比较形象的图标来定义鼠标样式,用户体验上就更加形象了.
自定义鼠标样式
/// <summary>
/// 自定义鼠标样式
/// </summary>
/// <param name="cursor">一张背景透明的图片,格式为(png/gif)</param>
/// <param name="hotPoint"></param>
public static System.Windows.Forms.Cursor SetCursor(System.Drawing.Bitmap cursor, System.Drawing.Point hotPoint)
//设置鼠标样式
{
int hotX = hotPoint.X;
int hotY = hotPoint.Y;
System.Drawing.Bitmap myNewCursor = new System.Drawing.Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myNewCursor);
g.Clear(System.Drawing.Color.FromArgb(0, 0, 0, 0));
g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width, cursor.Height);
System.Windows.Forms.Cursor newCursor = new System.Windows.Forms.Cursor(myNewCursor.GetHicon());
g.Dispose();
myNewCursor.Dispose();
return newCursor;
}
/// 自定义鼠标样式
/// </summary>
/// <param name="cursor">一张背景透明的图片,格式为(png/gif)</param>
/// <param name="hotPoint"></param>
public static System.Windows.Forms.Cursor SetCursor(System.Drawing.Bitmap cursor, System.Drawing.Point hotPoint)
//设置鼠标样式
{
int hotX = hotPoint.X;
int hotY = hotPoint.Y;
System.Drawing.Bitmap myNewCursor = new System.Drawing.Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myNewCursor);
g.Clear(System.Drawing.Color.FromArgb(0, 0, 0, 0));
g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width, cursor.Height);
System.Windows.Forms.Cursor newCursor = new System.Windows.Forms.Cursor(myNewCursor.GetHicon());
g.Dispose();
myNewCursor.Dispose();
return newCursor;
}