Build.cs
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
public class SlateWindowTest : ModuleRules
{
public SlateWindowTest(ReadOnlyTargetRules Target) : base(Target)
{
PublicIncludePathModuleNames.Add("Launch");
PrivateDependencyModuleNames.Add("Core");
PrivateDependencyModuleNames.Add("Projects");
PrivateDependencyModuleNames.Add("Slate");
PrivateDependencyModuleNames.Add("SlateCore");
PrivateDependencyModuleNames.Add("StandaloneRenderer");
// to link with CoreUObject module:
// PrivateDependencyModuleNames.Add("CoreUObject");
// to enable tracing:
// AppendStringToPublicDefinition("UE_TRACE_ENABLED", "1");
// to enable LLM tracing:
// GlobalDefinitions.Add("LLM_ENABLED_IN_CONFIG=1");
// GlobalDefinitions.Add("UE_MEMORY_TAGS_TRACE_ENABLED=1");
}
}
Target.cs
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
using System.Collections.Generic;
[SupportedPlatforms(UnrealPlatformClass.All)]
public class SlateWindowTestTarget : TargetRules
{
public SlateWindowTestTarget(TargetInfo Target) : base(Target)
{
Type = TargetType.Program;
IncludeOrderVersion = EngineIncludeOrderVersion.Latest;
LinkType = TargetLinkType.Monolithic;
LaunchModuleName = "SlateWindowTest";
// Lean and mean
bBuildDeveloperTools = false;
// Editor-only is enabled for desktop platforms to run unit tests that depend on editor-only data
// It's disabled in test and shipping configs to make profiling similar to the game
bool bDebugOrDevelopment = Target.Configuration == UnrealTargetConfiguration.Debug || Target.Configuration == UnrealTargetConfiguration.Development;
bBuildWithEditorOnlyData = Target.Platform.IsInGroup(UnrealPlatformGroup.Desktop) && bDebugOrDevelopment;
// Currently this app is not linking against the engine, so we'll compile out references from Core to the rest of the engine
bCompileAgainstEngine = false;
bCompileAgainstCoreUObject = true; // 需要使用CoreUObject模块,因为需要使用对象系统和反射机制
bCompileAgainstApplicationCore = true; // 需要使用ApplicationCore模块,需要使用应用程序框架和事件处理机制
bCompileICU = false;
// UnrealHeaderTool is a console application, not a Windows app (sets entry point to main(), instead of WinMain())
bIsBuildingConsoleApplication = false; // 使用WinMain()函数作为入口,因为需要绘制UI,因此关闭 console application
}
}
.h
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "SlotBase.h"
#include "StandaloneRenderer.h"
.cpp
// Copyright Epic Games, Inc. All Rights Reserved.
#include "SlateWindowTest.h"
#include "RequiredProgramMainCPPInclude.h"
#include "StandaloneRenderer.h"
//定义一个日志类别
DEFINE_LOG_CATEGORY_STATIC(LogSlateWindowTest, Log, All);
//实现一个名为SlateWindowTest的应用程序
IMPLEMENT_APPLICATION(SlateWindowTest, "SlateWindowTest");
//Windows应用程序的入口点
int WINAPI WinMain( _In_ HINSTANCE hInInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR, _In_ int nCmdShow )
{
//预初始化Unreal Engine循环,并获取命令行参数,GetCommandLineW函数检索当前进程的命令行字符串
GEngineLoop.PreInit(GetCommandLineW());
// 告诉模块管理器,当加载新的C++模块时,它现在可以处理新加载的UObjects
FModuleManager::Get().StartProcessingNewlyLoadedObjects();
// 使用平台的独立渲染器启动正常的Slate应用程序
SlateApplication::InitializeAsStandaloneApplication(GetStandardStandaloneRenderer());
//初始化高DPI支持
FSlateApplication::InitHighDPI(true);
//创建一个新的SWindow对象,设置窗口的客户区大小为200x200像素
TSharedRef<SWindow> Window = SNew(SWindow)
.ClientSize(FVector2D(800.f, 600.f))
[
//在窗口中放置一个空的小部件
//SNullWidget::NullWidget
//在窗口内放入图片
SNew(SImage)
];
//将新创建的窗口添加到Slate应用程序中
FSlateApplication::Get().AddWindow(Window);
//IsEngineExitRequested=false, while保持窗口,=true,向下执行
while (!IsEngineExitRequested())
{
//如果已请求退出,开始退出过程
BeginExitIfRequested();
//处理所有待处理的消息
FSlateApplication::Get().PumpMessages();
//更新Slate应用程序的状态
FSlateApplication::Get().Tick();
}
//关闭并清理Slate应用程序
FSlateApplication::Shutdown();
//在退出之前清理操作
GEngineLoop.AppPreExit();
//执行最终退出操作
GEngineLoop.AppExit();
//结束程序,正常退出
return 0;
}