方法一:需要调用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函数,自行实现封装。
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论