Qt5 模拟鼠标点击

windows官方说明:https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-mouse_event?redirectedfrom=MSDN

参考自:https://www.fearlazy.com/index.php/post/168.html

做一个游戏辅助,使用Windows API鼠标自动在某一个坐标点击。

//头文件
#include<Windows.h>
#pragma comment(lib, "User32.lib")

QDesktopWidget *desktopwidget = QApplication::desktop();
int desktop_width = desktopwidget->width();  //获取屏幕分辨率宽度
int desktop_height = desktopwidget->height();  //获取屏幕分辨率高度
int stronghold_height = desktop_height * 0.68;  //要点击鼠标的位置在屏幕一定比例的位置
int stronghold_width = desktop_width * 0.9;
::mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, stronghold_width * 65535 / 1366, stronghold_height * 65535 / 768,0 ,0);  //移动到指定位置
::mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);  //按下左键
::mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);  //左键抬起