如果你需要通过程序获取鼠标的形状,可以按照以下步骤操作:
1.使用GetCursorInfo函数获取当前鼠标信息:
[DllImport("user32.dll")]
public static extern bool GetCursorInfo(out CURSORINFO pci);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
public struct CURSORINFO
{
public int cbSize;
public int flags;
public IntPtr hCursor;
public POINT ptScreenPos;
}
2.调用GetIconInfo函数获取鼠标指针的信息和位图:
[DllImport("user32.dll")]
public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
[StructLayout(LayoutKind.Sequential)]
public struct BITMAP
{
public int bmType;
public int bmWidth;
public int bmHeight;
public int bmWidthBytes;
public ushort bmPlanes;
public ushort bmBitsPixel;
public IntPtr bmBits;
}
[StructLayout(LayoutKind.Sequential)]
public struct ICONINFO
{
public bool fIcon;
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
3.将鼠标图标转换为位图:
Bitmap bmp = null;
if (iconInfo.hbmColor != IntPtr.Zero)
{
bmp = Bitmap.FromHbitmap(iconInfo.hbmColor);
}
else
{
bmp = Bitmap.FromHbitmap(iconInfo.hbmMask);
}
4.使用Graphics对象的CopyFromScreen方法获取鼠标指针的形状:
Rectangle cursorBounds = new Rectangle(cursInfo.ptScreenPos.x, cursInfo.ptScreenPos.y, bmp.Width, bmp.Height);
Graphics graphics = Graphics.FromImage(bmp);
graphics.CopyFromScreen(cursorBounds.Left, cursorBounds.Top, 0, 0, cursorBounds.Size);
以上的是思路和方法,下面是一个简单的源码实现.
Graphics myGraphics = this.CreateGraphics();
Cursor.Draw(myGraphics, new Rectangle(e.X, e.Y, 10, 10));
效果如下: