将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

posted @ 2016-11-03 14:58  湛蓝玫瑰  阅读(3674)  评论(0编辑  收藏  举报