Try to set viewport size by Windows API

Hi!

I’m trying to set viewport size with C++ by Windows API.

My code only works in Editor.

What should I do to make it works after packaging ?

here is my code

#if PLATFORM_WINDOWS
#include "windows/WindowsWindow.h"
#include "Windows/AllowWindowsPlatformTypes.h"
#endif

#if PLATFORM_WINDOWS
	uint SWP_SHOWWINDOW = 0x0040;

	TSharedPtr<FGenericWindow> NativeWindow = GEngine->GameViewport->GetWindow()->GetNativeWindow();

	auto Window = static_cast<FWindowsWindow*>(NativeWindow.Get());

	auto hWnd = Window->GetHWnd();

	SetWindowPos(hWnd, 0, x, y, width, height, SWP_SHOWWINDOW );
#endif

I also tried

GEngine->GameViewport->GetWindow()->ReshapeWindow(FVector2D(x, y), FVector2D(width, height));

it doesn’t work, either.

Thanks for your help.

Perhaps try FSystemResolution::RequestResolutionChange?

My temporary solution is like this:

if (camSensor_.fullScreen)
		{
			GEngine->GameViewport->GetWindow()->MoveWindowTo(FVector2D(viewport_x, viewport_y));
			gameSetting->SetFullscreenMode(EWindowMode::WindowedFullscreen);
			gameSetting->ApplySettings(true);
		}
		else
		{
			gameSetting->SetFullscreenMode(EWindowMode::Windowed);
			gameSetting->SetScreenResolution(FIntPoint(viewport_width, .viewport_height));
			gameSetting->ApplySettings(true);

			GEngine->GameViewport->GetWindow()->MoveWindowTo(FVector2D(viewport_x, viewport_y));
		} 

It’s quite weird that some APIs work and some doesn’t.

And no document can explain why. :sweat_smile: