写在前面
用WinForm做RPA项目时经常需要模拟鼠标操作,通过调用Windows Api 可以实现控制鼠标的移动、点击以及滚轮滚动,做到跟人工一样的操作。
代码实现
public static class MouseKeyController
{
[DllImport("user32")]
private static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
private static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
private static extern int PostMessage(IntPtr hWnd, int Msg, Keys wParam, int lParam);
#region 屏幕分辨率和缩放百分比
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr ptr);
[DllImport("gdi32.dll")]
private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
private static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
const int HORZRES = 8;
const int VERTRES = 10;
const int LOGPIXELSX = 88;
const int LOGPIXELSY = 90;
const int DESKTOPVERTRES = 117;
const int DESKTOPHORZRES = 118;
/// <summary>
/// 获取真实屏幕分辨率大小
/// </summary>
public static Size DESKTOP
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
Size size = new Size();
size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
ReleaseDC(IntPtr.Zero, hdc);
return size;
}
}
#endregion
//移动鼠标
const int MOUSEEVENTF_MOVE = 0x0001;
//模拟鼠标左键按下
const int MOUSEEVENTF_LEFTDOWN = 0x0002;
//模拟鼠标左键抬起
const int MOUSEEVENTF_LEFTUP = 0x0004;
//模拟鼠标右键按下
const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
//模拟鼠标右键抬起
const int MOUSEEVENTF_RIGHTUP = 0x0010;
//模拟鼠标中键按下
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
//模拟鼠标中键抬起
const int MOUSEEVENTF_MIDDLEUP = 0x0040;
//标示是否采用绝对坐标
const int MOUSEEVENTF_ABSOLUTE = 0x8000;
//模拟鼠标滚轮滚动操作,必须配合dwData参数
const int MOUSEEVENTF_WHEEL = 0x0800;
private static int screenWidth = 0;
private static int screenHeight = 0;
public static void Init()
{
screenWidth = Screen.PrimaryScreen.Bounds.Width;
screenHeight = Screen.PrimaryScreen.Bounds.Height;
}
public static void MoveMouseWheel(int offset)
{
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, offset, 0);//鼠标滚动,使界面向下滚动offset的高度
}
public static void MoveMousePoint(int x, int y)
{
mouse_event(MOUSEEVENTF_MOVE, x, y, 0, 0);//相对当前鼠标位置x轴和y轴分别移动50像素
}
public static void SetMousePoint(int x, int y)
{
// 需要说明的是,如果没有使用MOUSEEVENTF_ABSOLUTE,函数默认的是相对于鼠标当前位置的点,如果dx,和dy,用0,0表示,这函数认为是当前鼠标所在的点。
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x * 65536 / screenWidth, y * 65536 / screenHeight, 0, 0);
}
public static void MouseLeftDown()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
}
public static void MouseLeftUp()
{
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
public static void MouseLeftClick()
{
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
public static void MouseRightClick()
{
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
}
}
总结
原文地址:https://blog.csdn.net/rjcql/article/details/134539976
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_10947.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。