需求:在Windows平台软件中集成一个VR模块,能够在Qt软件中集成一个UE生成的可执行窗口程序。
- 新建一个c++类:VRModule,继承QWidget。这个类适是用于加载UE程序的。添加头文件:
#include <QObject>
#include <QWidget>
#include <QProcess>
#include <QTimer>
#include <QtConcurrent> // 需要在Pro文件中添加QT += concurrent
#include "windows.h"
- 定义一个定时器,用于更新窗口的激活状态和位置
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &VRModule::timerShowUe);
m_timer->start(10);
void VRModule::timerShowUe() {
if (m_Widget) {
if (GetForegroundWindow() != m_hwnWindow && !this->isActiveWindow()) {
SetWindowPos(m_hwnWindow,
HWND_NOTOPMOST,
mapToGlobal(m_Widget->pos()).x(),
mapToGlobal(m_Widget->pos()).y(),
m_Widget->width(),
m_Widget->height(),
SWP_NOACTIVATE);
} else {
SetWindowPos(m_hwnWindow,
HWND_TOPMOST,
mapToGlobal(m_Widget->pos()).x(),
mapToGlobal(m_Widget->pos()).y(),
m_Widget->width(),
m_Widget->height(),
SWP_NOACTIVATE);
}
}
}
- GetForegroundWindow() 函数用于获取当前前台窗口的句柄。
- m_hwnWindow ue程序窗口句柄
- this->isActiveWindow() 检查当前窗口是否处于活动状态。
- SetWindowPos() 是一个 Windows API 函数,用于改变窗口的位置和大小。
这段代码的功能解释如下:
- 如果前台窗口不是 m_hwnWindow,且当前窗口不处于活动状态,代码使用 SetWindowPos() 设置 m_hwnWindow 的位置,使其不是最顶层的窗口。
- 如果不满足上述条件,则使用 SetWindowPos() 将 m_hwnWindow 设置为最顶层的窗口。
mapToGlobal() 函数用于将显示UE程序的QWidget坐标转换为屏幕坐标。SWP_NOACTIVATE 标志用于在移动窗口时防止激活窗口。
- 添加一个函数,用于外部调用该类,传递UE程序的路径和用于显示UE程序的QWidget窗口
void VRModule::getPathAndWidgetContainer(const QString &path, QWidget *widgetContainer) {
m_Widget = widgetContainer;
//获取启动程序的名字
QString fileNameWithExtension = QFileInfo(path).fileName();
QString baseName = fileNameWithExtension.section('.', 0, 0);
QString APPName = QString("%1 (64-bit Development PCD3D_SM6) ").arg(baseName);
m_appName = APPName.toStdWString();
//应用程序以窗口模式运行,而不是全屏
QStringList arguments;
arguments << "-WINDOWED";
m_process = new QProcess;
m_process->start(path, arguments);
QtConcurrent::run([this]{
while (true) {
m_hwnWindow = FindWindow(L"UnrealWindow", m_appName.c_str());
LONG style = GetWindowLong(m_hwnWindow, GWL_EXSTYLE) & (~WS_OVERLAPPEDWINDOW);
//隐藏图标
SetWindowLong(m_hwnWindow, GWL_EXSTYLE, style | WS_EX_TOOLWINDOW);
//隐藏菜单栏
SetWindowLong(m_hwnWindow, GWL_STYLE,

最低0.47元/天 解锁文章
5685





