本文描述QT6 界面中嵌入第三方程序中的窗口。
第三方程序也是用QT6编写。
QT的编译器版本都为QT6.2.4 MSVC2019 64 bit
第三方程序为QT写的能调试成功。但不是QT写的还不可以。
使用场景:将软件分成几个模块,多人写或者一个人先后写。
1.头文件
#ifndef PARENTWIDGET_H
#define PARENTWIDGET_H
#include <QWidget>
#include <Windows.h>
#pragma comment (lib, "User32.lib")
namespace Ui {
class ParentWidget;
}
class ParentWidget : public QWidget
{
Q_OBJECT
public:
explicit ParentWidget(QWidget *parent = 0);
~ParentWidget();
HWND GetWinHwndByTitle(QString title);
int GetProcessIdByHWND(HWND handle);
private:
Ui::ParentWidget *ui;
QWidget *childWidget;
private slots:
void integrate();
};
#endif // PARENTWIDGET_H
2.cpp文件
#include "parentwidget.h"
#include "ui_parentwidget.h"
#include <QDebug>
#include <QWindow>
ParentWidget::ParentWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ParentWidget),
childWidget(NULL)
{
ui->setupUi(this);
connect(ui->integrateWidgetBtn, SIGNAL(clicked(bool)), this, SLOT(integrate()));
}
ParentWidget::~ParentWidget()
{
delete ui;
}
void ParentWidget::integrate()//你要调用的函数
{
//int childWidgetWinId = 2295024; //get from debug output
HWND hwnd=GetWinHwndByTitle("xxx");//第三方软件的title
int intHwnd = reinterpret_cast<int>(hwnd);
QWindow *childWindow = QWindow::fromWinId(intHwnd);
childWindow->setFlag(Qt::FramelessWindowHint);
childWidget = QWidget::createWindowContainer(childWindow, this, Qt::Widget );
ui->childWidgetLayout->addWidget(childWidget);
}
HWND ParentWidget::GetWinHwndByTitle(QString title)
{
HWND hWnd = NULL;
hWnd = FindWindow(NULL, (LPCWSTR)title.unicode());
return hWnd;
}
3.总结
integrate()函数和GetWinHwndByTitle(QString title)函数很容易稍加修改,放入你自己的GUI类中。注意:头文件中需要加
#include <Windows.h>
#pragma comment (lib, "User32.lib")