UE5 运行时捕捉外部窗口并嵌入到主窗口

UE5 运行时捕捉外部窗口并嵌入到主窗口的一种方法

创建一个Slate类用于生成一个窗口

cpp 复制代码
.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Widgets/SCompoundWidget.h"

/**
 * 
 */
class NEWWINDOWTEST0822_API SCreateNewWindow : public SCompoundWidget
{
public:
	SLATE_BEGIN_ARGS(SCreateNewWindow)
	{}
	SLATE_END_ARGS()

	/** Constructs this widget with InArgs */
	void Construct(const FArguments& InArgs);

	static void CreateAAA();
};
cpp 复制代码
.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "SCreateNewWindow.h"
#include "SlateOptMacros.h"

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SCreateNewWindow::Construct(const FArguments& InArgs)
{
	/*
	ChildSlot
	[
		// Populate the widget
	];
	*/
}

void SCreateNewWindow::CreateAAA()
{
	TSharedPtr<SWindow> NewWin = SNew(SWindow).ClientSize(FVector2D(800,600))
					.Title(FText::FromString(TEXT("AAAAA")));

	FSlateApplication::Get().AddWindow(NewWin.ToSharedRef());
}

END_SLATE_FUNCTION_BUILD_OPTIMIZATION

创建一个Actor类,用于调用Slate类中的方法

cpp 复制代码
.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class NEWWINDOWTEST0822_API AMyActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMyActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UFUNCTION(BlueprintCallable,Category = "Function")
	static void SpawnWindow();

	UFUNCTION(BlueprintCallable,Category = "Function")
	static void findAAA(FString AAAname);

	static void OnMainWindowClosed();

};
cpp 复制代码
.cpp


#include "MyActor.h"
#include "Windows/AllowWindowsPlatformTypes.h"
#include "SCreateNewWindow.h"

FDelegateHandle MainWindowClosedHandle; // 用于存储主窗口关闭事件的句柄
HWND OtherWindowHandle = nullptr; // 嵌入的子窗口句柄

// Sets default values
AMyActor::AMyActor()
{
    // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;
}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
    Super::BeginPlay();
}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}

void AMyActor::SpawnWindow()
{
    SCreateNewWindow::CreateAAA();
}

void AMyActor::findAAA(FString AAAname)
{
    void* winhandle = GEngine->GameViewport->GetWindow()->GetNativeWindow()->GetOSWindowHandle();

    // 进行类型转换,获取主窗口句柄
    HWND MainWindowHwnd = static_cast<HWND>(winhandle);

    if (MainWindowHwnd)
    {
        const WCHAR* WindowName = *AAAname;
        HWND otherWindow = FindWindowW(NULL, WindowName);

        // 设置子窗口位置
        SetWindowPos(otherWindow, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);

        // 获取子窗口当前样式
        LONG style = GetWindowLong(otherWindow, GWL_STYLE);

        // 清除窗口边框样式
        style &= ~(WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);

        // 添加无边框样式和可移动样式
        style |= WS_POPUP | WS_BORDER;

        // 设置子窗口样式
        SetWindowLong(otherWindow, GWL_STYLE, style);

        // 将子窗口嵌入到主窗口中
        SetParent(otherWindow, MainWindowHwnd);

        // 存储嵌入的子窗口句柄
        OtherWindowHandle = otherWindow;

        // 注册主窗口关闭事件
        MainWindowClosedHandle = FCoreDelegates::OnPreExit.AddStatic(&AMyActor::OnMainWindowClosed);
    }
}

void AMyActor::OnMainWindowClosed()
{
    if (OtherWindowHandle)
    {
        DestroyWindow(OtherWindowHandle);
    }

    // 取消主窗口关闭事件的注册
    FCoreDelegates::OnPreExit.Remove(MainWindowClosedHandle);
}
相关推荐
索迪迈科技16 小时前
Flink Task线程处理模型:Mailbox
java·大数据·开发语言·数据结构·算法·flink
dy171719 小时前
element-plus表格默认展开有子的数据
前端·javascript·vue.js
float_六七20 小时前
IntelliJ IDEA双击Ctrl的妙用
java·ide·intellij-idea
能摆一天是一天21 小时前
JAVA stream().flatMap()
java·windows
颜如玉1 天前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
2501_915918411 天前
Web 前端可视化开发工具对比 低代码平台、可视化搭建工具、前端可视化编辑器与在线可视化开发环境的实战分析
前端·低代码·ios·小程序·uni-app·编辑器·iphone
程序员的世界你不懂1 天前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
星空寻流年1 天前
设计模式第一章(建造者模式)
java·设计模式·建造者模式
索迪迈科技1 天前
网络请求库——Axios库深度解析
前端·网络·vue.js·北京百思可瑞教育·百思可瑞教育
gnip1 天前
JavaScript二叉树相关概念
前端