C# 自定义鼠标样式

接上一篇,不调用 API 的方式。

▲ 样式效果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 自定义鼠标样式的两种方法:
https://www.cnblogs.com/tianguook/archive/2010/08/03/1791572.html
感谢支持!