Slate UI OpenFileDialog

Slate UI OpenFileDialog

本节内容参考《UE4 Slate创建独立窗口APP》创建OpenFileDialog打开文件对话框,同时使用简单的SGridPanelSScrollBox进行窗体布局。

打开文件对话框

FString OpenFileDialog()
{
	const FString FileDescription = NSLOCTEXT("SlateAppProgram","OpenFileDialogDesc" ,"文本文件").ToString();
	const FString FileExtension = TEXT("*.*;*.txt");
	const FString FileTypes = FString::Printf(TEXT("%s (%s)|%s"), *FileDescription, *FileExtension, *FileExtension);

	// Prompt the user for the filenames
	TArray<FString> OpenFilenames;
	IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
	bool bFileSelected = false;
	int32 FilterIndex = -1;

	// Open file dialog
	if (DesktopPlatform)
	{
		const void* ParentWindowWindowHandle = FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr);

		bFileSelected = DesktopPlatform->OpenFileDialog(
			ParentWindowWindowHandle,
			LOCTEXT("OpenFileDialogTitle","Open").ToString(),
			FPaths::GameAgnosticSavedDir(),
			TEXT(""),
			FileTypes,
			EFileDialogFlags::None,
			OpenFilenames,
			FilterIndex
		);
	}

	// Load file
	if (bFileSelected)
	{
		if (OpenFilenames.Num() > 0)
		{
			return OpenFilenames[0];
		}
	}

	return TEXT("");
}
 
FReply OnButtonClick()
{
	FText title = NSLOCTEXT("SlateAppProgram", "DefaultMessageTitle", "提示");
	FString filename=OpenFileDialog();
	FMessageDialog::Open(EAppMsgType::Ok, FText::FromString(filename), &title);
	return FReply::Handled();
}

TSharedRef<SWidget> MakeButton(FText InLabel)
{
	return SNew(SButton)
		.Text(InLabel)
		.OnClicked_Static(&OnButtonClick);
}

SWindow控件布局

TSharedPtr<SWindow> MainWindow = SNew(SWindow).ClientSize(FVector2D(1280, 720)).Title(
		FText::FromString(TEXT("SlateAppProgram")))
		[
			SNew(SBorder)
			.BorderImage(FCoreStyle::Get().GetBrush("ToolPanel.GroupBorder"))
			[
				SNew(SScrollBox)
				+ SScrollBox::Slot()
				.Padding(5.0f)
				[
					SNew(SGridPanel)
					.FillColumn(0, 0.5f)
					.FillColumn(1, 0.5f)

					// SBorder
					+ SGridPanel::Slot(0, 0)
					[
						SNew(STextBlock)
						.Text(LOCTEXT("SBorderLabel", "SBorder"))
					]

					+ SGridPanel::Slot(1, 0)
					.Padding(0.0f, 5.0f)
					[
						SNew(SBorder)
						[
							SNew(SSpacer)
							.Size(FVector2D(100.0f, 50.0f))
						]
					]
					//
					+ SGridPanel::Slot(0, 1)
					[
						SNew(SEditableTextBox)
					]

					+ SGridPanel::Slot(1, 1)
					.HAlign(HAlign_Center)
					.Padding(0.0f, 5.0f)
					[
						MakeButton(LOCTEXT("SButtonLabel", "浏览"))
					]
				]
			]
		];

运行结果

在这里插入图片描述

原文链接

Slate UI OpenFileDialog

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值