嵌入到Qt窗口有2种思路:
1、直接使用WinAPI将窗口直接嵌入,缺点:你需要自己编写移动、Layout之类的调整代码。 2、使用createWindowContainer,将窗口作为QWidget嵌入,缺点:默认Widget会屏蔽掉按键事件,所以需要让其取得,也就是说需要完善好这一方面的逻辑。
中间2个函数是用于查找桌面句柄的,另外2块注释的地方分别是,将打包工程嵌入到桌面、嵌入到Qt窗口的代码。
#include "widget.h"#include "ui_widget.h"#include "windows.h"#include#include Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this);}Widget::~Widget(){ delete ui;}//网上找到的把窗体嵌入桌面的函数static BOOL enumUserWindowsCB(HWND hwnd,LPARAM lParam){ long wflags = GetWindowLong(hwnd, GWL_STYLE); if(!(wflags & WS_VISIBLE)) return TRUE; HWND sndWnd; if( !(sndWnd=FindWindowEx(hwnd, NULL, L"SHELLDLL_DefView", NULL)) ) return TRUE; HWND targetWnd; if( !(targetWnd=FindWindowEx(sndWnd, NULL, L"SysListView32", L"FolderView")) ) return TRUE; HWND* resultHwnd = (HWND*)lParam; *resultHwnd = targetWnd; return FALSE; }//网上找到的把窗体嵌入桌面的函数HWND findDesktopIconWnd() { HWND resultHwnd = NULL; EnumWindows((WNDENUMPROC)enumUserWindowsCB, (LPARAM)&resultHwnd); return resultHwnd; }void Widget::on_pushButton_clicked(){ HWND hwnWindow=FindWindow(NULL,L"DemoGame"); HWND desktopHwnd=findDesktopIconWnd(); QWindow *window=QWindow::fromWinId((WId)hwnWindow);// //将窗口嵌入到桌面上// LONG styleValue=GetWindowLong(hwnWindow,GWL_STYLE);// styleValue&=~WS_CAPTION;// SetWindowLong(hwnWindow,GWL_STYLE,styleValue);// SetParent(hwnWindow,desktopHwnd);// //嵌入Qt窗口// SetParent(hwnWindow,(HWND)QWidget::winId());// window->showFullScreen(); //嵌入Qt窗口,需要设置焦点让Ue4接受按键事件 QWidget *windowWidget=QWidget::createWindowContainer(window); ui->verticalLayout->addWidget(windowWidget); windowWidget->setFocusPolicy(Qt::StrongFocus); windowWidget->setFocus(); windowWidget->grabKeyboard(); windowWidget->grabMouse(); this->setFocusPolicy(Qt::ClickFocus);}