一、流程
1、调用Window接口模拟鼠标,键盘事件
POINT point;
LPPOINT lpppoint = &point;
GetCursorPos(lpppoint);//获取鼠标位置
SetCursorPos(point.x, point.y);//设置鼠标位置
//鼠标左键按下
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, point.x, point.y, 0, 0);
//获取键盘状态
short s = GetKeyState(VK_SHIFT);
//按键按下,注意:字母只有大写生效且会转换为小写,大写:先按shift再写字母,或按下capital键
//微软码表:https://learn.microsoft.com/zh-cn/windows/win32/inputdev/virtual-key-codes
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
2、嵌入外部程序
使用VS自带的调用Window接口获取外部程序
打开VS -》工具-》Spy++(+)
键盘【Ctrl + F】-》查找窗口-》将工具拖拽到已打开的程序窗口,获取到标题、类,创建窗口时使用
3、将鼠标键盘事件响应到具体坐标位置
通过与定时器配合使用可以实现一些固定的自动化操作(*^▽^*)
二、程序
==========================ExternalAPPControl.h=======================
#pragma once
#include <QtWidgets/QWidget>
#include "ui_ExternalAPPControl.h"
#include <Windows.h>
#include <WinUser.h>
#include <windef.h>
#include <qdebug.h>
#include <qprocess.h>
#include <qwindow.h>
#include <qsettings.h>
enum LANGUAGE
{
ENGLISH = 0,
CHINA
};
enum CAPITAL
{
SMALL = 0,
BIG
};
class ExternalAPPControl : public QWidget
{
Q_OBJECT
public:
ExternalAPPControl(QWidget *parent = nullptr);
~ExternalAPPControl();
private slots:
void slotBtnClicked();
private:
void loadSettings();
void loadExternalApp();
private:
Ui::ExternalAPPControlClass ui;
QProcess* m_process;
QWindow* m_window;
PROCESS_INFORMATION m_pi;
};
==========================ExternalAPPControl.cpp=====================
#include "ExternalAPPControl.h"
ExternalAPPControl::ExternalAPPControl(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
loadExternalApp();
QObject::connect(ui.pushButton, &QPushButton::clicked, this, &ExternalAPPControl::slotBtnClicked);
}
ExternalAPPControl::~ExternalAPPControl()
{
//退出进程
DWORD exitCode;
TerminateProcess(m_pi.hProcess, exitCode);
}
void ExternalAPPControl::slotBtnClicked()
{
POINT point;
LPPOINT lpppoint = &point;
GetCursorPos(lpppoint);
//
QPoint lineEditPos = ui.lineEdit->mapToGlobal(QPoint(0, 0));
qDebug() << "lineEditPos:" << lineEditPos;
//
SetCursorPos(lineEditPos.x(), lineEditPos.y());
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, lineEditPos.x(), lineEditPos.y(), 0, 0);
//获取键盘状态
short s = GetKeyState(VK_SHIFT);
if (s == 0)//初始化一下输入法
{
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
qDebug() << "english";
}
else if (s == 1)
{
}
//默认中文输入法
//大写
keybd_event(VK_CAPITAL, 0, 0, 0);
keybd_event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CAPITAL, 0, 0, 0);
keybd_event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0);
keybd_event(65, 0, 0, 0);
keybd_event(65, 0, KEYEVENTF_KEYUP, 0);
//小写
keybd_event(VK_CAPITAL, 0, 0, 0);
keybd_event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0);
keybd_event(65, 0, 0, 0);
keybd_event(65, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CAPITAL, 0, 0, 0);
keybd_event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0);
//鼠标归位
SetCursorPos(point.x, point.y);
}
void ExternalAPPControl::loadSettings()
{
}
void ExternalAPPControl::loadExternalApp()
{
QString cmd = "C:/SoftWare/Notepad++/notepad++.exe";
STARTUPINFO si = { sizeof(si) };
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = true;
bool ret = CreateProcess(
NULL,
(LPWSTR)cmd.toStdWString().c_str(),
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&m_pi);
Sleep(1000);
WId wid = (WId)FindWindow(L"Notepad++", L"新文件 1 - Notepad++");//获取窗口句柄,类名,标题
m_window = QWindow::fromWinId(wid);
if (m_window)
{
QWidget* widget = QWidget::createWindowContainer(m_window, this, Qt::Widget);
ui.widget_externalAPP->layout()->addWidget(widget);
}
else
{
qDebug() << "add external app error";
}
}