600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > QT模拟鼠标事件 实现点击双击移动拖拽等

QT模拟鼠标事件 实现点击双击移动拖拽等

时间:2020-05-20 02:47:08

相关推荐

QT模拟鼠标事件 实现点击双击移动拖拽等

之前有涉及过一个远程桌面控制的项目,需要传一些指令给远程的电脑,实现简单的桌面点击、移动、拖拉等功能,当时没有时间实现得很好,今天又研究了一下,故此记录。

虽然我用的是QT,但核心涉及的还是系统的API,所以其他平台应该也是一样的。

废话不多说,直接上代码。

mouseacts.h:

#ifndef MOUSEACTS_H#define MOUSEACTS_H#include <QWidget>#include "Windows.h"//这是引入Windows操作系统的API#include "WinUser.h"#include <QDesktopWidget>#include <QApplication>#include <QDebug>#pragma comment(lib, "User32.lib")//这个一定要有,不然编译不过class MouseActs : public QWidget{Q_OBJECTpublic:MouseActs(QWidget *parent = nullptr);public slots:void getClickedPos() ;//获取鼠标点击处的全局坐标值(屏幕坐标)static void doPressAct(int,int,int,int,int);//执行鼠标动作。};#endif // MOUSEACTS_H

mouseacts.cpp

#include "mouseacts.h"MouseActs::MouseActs(QWidget *parent): QWidget (parent){}//获取鼠标点击处的全局坐标值(屏幕坐标)void MouseActs::getClickedPos (){GetCursorPos(&pos);}//执行鼠标动作void MouseActs::doPressAct(int x,int y,int x1,int y1,int type_id){SetCursorPos(x,y);//必须要先设置一下鼠标的初始位置,不然会导致不成功//获取屏幕尺寸大小QDesktopWidget * desktop = QApplication::desktop();QRect screen_rect = desktop->screenGeometry(0);int desktop_width = screen_rect.width();int desktop_height = screen_rect.height();qDebug()<<"desktop_width"<<desktop_width;qDebug()<<"desktop_height"<<desktop_height;if(type_id==1){//鼠标左键单击mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,x, y, 0, 0);//按下mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,x, y, 0, 0);//松开}else if(type_id==2){//鼠标左键双击mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,x, y, 0, 0);mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,x, y, 0, 0);mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,x, y, 0, 0);mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,x, y, 0, 0);}else if(type_id==3){//鼠标右键单击mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN,x, y, 0, 0);mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP,x, y, 0, 0);}else if(type_id==4){//鼠标右键双击mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN,x, y, 0, 0);mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP,x, y, 0, 0);mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN,x, y, 0, 0);mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP,x, y, 0, 0);}else if(type_id==5){//鼠标移动mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE ,x * 65536 / desktop_width,y * 65536 / desktop_height,0,GetMessageExtraInfo());}else if(type_id==6){//鼠标拖拽mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,x, y, 0, 0);mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE ,x1 * 65536 / desktop_width,y1 * 65536 / desktop_height,0,GetMessageExtraInfo());mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,x1, y1, 0, 0);qDebug()<<"--"<<x<<","<<y<<"to"<<x1<<","<<y1;}}

这样直接外部调用就可以啦

void MainWidget::on_btn_test_1_clicked(){MouseActs macts;macts.doPressAct(ui->lineEdit->text().toInt(),ui->lineEdit_2->text().toInt(),0,0,1);}void MainWidget::on_btn_test_2_clicked(){MouseActs::doPressAct(ui->lineEdit->text().toInt(),ui->lineEdit_2->text().toInt(),0,0,2);}void MainWidget::on_btn_test_3_clicked(){MouseActs::doPressAct(ui->lineEdit->text().toInt(),ui->lineEdit_2->text().toInt(),0,0,3);}void MainWidget::on_btn_test_4_clicked(){MouseActs::doPressAct(ui->lineEdit->text().toInt(),ui->lineEdit_2->text().toInt(),0,0,4);}void MainWidget::on_btn_test_5_clicked(){MouseActs::doPressAct(ui->lineEdit->text().toInt(),ui->lineEdit_2->text().toInt(),0,0,5);}void MainWidget::on_btn_test_6_clicked(){MouseActs::doPressAct(ui->lineEdit->text().toInt(),ui->lineEdit_2->text().toInt(),1000,1000,6);}

以上代码亲测有效,有参考这个文章,感兴趣可以去看一下。参考一

功能实现主要是doPressAct这个接口,根据type_id来判断走那段代码,以实现左右键单双击,移动和拖拽

好的,接下来讲一下涉及的三个接口。

1.GetCursorPos(&pos);获取鼠标位置,这个我没有用到,不过其实在qt中,直接调用this->pos()就可以了。

2.SetCursorPos(x,y);设置鼠标位置,移动的话可以直接用这个

3.mouse_event鼠标事件,核心的实现接口

重点解析一下mouse_event接口

[System.Runtime.InteropServices.DllImport(“user32”)] private static

extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int

dwExtraInfo);

参数 意义 dwFlags Long,下表中标志之一或它们的组合 dx,dy

Long,根据MOUSEEVENTF_ABSOLUTE标志,指定x,y方向的绝对位置或相对位置 cButtons Long,没有使用

dwExtraInfo Long,没有使用

dwFlags常数 意义

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; 标示是否采用绝对坐标

根据不同的dwFlags标志,以及dx、dy的设置,基本可以实现鼠标的移动、点击、双击、右键单击、中键单击等等功能,详情可以参考我的代码。

这里再重点说一下移动的情况,也就是MOUSEEVENTF_MOVE 的情况!!

这个的x、y坐标似乎与点击的情况不同,需要特殊处理,详情参考我的代码

这里引用在网上给出的一个解释,虽然我也没有很看懂

模拟鼠标移动的事件中,标志位取值不同,输入坐标的意义也不同。

简单来说就是,添加MOUSEEVENTF_ABSOLUTE标志位表示鼠标移动是通过绝对坐标定位,此时的坐标要通过转换。

光标在屏幕中被分成65535个小块,可以通过如下转换:

复制代码代码如下:

double fx = x *(65535.0f / fScreenWidth);

double fy = y *(65535.0f / fScreenHeight);

若不使用MOUSEEVENTF_ABSOLUTE标志位,则坐标是相对前一坐标的位移。

如上,输入的坐标要经过一些转换运算,但我们首先要知道fScreenWidth和fScreenHeight的宽高,也就是屏幕尺寸的宽高

不同的显示器自然有不同的分辨率,我是通过这样获取的:

//获取屏幕尺寸大小QDesktopWidget * desktop = QApplication::desktop();QRect screen_rect = desktop->screenGeometry(0);//0指的是第一个显示器,多显示器的要另外获取编号int desktop_width = screen_rect.width();int desktop_height = screen_rect.height();qDebug()<<"desktop_width"<<desktop_width;qDebug()<<"desktop_height"<<desktop_height;

至此,简单的模拟鼠标事件控制就实现好了。当然,这都是一步到位的,像有一些移动和拖拽移动的动态效果,可能要通过定时器啊或者其他手段来辅助实现,但基本上就是这些API的功能了。

有什么错误和补充的也可以在评论区告诉我,谢谢。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。