因为光标的标准尺寸是 32x32 像素,但大多数像素是透明的,所以您必须通过检查光标本身来确定“实际”高度。
例如,我将默认光标绘制到这样的位图上:
Bitmap bmp = new Bitmap(Cursor.Current.Size.Width, Cursor.Current.Size.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Red);
Cursor.Current.Draw(g, new Rectangle(new Point(0, 0), bmp.Size));
}
pictureBox1.Image = bmp;
所以你可以清楚地看到光标相对于它的实际大小有多大:
编辑:这是一个示例,它根据光标的“实际”大小在当前光标位置下方绘制一个矩形:
private void button1_Click(object sender, EventArgs e)
{
Rectangle bounds = CursorBounds();
Point pt = new Point(Cursor.Position.X + bounds.Left, Cursor.Position.Y + bounds.Bottom);
ControlPaint.DrawReversibleFrame(new Rectangle(pt, new Size(100, 30)), this.BackColor, FrameStyle.Dashed);
}
private Rectangle CursorBounds()
{
using (Bitmap bmp = new Bitmap(Cursor.Current.Size.Width, Cursor.Current.Size.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Transparent);
Cursor.Current.Draw(g, new Rectangle(new Point(0, 0), bmp.Size));
int xMin = bmp.Width;
int xMax = -1;
int yMin = bmp.Height;
int yMax = -1;
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
if (bmp.GetPixel(x, y).A > 0)
{
xMin = Math.Min(xMin, x);
xMax = Math.Max(xMax, x);
yMin = Math.Min(yMin, y);
yMax = Math.Max(yMax, y);
}
}
}
return new Rectangle(new Point(xMin, yMin), new Size((xMax - xMin) + 1, (yMax - yMin) + 1));
}
}
}
我真的不确定是否有更好的方法来做到这一点...... =\