Improve MessageBoxEx show function, making experience like System.Windows.MessageBox

This commit is contained in:
Jack251970 2024-11-26 22:02:24 +08:00
parent 897fd7aaba
commit 32c138b9fd
2 changed files with 56 additions and 37 deletions

View file

@ -21,7 +21,7 @@
<KeyBinding Key="Escape" Command="Close" /> <KeyBinding Key="Escape" Command="Close" />
</Window.InputBindings> </Window.InputBindings>
<Window.CommandBindings> <Window.CommandBindings>
<CommandBinding Command="Close" Executed="cmdEsc_OnPress" /> <CommandBinding Command="Close" Executed="KeyEsc_OnPress" />
</Window.CommandBindings> </Window.CommandBindings>
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>

View file

@ -16,62 +16,52 @@ namespace Flow.Launcher.Core
static MessageBoxEx msgBox; static MessageBoxEx msgBox;
static MessageBoxResult _result = MessageBoxResult.No; static MessageBoxResult _result = MessageBoxResult.No;
/// 1 parameter /// 1 parameter
public static MessageBoxResult Show(string messageBoxText) public static MessageBoxResult Show(string messageBoxText)
{ {
return Show(messageBoxText, string.Empty, MessageBoxButton.OK, MessageBoxImage.None); return Show(messageBoxText, string.Empty, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK);
} }
// 2 parameter // 2 parameter
public static MessageBoxResult Show(string messageBoxText, string title) public static MessageBoxResult Show(string messageBoxText, string caption)
{ {
return Show(messageBoxText, title, MessageBoxButton.OK, MessageBoxImage.None); return Show(messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK);
} }
/// 3 parameter /// 3 parameter
public static MessageBoxResult Show(string messageBoxText, string title, MessageBoxButton type) public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button)
{ {
return Show(messageBoxText, title, type, MessageBoxImage.None); return Show(messageBoxText, caption, button, MessageBoxImage.None, MessageBoxResult.OK);
} }
// 4 parameter, Final Display Message. // 4 parameter
public static MessageBoxResult Show(string messageBoxText, string title, MessageBoxButton button, MessageBoxImage image) public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon)
{
return Show(messageBoxText, caption, button, icon, MessageBoxResult.OK);
}
// 5 parameter, Final Display Message.
public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
{ {
msgBox = new MessageBoxEx(); msgBox = new MessageBoxEx();
if (title == string.Empty && button == MessageBoxButton.OK && image == MessageBoxImage.None) if (caption == string.Empty && button == MessageBoxButton.OK && icon == MessageBoxImage.None)
{ {
msgBox.DescOnlyTextBlock.Text = messageBoxText; msgBox.DescOnlyTextBlock.Text = messageBoxText;
msgBox.Title = messageBoxText; msgBox.Title = messageBoxText;
} }
else else
{ {
msgBox.TitleTextBlock.Text = title; msgBox.TitleTextBlock.Text = caption;
msgBox.DescTextBlock.Text = messageBoxText; msgBox.DescTextBlock.Text = messageBoxText;
msgBox.Title = title; msgBox.Title = caption;
SetImageOfMessageBox(image); SetImageOfMessageBox(icon);
} }
SetVisibilityOfButtons(button); SetVisibilityOfButtons(button, defaultResult);
msgBox.ShowDialog(); msgBox.ShowDialog();
return _result; return _result;
} }
private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender == btnOk)
_result = MessageBoxResult.OK;
else if (sender == btnYes)
_result = MessageBoxResult.Yes;
else if (sender == btnNo)
_result = MessageBoxResult.No;
else if (sender == btnCancel)
_result = MessageBoxResult.Cancel;
else
_result = MessageBoxResult.None;
msgBox.Close();
msgBox = null;
}
private static void SetVisibilityOfButtons(MessageBoxButton button) private static void SetVisibilityOfButtons(MessageBoxButton button, MessageBoxResult defaultResult)
{ {
switch (button) switch (button)
{ {
@ -84,24 +74,36 @@ namespace Flow.Launcher.Core
case MessageBoxButton.OKCancel: case MessageBoxButton.OKCancel:
msgBox.btnNo.Visibility = Visibility.Collapsed; msgBox.btnNo.Visibility = Visibility.Collapsed;
msgBox.btnYes.Visibility = Visibility.Collapsed; msgBox.btnYes.Visibility = Visibility.Collapsed;
msgBox.btnOk.Focus(); if (defaultResult == MessageBoxResult.Cancel)
msgBox.btnCancel.Focus();
else
msgBox.btnOk.Focus();
break; break;
case MessageBoxButton.YesNo: case MessageBoxButton.YesNo:
msgBox.btnOk.Visibility = Visibility.Collapsed; msgBox.btnOk.Visibility = Visibility.Collapsed;
msgBox.btnCancel.Visibility = Visibility.Collapsed; msgBox.btnCancel.Visibility = Visibility.Collapsed;
msgBox.btnYes.Focus(); if (defaultResult == MessageBoxResult.No)
msgBox.btnNo.Focus();
else
msgBox.btnYes.Focus();
break; break;
case MessageBoxButton.YesNoCancel: case MessageBoxButton.YesNoCancel:
msgBox.btnOk.Visibility = Visibility.Collapsed; msgBox.btnOk.Visibility = Visibility.Collapsed;
msgBox.btnYes.Focus(); if (defaultResult == MessageBoxResult.No)
msgBox.btnNo.Focus();
else if (defaultResult == MessageBoxResult.Cancel)
msgBox.btnCancel.Focus();
else
msgBox.btnYes.Focus();
break; break;
default: default:
break; break;
} }
} }
private static void SetImageOfMessageBox(MessageBoxImage image)
private static void SetImageOfMessageBox(MessageBoxImage icon)
{ {
switch (image) switch (icon)
{ {
case MessageBoxImage.Exclamation: case MessageBoxImage.Exclamation:
msgBox.SetImage("Exclamation.png"); msgBox.SetImage("Exclamation.png");
@ -124,23 +126,40 @@ namespace Flow.Launcher.Core
break; break;
} }
} }
private void SetImage(string imageName) private void SetImage(string imageName)
{ {
string uri = Constant.ProgramDirectory + "/Images/" + imageName; string uri = Constant.ProgramDirectory + "/Images/" + imageName;
var uriSource = new Uri(uri, UriKind.RelativeOrAbsolute); var uriSource = new Uri(uri, UriKind.RelativeOrAbsolute);
Img.Source = new BitmapImage(uriSource); Img.Source = new BitmapImage(uriSource);
} }
private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e)
private void KeyEsc_OnPress(object sender, ExecutedRoutedEventArgs e)
{ {
DialogResult = false; DialogResult = false;
Close(); Close();
} }
private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender == btnOk)
_result = MessageBoxResult.OK;
else if (sender == btnYes)
_result = MessageBoxResult.Yes;
else if (sender == btnNo)
_result = MessageBoxResult.No;
else if (sender == btnCancel)
_result = MessageBoxResult.Cancel;
else
_result = MessageBoxResult.None;
msgBox.Close();
msgBox = null;
}
private void Button_Cancel(object sender, RoutedEventArgs e) private void Button_Cancel(object sender, RoutedEventArgs e)
{ {
msgBox.Close(); msgBox.Close();
msgBox = null; msgBox = null;
} }
} }
} }