参考自:

https://blog.csdn.net/qq_46865566/article/details/139010127

修改项目的.Build.cs文件

添加下述代码,在

        // 添加这一行PublicDependencyModuleNames
		PublicSystemLibraries.Add("user32.lib");
		PublicDependencyModuleNames.AddRange(new string[]
		{
			...,
			"Slate","SlateCore","ApplicationCore"//添加这些
		});

然后关闭项目,删除Binaries Intermediate ,然后右键.uproject 点击Generate Visual Studio project files 刷新项目文件。

创建脚本

创建继承自蓝图函数库UBlueprintFunctionLibrary的脚本

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

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "TopWindowBFL.generated.h"

/**
 * 
 */
UCLASS()
class ZHUANGBEIKU_WLHY_TJ_API UTopWindowBFL : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
public:
	UFUNCTION(BlueprintCallable, Category = "Window")
	static void SetWindowOnTop(bool StayOnTop);

};
// Fill out your copyright notice in the Description page of Project Settings.
 
 
#include "TopWindowBFL.h"
#include "Engine/Engine.h"
#include "Engine/GameViewportClient.h"
#include "Slate/SceneViewport.h"
#include "Framework/Application/SlateApplication.h"
#include "Widgets/SWindow.h"
#include "Windows/WindowsHWrapper.h"
#include "Windows/AllowWindowsPlatformTypes.h"
#include <windows.h>
#include "Windows/HideWindowsPlatformTypes.h"
#include "WinUser.h"
 
void UTopWindowBFL::SetWindowOnTop(bool StayOnTop)
{
	if (GEngine && GEngine->GameViewport)
	{
		TSharedPtr<SWindow> Window = GEngine->GameViewport->GetWindow();
		if (Window.IsValid())
		{
			TSharedPtr<FGenericWindow> NativeWindowPtr = Window->GetNativeWindow();
			if (NativeWindowPtr.IsValid())
			{
				HWND Handle = static_cast<HWND>(NativeWindowPtr->GetOSWindowHandle());
				if (Handle)
				{
					if (!::SetWindowPos(Handle, StayOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE))
					{
						DWORD Error = GetLastError();
						UE_LOG(LogTemp, Error, TEXT("SetWindowPos failed with error code: %d"), Error);
					}
				}
				else
				{
					UE_LOG(LogTemp, Error, TEXT("Failed to get OS window handle."));
				}
			}
			else
			{
				UE_LOG(LogTemp, Error, TEXT("Failed to get native window pointer."));
			}
		}
		else
		{
			UE_LOG(LogTemp, Error, TEXT("Game viewport window is not valid."));
		}
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("GEngine or GameViewport is null."));
	}
}

使用

蓝图中调用