diff --git a/src/Shared/HandyControl_Shared/Controls/Window/MessageBox.cs b/src/Shared/HandyControl_Shared/Controls/Window/MessageBox.cs index c1e3f91d..03915bfb 100644 --- a/src/Shared/HandyControl_Shared/Controls/Window/MessageBox.cs +++ b/src/Shared/HandyControl_Shared/Controls/Window/MessageBox.cs @@ -16,78 +16,44 @@ using HandyControl.Tools.Interop; namespace HandyControl.Controls; /// -/// 消息框 +/// Displays a message box with text, buttons, and symbols to inform and instruct the user. /// [SuppressMessage("ReSharper", "RedundantDelegateCreation")] [TemplatePart(Name = ElementPanel, Type = typeof(Panel))] [TemplatePart(Name = ElementButtonClose, Type = typeof(Button))] - public sealed class MessageBox : Window { private const string ElementPanel = "PART_Panel"; - private const string ElementButtonClose = "PART_ButtonClose"; - private Button _buttonClose; - - private Panel _panel; - - private MessageBoxResult _messageBoxResult = MessageBoxResult.Cancel; - - private Button _buttonOk; - - private Button _buttonCancel; - - private Button _buttonYes; - - private Button _buttonNo; - - private bool _showOk; - - private bool _showCancel; - - private bool _showYes; - - private bool _showNo; - - private IntPtr _lastActiveWindowIntPtr; - public static readonly DependencyProperty MessageProperty = DependencyProperty.Register( nameof(Message), typeof(string), typeof(MessageBox), new PropertyMetadata(default(string))); - public string Message - { - get => (string) GetValue(MessageProperty); - set => SetValue(MessageProperty, value); - } - public static readonly DependencyProperty ImageProperty = DependencyProperty.Register( nameof(Image), typeof(Geometry), typeof(MessageBox), new PropertyMetadata(default(Geometry))); - public Geometry Image - { - get => (Geometry) GetValue(ImageProperty); - set => SetValue(ImageProperty, value); - } - public static readonly DependencyProperty ImageBrushProperty = DependencyProperty.Register( nameof(ImageBrush), typeof(Brush), typeof(MessageBox), new PropertyMetadata(default(Brush))); - public Brush ImageBrush - { - get => (Brush) GetValue(ImageBrushProperty); - set => SetValue(ImageBrushProperty, value); - } - public static readonly DependencyProperty ShowImageProperty = DependencyProperty.Register( nameof(ShowImage), typeof(bool), typeof(MessageBox), new PropertyMetadata(ValueBoxes.FalseBox)); - public bool ShowImage - { - get => (bool) GetValue(ShowImageProperty); - set => SetValue(ShowImageProperty, ValueBoxes.BooleanBox(value)); - } + private Button _buttonClose; + private Panel _panel; + private MessageBoxResult _messageBoxResult = MessageBoxResult.Cancel; + private Button _buttonOk; + private Button _buttonCancel; + private Button _buttonYes; + private Button _buttonNo; + private bool _showOk; + private bool _showCancel; + private bool _showYes; + private bool _showNo; + private IntPtr _lastActiveWindowIntPtr; + /// + /// This constructor is private so people aren't tempted to try and create instances of these -- they should just use the static methods. + /// private MessageBox() { CommandBindings.Add(new CommandBinding(ControlCommands.Confirm, (s, e) => @@ -112,6 +78,30 @@ public sealed class MessageBox : Window }, (s, e) => e.CanExecute = _showNo)); } + public string Message + { + get => (string) GetValue(MessageProperty); + set => SetValue(MessageProperty, value); + } + + public Geometry Image + { + get => (Geometry) GetValue(ImageProperty); + set => SetValue(ImageProperty, value); + } + + public Brush ImageBrush + { + get => (Brush) GetValue(ImageBrushProperty); + set => SetValue(ImageBrushProperty, value); + } + + public bool ShowImage + { + get => (bool) GetValue(ShowImageProperty); + set => SetValue(ShowImageProperty, ValueBoxes.BooleanBox(value)); + } + protected override void OnSourceInitialized(EventArgs e) { if (_showYes && !_showCancel) @@ -119,7 +109,8 @@ public sealed class MessageBox : Window var hMenu = InteropMethods.GetSystemMenu(this.GetHandle(), false); if (hMenu != IntPtr.Zero) { - InteropMethods.EnableMenuItem(hMenu, InteropValues.SC_CLOSE, InteropValues.MF_BYCOMMAND | InteropValues.MF_GRAYED); + InteropMethods.EnableMenuItem(hMenu, InteropValues.SC_CLOSE, + InteropValues.MF_BYCOMMAND | InteropValues.MF_GRAYED); } if (_buttonClose != null) @@ -180,72 +171,88 @@ public sealed class MessageBox : Window protected override void OnPreviewKeyDown(KeyEventArgs e) { - if (e.Key == Key.System && e.SystemKey == Key.F4) + if (e is { Key: Key.System, SystemKey: Key.F4 }) { e.Handled = true; return; } - if (e.KeyboardDevice.Modifiers == ModifierKeys.Control && e.Key == Key.C) + if (e.KeyboardDevice.Modifiers != ModifierKeys.Control || e.Key != Key.C) { - var builder = new StringBuilder(); - var line = new string('-', 27); - builder.Append(line); - builder.Append(Environment.NewLine); - builder.Append(Title); - builder.Append(Environment.NewLine); - builder.Append(line); - builder.Append(Environment.NewLine); - builder.Append(Message); - builder.Append(Environment.NewLine); - builder.Append(line); - builder.Append(Environment.NewLine); - if (_showOk) - { - builder.Append(Lang.Confirm); - builder.Append(" "); - } - if (_showYes) - { - builder.Append(Lang.Yes); - builder.Append(" "); - } - if (_showNo) - { - builder.Append(Lang.No); - builder.Append(" "); - } - if (_showCancel) - { - builder.Append(Lang.Cancel); - builder.Append(" "); - } - builder.Append(Environment.NewLine); - builder.Append(line); - builder.Append(Environment.NewLine); + return; + } - try - { - Clipboard.SetDataObject(builder.ToString()); - } - catch - { - // ignored - } + const int dashCount = 27; + var line = new string('-', dashCount); + + var builder = new StringBuilder() + .Append(line) + .Append(Environment.NewLine) + .Append(Title) + .Append(Environment.NewLine) + .Append(line) + .Append(Environment.NewLine) + .Append(Message) + .Append(Environment.NewLine) + .Append(line) + .Append(Environment.NewLine); + + if (_showOk) + { + builder.Append(Lang.Confirm); + builder.Append(" "); + } + + if (_showYes) + { + builder.Append(Lang.Yes); + builder.Append(" "); + } + + if (_showNo) + { + builder.Append(Lang.No); + builder.Append(" "); + } + + if (_showCancel) + { + builder.Append(Lang.Cancel); + builder.Append(" "); + } + + builder.Append(Environment.NewLine); + builder.Append(line); + builder.Append(Environment.NewLine); + + try + { + Clipboard.SetDataObject(builder.ToString()); + } + catch + { + // ignored } } - /// - /// 成功 - /// - /// - /// public static MessageBoxResult Success(string messageBoxText, string caption = null) + { + return Success(owner: null, messageBoxText, caption); + } + + public static MessageBoxResult Success(System.Windows.Window owner, string messageBoxText, string caption = null) { MessageBox messageBox = null; Application.Current.Dispatcher.Invoke(new Action(() => { - messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK); + messageBox = CreateMessageBox( + owner: owner, + messageBoxText: messageBoxText, + caption: caption, + button: MessageBoxButton.OK, + icon: MessageBoxImage.None, + defaultResult: MessageBoxResult.OK + ); SetButtonStatus(messageBox, MessageBoxButton.OK, MessageBoxResult.OK); messageBox.ShowImage = true; messageBox.Image = ResourceHelper.GetResourceInternal(ResourceToken.SuccessGeometry); @@ -257,17 +264,24 @@ public sealed class MessageBox : Window return messageBox._messageBoxResult; } - /// - /// 消息 - /// - /// - /// public static MessageBoxResult Info(string messageBoxText, string caption = null) + { + return Info(owner: null, messageBoxText, caption); + } + + public static MessageBoxResult Info(System.Windows.Window owner, string messageBoxText, string caption = null) { MessageBox messageBox = null; Application.Current.Dispatcher.Invoke(new Action(() => { - messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK); + messageBox = CreateMessageBox( + owner: owner, + messageBoxText: messageBoxText, + caption: caption, + button: MessageBoxButton.OK, + icon: MessageBoxImage.Information, + defaultResult: MessageBoxResult.OK + ); SetButtonStatus(messageBox, MessageBoxButton.OK, MessageBoxResult.OK); SetImage(messageBox, MessageBoxImage.Information); SystemSounds.Asterisk.Play(); @@ -277,17 +291,24 @@ public sealed class MessageBox : Window return messageBox._messageBoxResult; } - /// - /// 警告 - /// - /// - /// public static MessageBoxResult Warning(string messageBoxText, string caption = null) + { + return Warning(owner: null, messageBoxText, caption); + } + + public static MessageBoxResult Warning(System.Windows.Window owner, string messageBoxText, string caption = null) { MessageBox messageBox = null; Application.Current.Dispatcher.Invoke(new Action(() => { - messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); + messageBox = CreateMessageBox( + owner: owner, + messageBoxText: messageBoxText, + caption: caption, + button: MessageBoxButton.OK, + icon: MessageBoxImage.Warning, + defaultResult: MessageBoxResult.OK + ); SetButtonStatus(messageBox, MessageBoxButton.OK, MessageBoxResult.OK); SetImage(messageBox, MessageBoxImage.Warning); SystemSounds.Asterisk.Play(); @@ -297,17 +318,24 @@ public sealed class MessageBox : Window return messageBox._messageBoxResult; } - /// - /// 错误 - /// - /// - /// public static MessageBoxResult Error(string messageBoxText, string caption = null) + { + return Error(owner: null, messageBoxText, caption); + } + + public static MessageBoxResult Error(System.Windows.Window owner, string messageBoxText, string caption = null) { MessageBox messageBox = null; Application.Current.Dispatcher.Invoke(new Action(() => { - messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK); + messageBox = CreateMessageBox( + owner: owner, + messageBoxText: messageBoxText, + caption: caption, + button: MessageBoxButton.OK, + icon: MessageBoxImage.Error, + defaultResult: MessageBoxResult.OK + ); SetButtonStatus(messageBox, MessageBoxButton.OK, MessageBoxResult.OK); SetImage(messageBox, MessageBoxImage.Error); SystemSounds.Asterisk.Play(); @@ -317,17 +345,24 @@ public sealed class MessageBox : Window return messageBox._messageBoxResult; } - /// - /// 严重 - /// - /// - /// public static MessageBoxResult Fatal(string messageBoxText, string caption = null) + { + return Fatal(owner: null, messageBoxText, caption); + } + + public static MessageBoxResult Fatal(System.Windows.Window owner, string messageBoxText, string caption = null) { MessageBox messageBox = null; Application.Current.Dispatcher.Invoke(new Action(() => { - messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK); + messageBox = CreateMessageBox( + owner: owner, + messageBoxText: messageBoxText, + caption: caption, + button: MessageBoxButton.OK, + icon: MessageBoxImage.None, + defaultResult: MessageBoxResult.OK + ); SetButtonStatus(messageBox, MessageBoxButton.OK, MessageBoxResult.OK); messageBox.ShowImage = true; messageBox.Image = ResourceHelper.GetResourceInternal(ResourceToken.FatalGeometry); @@ -339,17 +374,24 @@ public sealed class MessageBox : Window return messageBox._messageBoxResult; } - /// - /// 询问 - /// - /// - /// public static MessageBoxResult Ask(string messageBoxText, string caption = null) + { + return Ask(owner: null, messageBoxText, caption); + } + + public static MessageBoxResult Ask(System.Windows.Window owner, string messageBoxText, string caption = null) { MessageBox messageBox = null; Application.Current.Dispatcher.Invoke(new Action(() => { - messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.Cancel); + messageBox = CreateMessageBox( + owner: owner, + messageBoxText: messageBoxText, + caption: caption, + button: MessageBoxButton.OKCancel, + icon: MessageBoxImage.Question, + defaultResult: MessageBoxResult.Cancel + ); SetButtonStatus(messageBox, MessageBoxButton.OKCancel, MessageBoxResult.Cancel); SetImage(messageBox, MessageBoxImage.Question); SystemSounds.Asterisk.Play(); @@ -359,17 +401,19 @@ public sealed class MessageBox : Window return messageBox._messageBoxResult; } - /// - /// 自定义信息展示 - /// - /// - /// public static MessageBoxResult Show(MessageBoxInfo info) { MessageBox messageBox = null; Application.Current.Dispatcher.Invoke(new Action(() => { - messageBox = CreateMessageBox(null, info.Message, info.Caption, info.Button, MessageBoxImage.None, info.DefaultResult); + messageBox = CreateMessageBox( + owner: info.Owner, + messageBoxText: info.Message, + caption: info.Caption, + button: info.Button, + icon: MessageBoxImage.None, + defaultResult: info.DefaultResult + ); SetButtonStatus(messageBox, info.Button, info.DefaultResult); if (!string.IsNullOrEmpty(info.IconKey)) @@ -383,6 +427,7 @@ public sealed class MessageBox : Window { messageBox.Style = ResourceHelper.GetResource