将外部程序嵌入qt

网上搜了下实现方案,原理很简单,就是通过获取外部窗口的id,转换为QWindow嵌入当前qt窗口。于是问题就变成了如何查看外部程序的窗口属性,推荐使用spy++,小巧实用,放链接:
链接: https://pan.baidu.com/s/15cZuL1OPITdM1OwnTkBSww 提取码: diax
以记事本程序为例,可以看到窗口标题为"无标题 - 记事本",类型为Notepad。
spy.png
代码很简单,就不过多介绍了。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>
#include <QDir>
#include "windows.h"
#include "stdio.h"
#include <QDebug>
#include <QMainWindow>
#include <QWindow>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //启动
    QDir::setCurrent("C:\\windows\\system32");
    QProcess *pro = new QProcess(this);
    pro->start("notepad.exe");
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::integrate()
{
    QString Name = "无标题 - 记事本";
    QString Name2 = "MainWindow";
    WId hwnd = (WId)FindWindow(L"Notepad",(LPCTSTR)Name.unicode());
    WId m_hWnd = (WId)FindWindow(L"Qt5QWindowIcon",(LPCTSTR)Name2.unicode());
    qDebug()<<hwnd;
    qDebug()<<m_hWnd;
    //嵌入
    if (hwnd > 0)
    {
        QWindow *m_window;
        m_window = QWindow::fromWinId(WId(hwnd));
        QWidget *m_widget;
        m_widget = QWidget::createWindowContainer(m_window,this);
        setCentralWidget(m_widget);
    }
}

void MainWindow::on_pushButton_clicked()
{
    integrate();
}
# qt 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×