当前位置: 首页 > news >正文

QT-窗口嵌入外部exe

窗口类:

#pragma once
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QProcess>
#include <QTimer>
#include <QDebug>
#include <Windows.h>
#include <QWindow>
#include <QResizeEvent>class ExternalAppWidget : public QWidget 
{Q_OBJECTpublic:ExternalAppWidget(QString exePath, QString strTitle, QWidget *parent = nullptr);~ExternalAppWidget();private:bool checkAppStartDone(QString strAppTitle);void killProcessByExeName(QString processName);protected:void resizeEvent(QResizeEvent *event);private:QVBoxLayout *m_pLayout;QProcess	*m_pProcess;QWidget		*m_pExternalWindow;QString		m_strExePath;QString		m_strExeTile;
};
#include "externalAppWidget.h"
#include <QFileInfo>
#include <QDir>
#include <QThread>
#include <QMessageBox>
#include <QTime>// 不阻塞定时器
struct sTimeout
{QTime time;uint32_t interval;void start(uint32_t t){interval = t;time.start();};bool isTimeOut(){return time.elapsed() > interval;};
};ExternalAppWidget::ExternalAppWidget(QString exePath, QString strTitle,QWidget *parent) :QWidget(parent),m_pExternalWindow(nullptr), m_strExePath(exePath), m_strExeTile(strTitle)
{QFileInfo fileInfo(exePath);killProcessByExeName(fileInfo.fileName());m_pLayout = new QVBoxLayout(this);// 启动外部程序m_pProcess = new QProcess(this);m_pProcess->setWorkingDirectory(fileInfo.absolutePath());m_pProcess->start(exePath); m_pProcess->waitForStarted();checkAppStartDone(m_strExeTile);// 获取外部程序的窗口句柄HWND hwnd = FindWindowW(NULL, reinterpret_cast<LPCWSTR>(m_strExeTile.utf16()));if (hwnd){// 修改窗口样式以去掉标题栏LONG style = GetWindowLong(hwnd, GWL_STYLE);style &= ~(WS_CAPTION | WS_THICKFRAME); // 移除标题栏和边框SetWindowLong(hwnd, GWL_STYLE, style | WS_POPUP); // 设置为无边框的弹出窗口ShowWindow(hwnd, SW_SHOW);WId wid = (WId)hwnd;QWindow *pQwindow;pQwindow = QWindow::fromWinId(wid);// 创建窗口容器m_pExternalWindow = QWidget::createWindowContainer(pQwindow, this);m_pExternalWindow->setAttribute(Qt::WA_NativeWindow);m_pExternalWindow->setMinimumSize(100, 100);m_pLayout->addWidget(m_pExternalWindow);m_pLayout->setMargin(0);		}else{//QMessageBox::information(0, "Tip", QString("未找到窗口:%1").arg(m_strExeTile), QMessageBox::Yes);qDebug() << "Failed to find external application window.";}setLayout(m_pLayout);
}ExternalAppWidget::~ExternalAppWidget()
{QFileInfo fileInfo(m_strExePath);killProcessByExeName(fileInfo.fileName());
}bool ExternalAppWidget::checkAppStartDone(QString strAppTitle)
{// 循环检查窗口是否存在QString windowTitle = strAppTitle;HWND hwnd = nullptr;int nStep = 0;sTimeout timeout;while (true){Sleep(5);switch (nStep){case 0:{timeout.start(30*1000);nStep = 1;}break;case 1:{hwnd = FindWindowW(NULL, reinterpret_cast<LPCWSTR>(windowTitle.utf16()));if (hwnd != NULL){return true; // 找到窗口,退出循环}else if (timeout.isTimeOut()){return false;}}break;default:break;}}return false;}void ExternalAppWidget::killProcessByExeName( QString processName)
{QStringList params;params << "-f" << "-im" << processName;QProcess process;process.start("taskkill", params);process.waitForFinished();
}void ExternalAppWidget::resizeEvent(QResizeEvent *event) {QWidget::resizeEvent(event);if (m_pExternalWindow) {m_pExternalWindow->resize(event->size());}
}

应用层调用

void cWinDebug::initRemoteDesktopWidget()
{for (int i = 1; i <= DEV_MAX_COUNT; i++){QString strTitleName = GET_PARAM_STRING(P_EXE_TITLE_NAME(i));QString strTabName = GET_PARAM_STRING(P_TAB_NAME(i));QString strExePath = GET_PARAM_STRING(P_EXE_PATH(i));if (strExePath.isEmpty() || strTitleName.isEmpty() || strTabName.isEmpty()){continue;}ExternalAppWidget* e = new ExternalAppWidget(strExePath, strTitleName, this);e->showMaximized();ui.tabWidgetRemote->insertTab(0, (QWidget*)e, strTabName);}ui.tabWidgetRemote->setCurrentIndex(0);
}

http://www.lryc.cn/news/515842.html

相关文章:

  • C#中使用系统默认应用程序打开文件
  • 如何在 Ubuntu 22.04 上配置 Logrotate 高级教程
  • java项目之校园管理系统的设计与实现(源码+文档)
  • 关于 webservice 日志中 源IP是node IP的问题,是否能解决换成 真实的客户端IP呢
  • Serializable接口
  • 如何操作github,gitee,gitcode三个git平台建立镜像仓库机制,这样便于维护项目只需要维护一个平台仓库地址的即可-优雅草央千澈
  • 【HDU】1089 A+B for Input-Output Practice (I)
  • lua库介绍:数据处理与操作工具库 - leo
  • 逆向入门(1)C篇-正儿巴经的第1个实验
  • vue数据请求通用方案:axios的options都有哪些值
  • 使用R语言绘制标准的中国地图和世界地图
  • 【PyTorch】迁移学习、数据增强
  • Lucas-Kanade光流法详解
  • python多张图片生成/合成gif
  • iptable限制多个端口出站
  • springmvc--请求参数的绑定
  • Redis查询缓存
  • 双馈风电DFIG并网系统次转子侧变流器RSC抑制策略研究基于LADRC和重复控制的方法
  • 国产编辑器EverEdit - 使用技巧:变量重命名的一种简单替代方法
  • 使用SSH建立内网穿透,能够访问内网的web服务器
  • JWT认证实战
  • 计算机网络 (23)IP层转发分组的过程
  • 权限管理的方法
  • 【郑大主办、ACM出版、EI稳定检索】第四届密码学、网络安全与通信技术国际会议 (CNSCT 2025)
  • 48小时,搭建一个设备巡检报修系统
  • 基于Redisson实现重入锁
  • Java文件操作的简单示例
  • 删除与增加特定行
  • 动态规划六——两个数组的dp问题
  • 项目优化之策略模式