C#实现绘制鼠标的示例代码
作者:芝麻粒儿 时间:2023-06-11 04:40:54
实践过程
效果
代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int _X, _Y;
[StructLayout(LayoutKind.Sequential)]
private struct ICONINFO
{
public bool fIcon;
public Int32 xHotspot;
public Int32 yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
[StructLayout(LayoutKind.Sequential)]
private struct CURSORINFO
{
public Int32 cbSize;
public Int32 flags;
public IntPtr hCursor;
public Point ptScreenPos;
}
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
private static extern int GetSystemMetrics(int mVal);
[DllImport("user32.dll", EntryPoint = "GetCursorInfo")]
private static extern bool GetCursorInfo(ref CURSORINFO cInfo);
[DllImport("user32.dll", EntryPoint = "CopyIcon")]
private static extern IntPtr CopyIcon(IntPtr hIcon);
[DllImport("user32.dll", EntryPoint = "GetIconInfo")]
private static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO iInfo);
private void Form1_Load(object sender, EventArgs e)
{
}
private Bitmap CaptureCursor(ref int _CX, ref int _CY)
{
IntPtr _Icon;
CURSORINFO _CursorInfo = new CURSORINFO();
ICONINFO _IconInfo;
_CursorInfo.cbSize = Marshal.SizeOf(_CursorInfo);
if (GetCursorInfo(ref _CursorInfo))
{
if (_CursorInfo.flags == 0x00000001)
{
_Icon = CopyIcon(_CursorInfo.hCursor);
if (GetIconInfo(_Icon, out _IconInfo))
{
_CX = _CursorInfo.ptScreenPos.X - _IconInfo.xHotspot;
_CY = _CursorInfo.ptScreenPos.Y - _IconInfo.yHotspot;
return Icon.FromHandle(_Icon).ToBitmap();
}
}
}
return null;
}
private void button1_Click(object sender, EventArgs e)
{
int x = Control.MousePosition.X;
int y = Control.MousePosition.Y;
pictureBox1.Image = CaptureCursor(ref x, ref y);
}
}
来源:https://zhima.blog.csdn.net/article/details/128102100
标签:C#,鼠标
0
投稿
猜你喜欢
Spring Security内置过滤器的维护方法
2022-07-30 18:10:16
Springboot项目中使用redis的配置详解
2021-11-26 03:43:44
C#使用Tesseract进行Ocr识别的方法实现
2022-12-15 06:54:23
使用Java将字符串在ISO-8859-1和UTF-8之间相互转换
2022-09-15 15:05:16
最值得Java开发者收藏的网站
2022-03-09 15:57:08
Android EditText实现关键词批量搜索示例
2022-12-09 06:20:21
C#中事务处理和非事务处理方法实例分析
2023-12-23 08:09:13
Android编程中出现The connection to adb is down问题的解决方法
2022-06-27 17:46:07
解析C# 程序结构
2021-11-15 05:22:59
C#中类与接口的区别个人总结
2023-05-08 17:29:40
Android ListView弹性效果的实现方法
2023-08-07 19:06:40
SpringMVC使用ResponseEntity实现文件上传下载
2023-08-20 02:10:58
Spark JDBC操作MySQL方式详细讲解
2021-05-24 12:41:51
springboot连接不上redis的三种解决办法
2022-03-21 20:57:23
VS2022调试通过海康摄像头烟火识别SDK的实现
2022-09-04 05:34:26
JAVA中常见异常类
2021-11-09 09:47:20
Java打印斐波那契前N项的实现示例
2022-12-03 21:18:10
Java equals 方法与hashcode 方法的深入解析
2022-03-05 19:20:15
Android之日期时间选择控件DatePicker和TimePicker实例
2023-08-02 14:57:26
探讨Java中函数是值传递还是引用传递问题
2021-07-25 19:34:44