QWidget(c++)嵌入window环境的exe
QWidget(c++)嵌入window环境的exe
我用的msvc2017 64bit编译器会报这个错误
visual.obj:-1: error: LNK2019: __imp_FindWindowW "public: __cdecl Visual::Visual(class QWidget *)" (??0Visual@@QEAA@PEAVQWidget@@@Z)
然后改成minGw 64bit 就不会报错
#ifndef VISUAL_H
#define VISUAL_H
#include <QWidget>
#include <QProcess>
#include<Windows.h>
#include<QWindow>
#include <Windows.h>
#include <iostream>
#include <WinUser.h>
QT_BEGIN_NAMESPACE
namespace Ui { class Visual; }
QT_END_NAMESPACE
class Visual : public QWidget
{
Q_OBJECT
public:
Visual(QWidget *parent = nullptr);
~Visual();
private:
Ui::Visual *ui;
QProcess *process;
QWindow * m_window;
};
#endif // VISUAL_H
#include "visual.h"
#include "ui_visual.h"
#include <Windows.h>
#include <iostream>
#include <WinUser.h>
Visual::Visual(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Visual)
{
ui->setupUi(this);
process = new QProcess(this);
//必须先启动exe,才能获取到它的句柄
QString cmd ="C:/Program Files/Tencent/WeChat/WeChat.exe";
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = true;
bool bRet = CreateProcess(
NULL,
(LPWSTR)cmd.toStdWString().c_str(),
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL, &si, &pi);
//延时是为了保证exe程序充足启动时间,才能获取句柄
Sleep(1000);
//窗口的类名和窗口的标题。要专门用spy工具去获取,下面有图
QString className = "WeChatLoginWndForPC"; // 记事本窗口的类名
QString windowName = "微信"; // 记事本窗口的标题
// 查找窗口句柄
WId wid = (WId)FindWindow((LPCWSTR)className.unicode(), (LPCWSTR)windowName.unicode());
// if (wid == 0) {
// std::cerr << "未找到窗口!" << std::endl;
// return ;
// }
m_window = QWindow::fromWinId(wid);
m_window->setFlags(m_window->flags() | Qt::CustomizeWindowHint | Qt::WindowTitleHint); //
QWidget *m_widget;
m_widget = QWidget::createWindowContainer(m_window, this->ui->widget);
m_widget->setMinimumSize(800, 800);
ui->widget->layout()->addWidget(m_widget);
}
Visual::~Visual()
{
delete ui;
}
我这里使用vs的spy工具,获取窗口的 标题和类名
最后是这种效果