QT:qt5调用打开exe程序并获取调用按钮控件实例2025.5.7

为实现在 VS2015 的 Qt 开发环境下打开外部 exe,列出其界面按钮控件的序号与文本名,然后点击包含特定文本的按钮控件。以下是更新后的代码:

cpp 复制代码
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
#include <windows.h>
#include <QString>
#include <vector>

// 查找窗口句柄
HWND findWindowByTitle(const QString& title) {
    return FindWindow(nullptr, title.toStdWString().c_str());
}

// 枚举子窗口并收集按钮句柄
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) {
    wchar_t className[256];
    GetClassNameW(hwnd, className, sizeof(className) / sizeof(wchar_t));
    if (wcscmp(className, L"Button") == 0) {
        std::vector<HWND>* buttons = reinterpret_cast<std::vector<HWND>*>(lParam);
        buttons->push_back(hwnd);
    }
    return TRUE;
}

// 模拟鼠标点击
void simulateClick(HWND hwnd, int x, int y) {
    PostMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x, y));
    PostMessage(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // 启动外部 exe
    QProcess process;
    process.start("C:\\Path\\To\\YourProgram.exe");

    if (!process.waitForStarted()) {
        qDebug() << "Failed to start the external program.";
        return -1;
    }

    // 等待一段时间,让外部程序的窗口完全打开
    QThread::sleep(5);

    // 查找外部程序的主窗口
    HWND mainWindow = findWindowByTitle("Your Program Title");
    if (mainWindow == nullptr) {
        qDebug() << "Could not find the main window of the external program.";
        return -1;
    }

    // 枚举子窗口,收集按钮句柄
    std::vector<HWND> buttons;
    EnumChildWindows(mainWindow, EnumChildProc, reinterpret_cast<LPARAM>(&buttons));

    if (buttons.size() < 3) {
        qDebug() << "There are not enough buttons in the external program window.";
        return -1;
    }

    // 获取第 3 个按钮的句柄
    HWND targetButton = buttons[2];

    // 获取目标按钮的位置和大小
    RECT rect;
    GetWindowRect(targetButton, &rect);

    // 计算按钮中心位置
    int centerX = (rect.left + rect.right) / 2;
    int centerY = (rect.top + rect.bottom) / 2;

    // 模拟点击目标按钮
    simulateClick(targetButton, centerX - rect.left, centerY - rect.top);

    return a.exec();
}    

代码解释

  1. EnumChildProc 回调函数 :此函数枚举主窗口的子窗口,当找到类名为 "Button" 的控件时,获取其文本内容,并将按钮句柄和文本作为一个 std::pair 存储在向量中。
  2. 主函数流程
    • 使用 QProcess 启动外部 exe。
    • 等待一段时间,确保外部程序的窗口完全打开。
    • 查找外部程序的主窗口。
    • 调用 EnumChildWindows 枚举子窗口,收集按钮句柄和文本。
    • 列出所有按钮的序号和文本。
    • 查找包含特定文本的按钮。
    • 若找到目标按钮,计算其中心位置并模拟点击操作。

注意事项

  • 需将 "C:\\Path\\To\\YourProgram.exe" 替换为实际的外部可执行文件路径。
  • 需将 "Your Program Title" 替换为实际的外部程序窗口标题。
  • 需将 "Your Target Button Text" 替换为要点击的按钮的特定文本。
  • 等待时间(QThread::sleep(5))可根据外部程序的启动速度进行调整。
相关推荐
全栈工程师修炼指南3 分钟前
奇技淫巧 | 巧用阿里云免费 ESA:获取用户真实IP地址与地理位置
数据库·阿里云·云计算
草莓熊Lotso26 分钟前
C++ 二叉搜索树(BST)完全指南:从概念原理、核心操作到底层实现
java·运维·开发语言·c++·人工智能·经验分享·c++进阶
碰大点33 分钟前
数据库“Driver not loaded“错误,单例模式重构方案
数据库·sql·qt·单例模式·重构
oliveira-time35 分钟前
单例模式中的饿汉式
java·开发语言
武子康38 分钟前
Java-173 Neo4j + Spring Boot 实战:从 Driver 到 Repository 的整合与踩坑
java·数据库·spring boot·后端·spring·nosql·neo4j
哥哥还在IT中39 分钟前
深入理解MySQL事务隔离级别与锁机制(从ACID到MVCC的全面解析)
数据库·mysql
Go away, devil2 小时前
Java-----集合
java·开发语言
李慕婉学姐2 小时前
Springboot智慧旅游管理系统6w63eon8(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·旅游
爱吃猫的鱼星3 小时前
SQL 分类
数据库·oracle
VBA63374 小时前
VBA即用型代码手册:利用函数保存为PDF文件UseFunctionSaveAsPDF
开发语言