c#使用win32api实现获取光标位置
作者:hebedich 时间:2022-05-09 10:59:33
方法一:需要调用win32api,winform、wpf通用
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
方法二:通过调用Win32 API设置鼠标位置,实现移到指定位置,模仿并实现鼠标点击动作,并回到鼠标原先位置的方法,代码如下:
//获取屏幕
int width = (int)SystemParameters.PrimaryScreenWidth;//得到屏幕整体宽度
int height = (int)SystemParameters.PrimaryScreenHeight;//得到屏幕整体高度
//获取鼠标初始位置,相对屏幕的绝对位置
System.Drawing.Point p = new System.Drawing.Point();
ApiHelper.GetCursorPos(out p);
if (width != 0) p.X = 65535 * p.X / width;
if (height != 0) p.Y = 65535 * p.Y / height;
//设置移动的位置坐标
int dy = 100;
int dx = 100;
dx = (int)(dx * 65535 / width);
dy = (int)(dy * 65535 / height);
//移到指定位置
ApiHelper.mouse_event((int)(MouseEventFlag.MOUSEEVENTF_MOVE | MouseEventFlag.MOUSEEVENTF_ABSOLUTE), dx, dy, 0, IntPtr.Zero);//移动到需要点击的位置
//完成一次点击
ApiHelper.mouse_event((int)(MouseEventFlag.MOUSEEVENTF_LEFTDOWN), 0, 0, 0, IntPtr.Zero);
ApiHelper.mouse_event((int)(MouseEventFlag.MOUSEEVENTF_LEFTUP), 0, 0, 0, IntPtr.Zero);//
//单击可以写为
ApiHelper.mouse_event((int)(MouseEventFlag.MOUSEEVENTF_LEFTDOWN | MouseEventFlag.MOUSEEVENTF_LEFTUP), 0, 0, 0, IntPtr.Zero);
//双击则再重复单击方法
//回到初始位置
ApiHelper.mouse_event((int)(MouseEventFlag.MOUSEEVENTF_MOVE | MouseEventFlag.MOUSEEVENTF_ABSOLUTE), p.X, p.Y, 0, IntPtr.Zero);//移动到需要点击的位置
代码中ApiHelper为作者封装的Win32 API方法,读者可以通过api精灵等软件查询api函数,自行实现封装。
标签:c#,win32api,获取光标位置
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解MyBatis-Plus Wrapper条件构造器查询大全
2023-09-05 08:55:52
新手初学Java集合框架
2022-10-06 03:01:51
Java 切割字符串的几种方式集合
2023-12-19 00:02:04
![](https://img.aspxhome.com/file/2023/9/84329_0s.jpg)
Java基础之Thymeleaf的简单使用
2023-08-24 19:00:22
![](https://img.aspxhome.com/file/2023/6/58086_0s.png)
Java程序员面试中的多线程问题总结
2021-12-12 07:48:33
详谈异步log4j2中的location信息打印问题
2023-12-22 05:53:31
Java 在PDF中添加条形码的两种方法
2023-05-29 19:10:23
![](https://img.aspxhome.com/file/2023/0/80930_0s.png)
IDEA解决maven包冲突easypoi NoClassDefFoundError的问题
2023-10-16 14:51:38
![](https://img.aspxhome.com/file/2023/2/61812_0s.png)
springboot+springsecurity如何实现动态url细粒度权限认证
2023-10-01 04:06:39
![](https://img.aspxhome.com/file/2023/0/64130_0s.png)
Android ViewPager实现无限循环轮播广告位Banner效果
2023-06-26 22:02:29
![](https://img.aspxhome.com/file/2023/3/93253_0s.jpg)
Java利用栈实现简易计算器功能
2022-05-08 16:57:25
SpringBoot如何通过Feign调用传递Header中参数
2023-11-24 21:39:29
java 动态生成SQL的实例讲解
2021-05-25 18:26:06
sqlite数据库的介绍与java操作sqlite的实例讲解
2023-05-09 03:07:40
springboot 无法自动装配的问题
2021-05-29 06:14:30
![](https://img.aspxhome.com/file/2023/8/81888_0s.png)
Spring Boot使用Allatori代码混淆的方法
2023-11-24 16:34:55
![](https://img.aspxhome.com/file/2023/0/59080_0s.jpg)
Java使用OpenCV3.2实现视频读取与播放
2023-11-23 06:14:02
![](https://img.aspxhome.com/file/2023/5/82425_0s.jpg)
Java流程控制顺序结构原理解析
2022-09-13 14:14:03
![](https://img.aspxhome.com/file/2023/1/79311_0s.png)
SpringBoot整合ShardingSphere的示例代码
2022-08-17 22:52:28
深入了解c#多线程编程
2022-08-14 15:13:58