將Unreal4打包后的工程嵌入到Qt或者桌面中


 1 #include "widget.h"
 2 #include "ui_widget.h"
 3 #include "windows.h"
 4 #include <QWindow>
 5 #include <QProcess>
 6 Widget::Widget(QWidget *parent) :
 7     QWidget(parent),
 8     ui(new Ui::Widget)
 9 {
10     ui->setupUi(this);
11 }
12 
13 Widget::~Widget()
14 {
15     delete ui;
16 }
17 
18 //網上找到的把窗體嵌入桌面的函數
19 static BOOL enumUserWindowsCB(HWND hwnd,LPARAM lParam)
20 {
21     long wflags = GetWindowLong(hwnd, GWL_STYLE);
22     if(!(wflags & WS_VISIBLE)) return TRUE;
23     HWND sndWnd;
24     if( !(sndWnd=FindWindowEx(hwnd, NULL, L"SHELLDLL_DefView", NULL)) ) return TRUE;
25     HWND targetWnd;
26      if( !(targetWnd=FindWindowEx(sndWnd, NULL, L"SysListView32", L"FolderView")) ) return TRUE;
27 
28     HWND* resultHwnd = (HWND*)lParam;
29     *resultHwnd = targetWnd;
30 
31     return FALSE;
32  }
33 //網上找到的把窗體嵌入桌面的函數
34 HWND findDesktopIconWnd()
35  {
36     HWND resultHwnd = NULL;
37     EnumWindows((WNDENUMPROC)enumUserWindowsCB, (LPARAM)&resultHwnd);
38     return resultHwnd;
39  }
40 
41 void Widget::on_pushButton_clicked()
42 {
43     HWND hwnWindow=FindWindow(NULL,L"DemoGame");
44     HWND desktopHwnd=findDesktopIconWnd();
45 
46     QWindow *window=QWindow::fromWinId((WId)hwnWindow);
47 
48 
49 //    //將窗口嵌入到桌面上
50 //    LONG styleValue=GetWindowLong(hwnWindow,GWL_STYLE);
51 //    styleValue&=~WS_CAPTION;
52 //    SetWindowLong(hwnWindow,GWL_STYLE,styleValue);
53 //    SetParent(hwnWindow,desktopHwnd);
54 
55 //    //嵌入Qt窗口
56 //    SetParent(hwnWindow,(HWND)QWidget::winId());
57 //    window->showFullScreen();
58 
59     //嵌入Qt窗口,需要設置焦點讓Ue4接受按鍵事件
60     QWidget *windowWidget=QWidget::createWindowContainer(window);
61     ui->verticalLayout->addWidget(windowWidget);
62     windowWidget->setFocusPolicy(Qt::StrongFocus);
63     windowWidget->setFocus();
64     windowWidget->grabKeyboard();
65     windowWidget->grabMouse();
66     this->setFocusPolicy(Qt::ClickFocus);
67 
68 }

中間2個函數是用於查找桌面句柄的,另外2塊注釋的地方分別是,將打包工程嵌入到桌面、嵌入到Qt窗口的代碼。

嵌入到Qt窗口有2種思路:1、直接使用WinAPI將窗口直接嵌入,缺點:你需要自己編寫移動、Layout之類的調整代碼。

            2、使用createWindowContainer,將窗口作為QWidget嵌入,缺點:默認Widget會屏蔽掉按鍵事件,所以需要讓其取得,也就是說需要完善好這一方面的邏輯。

 

 

本文有了更新 :https://www.cnblogs.com/blueroses/p/9151094.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM