Slate UI OpenFileDialog
本节内容参考《UE4 Slate创建独立窗口APP》创建OpenFileDialog
打开文件对话框,同时使用简单的SGridPanel
和SScrollBox
进行窗体布局。
打开文件对话框
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", "浏览"))
]
]
]
];