C# 自定义鼠标样式 1
原创
©著作权归作者所有:来自51CTO博客作者覺醒的原创作品,请联系作者获取转载授权,否则将追究法律责任
接上一篇,不调用 API 的方式。
▲ 样式效果
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MouseSt
{
using System.Runtime.InteropServices;
using System.Reflection;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Bitmap a = (Bitmap)Bitmap.FromFile("1.gif");
SetCursor(a, new Point(0, 0));
}
public void SetCursor(Bitmap cursor, Point hotPoint)
{
int hotX = hotPoint.X;
int hotY = hotPoint.Y;
// 这里为什么要 * 2
Bitmap myNewCursor = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
Graphics graphics = Graphics.FromImage(myNewCursor);
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(myNewCursor.GetHicon());
graphics.Dispose();
myNewCursor.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("被点击!");
}
}
}
Bitmap myNewCursor
为什么要 *2
? 没看明白。 因为 Cursor 属性计算定位的是样式图片的中心点位置?
参考:
C# winForm 自定义鼠标样式的两种方法: