enhance: closed #1710
This commit is contained in:
1 parent
49b2befc96
commit
0f65abbfcc
2 files changed
+256
-193
No files matched your search
@@ -16,78 +16,44 @@ using HandyControl.Tools.Interop;
|
|||||||
namespace HandyControl.Controls;
|
namespace HandyControl.Controls;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 消息框
|
/// Displays a message box with text, buttons, and symbols to inform and instruct the user.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SuppressMessage("ReSharper", "RedundantDelegateCreation")]
|
[SuppressMessage("ReSharper", "RedundantDelegateCreation")]
|
||||||
[TemplatePart(Name = ElementPanel, Type = typeof(Panel))]
|
[TemplatePart(Name = ElementPanel, Type = typeof(Panel))]
|
||||||
[TemplatePart(Name = ElementButtonClose, Type = typeof(Button))]
|
[TemplatePart(Name = ElementButtonClose, Type = typeof(Button))]
|
||||||
|
|
||||||
public sealed class MessageBox : Window
|
public sealed class MessageBox : Window
|
||||||
{
|
{
|
||||||
private const string ElementPanel = "PART_Panel";
|
private const string ElementPanel = "PART_Panel";
|
||||||
|
|
||||||
private const string ElementButtonClose = "PART_ButtonClose";
|
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(
|
public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
|
||||||
nameof(Message), typeof(string), typeof(MessageBox), new PropertyMetadata(default(string)));
|
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(
|
public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(
|
||||||
nameof(Image), typeof(Geometry), typeof(MessageBox), new PropertyMetadata(default(Geometry)));
|
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(
|
public static readonly DependencyProperty ImageBrushProperty = DependencyProperty.Register(
|
||||||
nameof(ImageBrush), typeof(Brush), typeof(MessageBox), new PropertyMetadata(default(Brush)));
|
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(
|
public static readonly DependencyProperty ShowImageProperty = DependencyProperty.Register(
|
||||||
nameof(ShowImage), typeof(bool), typeof(MessageBox), new PropertyMetadata(ValueBoxes.FalseBox));
|
nameof(ShowImage), typeof(bool), typeof(MessageBox), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||||
|
|
||||||
public bool ShowImage
|
private Button _buttonClose;
|
||||||
{
|
private Panel _panel;
|
||||||
get => (bool) GetValue(ShowImageProperty);
|
private MessageBoxResult _messageBoxResult = MessageBoxResult.Cancel;
|
||||||
set => SetValue(ShowImageProperty, ValueBoxes.BooleanBox(value));
|
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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This constructor is private so people aren't tempted to try and create instances of these -- they should just use the static methods.
|
||||||
|
/// </summary>
|
||||||
private MessageBox()
|
private MessageBox()
|
||||||
{
|
{
|
||||||
CommandBindings.Add(new CommandBinding(ControlCommands.Confirm, (s, e) =>
|
CommandBindings.Add(new CommandBinding(ControlCommands.Confirm, (s, e) =>
|
||||||
@@ -112,6 +78,30 @@ public sealed class MessageBox : Window
|
|||||||
}, (s, e) => e.CanExecute = _showNo));
|
}, (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)
|
protected override void OnSourceInitialized(EventArgs e)
|
||||||
{
|
{
|
||||||
if (_showYes && !_showCancel)
|
if (_showYes && !_showCancel)
|
||||||
@@ -119,7 +109,8 @@ public sealed class MessageBox : Window
|
|||||||
var hMenu = InteropMethods.GetSystemMenu(this.GetHandle(), false);
|
var hMenu = InteropMethods.GetSystemMenu(this.GetHandle(), false);
|
||||||
if (hMenu != IntPtr.Zero)
|
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)
|
if (_buttonClose != null)
|
||||||
@@ -180,72 +171,88 @@ public sealed class MessageBox : Window
|
|||||||
|
|
||||||
protected override void OnPreviewKeyDown(KeyEventArgs e)
|
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;
|
e.Handled = true;
|
||||||
return;
|
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();
|
return;
|
||||||
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);
|
|
||||||
|
|
||||||
try
|
const int dashCount = 27;
|
||||||
{
|
var line = new string('-', dashCount);
|
||||||
Clipboard.SetDataObject(builder.ToString());
|
|
||||||
}
|
var builder = new StringBuilder()
|
||||||
catch
|
.Append(line)
|
||||||
{
|
.Append(Environment.NewLine)
|
||||||
// ignored
|
.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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 成功
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="messageBoxText"></param>
|
|
||||||
/// <param name="caption"></param>
|
|
||||||
public static MessageBoxResult Success(string messageBoxText, string caption = null)
|
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;
|
MessageBox messageBox = null;
|
||||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
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);
|
SetButtonStatus(messageBox, MessageBoxButton.OK, MessageBoxResult.OK);
|
||||||
messageBox.ShowImage = true;
|
messageBox.ShowImage = true;
|
||||||
messageBox.Image = ResourceHelper.GetResourceInternal<Geometry>(ResourceToken.SuccessGeometry);
|
messageBox.Image = ResourceHelper.GetResourceInternal<Geometry>(ResourceToken.SuccessGeometry);
|
||||||
@@ -257,17 +264,24 @@ public sealed class MessageBox : Window
|
|||||||
return messageBox._messageBoxResult;
|
return messageBox._messageBoxResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 消息
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="messageBoxText"></param>
|
|
||||||
/// <param name="caption"></param>
|
|
||||||
public static MessageBoxResult Info(string messageBoxText, string caption = null)
|
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;
|
MessageBox messageBox = null;
|
||||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
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);
|
SetButtonStatus(messageBox, MessageBoxButton.OK, MessageBoxResult.OK);
|
||||||
SetImage(messageBox, MessageBoxImage.Information);
|
SetImage(messageBox, MessageBoxImage.Information);
|
||||||
SystemSounds.Asterisk.Play();
|
SystemSounds.Asterisk.Play();
|
||||||
@@ -277,17 +291,24 @@ public sealed class MessageBox : Window
|
|||||||
return messageBox._messageBoxResult;
|
return messageBox._messageBoxResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 警告
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="messageBoxText"></param>
|
|
||||||
/// <param name="caption"></param>
|
|
||||||
public static MessageBoxResult Warning(string messageBoxText, string caption = null)
|
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;
|
MessageBox messageBox = null;
|
||||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
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);
|
SetButtonStatus(messageBox, MessageBoxButton.OK, MessageBoxResult.OK);
|
||||||
SetImage(messageBox, MessageBoxImage.Warning);
|
SetImage(messageBox, MessageBoxImage.Warning);
|
||||||
SystemSounds.Asterisk.Play();
|
SystemSounds.Asterisk.Play();
|
||||||
@@ -297,17 +318,24 @@ public sealed class MessageBox : Window
|
|||||||
return messageBox._messageBoxResult;
|
return messageBox._messageBoxResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 错误
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="messageBoxText"></param>
|
|
||||||
/// <param name="caption"></param>
|
|
||||||
public static MessageBoxResult Error(string messageBoxText, string caption = null)
|
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;
|
MessageBox messageBox = null;
|
||||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
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);
|
SetButtonStatus(messageBox, MessageBoxButton.OK, MessageBoxResult.OK);
|
||||||
SetImage(messageBox, MessageBoxImage.Error);
|
SetImage(messageBox, MessageBoxImage.Error);
|
||||||
SystemSounds.Asterisk.Play();
|
SystemSounds.Asterisk.Play();
|
||||||
@@ -317,17 +345,24 @@ public sealed class MessageBox : Window
|
|||||||
return messageBox._messageBoxResult;
|
return messageBox._messageBoxResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 严重
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="messageBoxText"></param>
|
|
||||||
/// <param name="caption"></param>
|
|
||||||
public static MessageBoxResult Fatal(string messageBoxText, string caption = null)
|
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;
|
MessageBox messageBox = null;
|
||||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
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);
|
SetButtonStatus(messageBox, MessageBoxButton.OK, MessageBoxResult.OK);
|
||||||
messageBox.ShowImage = true;
|
messageBox.ShowImage = true;
|
||||||
messageBox.Image = ResourceHelper.GetResourceInternal<Geometry>(ResourceToken.FatalGeometry);
|
messageBox.Image = ResourceHelper.GetResourceInternal<Geometry>(ResourceToken.FatalGeometry);
|
||||||
@@ -339,17 +374,24 @@ public sealed class MessageBox : Window
|
|||||||
return messageBox._messageBoxResult;
|
return messageBox._messageBoxResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 询问
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="messageBoxText"></param>
|
|
||||||
/// <param name="caption"></param>
|
|
||||||
public static MessageBoxResult Ask(string messageBoxText, string caption = null)
|
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;
|
MessageBox messageBox = null;
|
||||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
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);
|
SetButtonStatus(messageBox, MessageBoxButton.OKCancel, MessageBoxResult.Cancel);
|
||||||
SetImage(messageBox, MessageBoxImage.Question);
|
SetImage(messageBox, MessageBoxImage.Question);
|
||||||
SystemSounds.Asterisk.Play();
|
SystemSounds.Asterisk.Play();
|
||||||
@@ -359,17 +401,19 @@ public sealed class MessageBox : Window
|
|||||||
return messageBox._messageBoxResult;
|
return messageBox._messageBoxResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 自定义信息展示
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="info"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static MessageBoxResult Show(MessageBoxInfo info)
|
public static MessageBoxResult Show(MessageBoxInfo info)
|
||||||
{
|
{
|
||||||
MessageBox messageBox = null;
|
MessageBox messageBox = null;
|
||||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
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);
|
SetButtonStatus(messageBox, info.Button, info.DefaultResult);
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(info.IconKey))
|
if (!string.IsNullOrEmpty(info.IconKey))
|
||||||
@@ -383,6 +427,7 @@ public sealed class MessageBox : Window
|
|||||||
{
|
{
|
||||||
messageBox.Style = ResourceHelper.GetResource<Style>(info.StyleKey) ?? info.Style;
|
messageBox.Style = ResourceHelper.GetResource<Style>(info.StyleKey) ?? info.Style;
|
||||||
}
|
}
|
||||||
|
|
||||||
SystemSounds.Asterisk.Play();
|
SystemSounds.Asterisk.Play();
|
||||||
messageBox.ShowDialog();
|
messageBox.ShowDialog();
|
||||||
}));
|
}));
|
||||||
@@ -390,31 +435,25 @@ public sealed class MessageBox : Window
|
|||||||
return messageBox._messageBoxResult;
|
return messageBox._messageBoxResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public static MessageBoxResult Show(
|
||||||
/// 信息展示
|
string messageBoxText,
|
||||||
/// </summary>
|
string caption = null,
|
||||||
/// <param name="messageBoxText"></param>
|
MessageBoxButton button = MessageBoxButton.OK,
|
||||||
/// <param name="caption"></param>
|
MessageBoxImage icon = MessageBoxImage.None,
|
||||||
/// <param name="button"></param>
|
MessageBoxResult defaultResult = MessageBoxResult.None
|
||||||
/// <param name="icon"></param>
|
)
|
||||||
/// <param name="defaultResult"></param>
|
{
|
||||||
/// <returns></returns>
|
return Show(owner: null, messageBoxText, caption, button, icon, defaultResult);
|
||||||
public static MessageBoxResult Show(string messageBoxText, string caption = null,
|
}
|
||||||
MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None,
|
|
||||||
MessageBoxResult defaultResult = MessageBoxResult.None) =>
|
|
||||||
Show(null, messageBoxText, caption, button, icon, defaultResult);
|
|
||||||
|
|
||||||
/// <summary>
|
public static MessageBoxResult Show(
|
||||||
/// 信息展示
|
System.Windows.Window owner,
|
||||||
/// </summary>
|
string messageBoxText,
|
||||||
/// <param name="owner"></param>
|
string caption = null,
|
||||||
/// <param name="messageBoxText"></param>
|
MessageBoxButton button = MessageBoxButton.OK,
|
||||||
/// <param name="caption"></param>
|
MessageBoxImage icon = MessageBoxImage.None,
|
||||||
/// <param name="button"></param>
|
MessageBoxResult defaultResult = MessageBoxResult.None
|
||||||
/// <param name="icon"></param>
|
)
|
||||||
/// <param name="defaultResult"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static MessageBoxResult Show(System.Windows.Window owner, string messageBoxText, string caption = null, MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None)
|
|
||||||
{
|
{
|
||||||
MessageBox messageBox = null;
|
MessageBox messageBox = null;
|
||||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
Application.Current.Dispatcher.Invoke(new Action(() =>
|
||||||
@@ -435,19 +474,23 @@ public sealed class MessageBox : Window
|
|||||||
string caption,
|
string caption,
|
||||||
MessageBoxButton button,
|
MessageBoxButton button,
|
||||||
MessageBoxImage icon,
|
MessageBoxImage icon,
|
||||||
MessageBoxResult defaultResult)
|
MessageBoxResult defaultResult
|
||||||
|
)
|
||||||
{
|
{
|
||||||
if (!IsValidMessageBoxButton(button))
|
if (!IsValidMessageBoxButton(button))
|
||||||
{
|
{
|
||||||
throw new InvalidEnumArgumentException(nameof(button), (int) button, typeof(MessageBoxButton));
|
throw new InvalidEnumArgumentException(nameof(button), (int) button, typeof(MessageBoxButton));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IsValidMessageBoxImage(icon))
|
if (!IsValidMessageBoxImage(icon))
|
||||||
{
|
{
|
||||||
throw new InvalidEnumArgumentException(nameof(icon), (int) icon, typeof(MessageBoxImage));
|
throw new InvalidEnumArgumentException(nameof(icon), (int) icon, typeof(MessageBoxImage));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IsValidMessageBoxResult(defaultResult))
|
if (!IsValidMessageBoxResult(defaultResult))
|
||||||
{
|
{
|
||||||
throw new InvalidEnumArgumentException(nameof(defaultResult), (int) defaultResult, typeof(MessageBoxResult));
|
throw new InvalidEnumArgumentException(nameof(defaultResult), (int) defaultResult,
|
||||||
|
typeof(MessageBoxResult));
|
||||||
}
|
}
|
||||||
|
|
||||||
var ownerWindow = owner ?? WindowHelper.GetActiveWindow();
|
var ownerWindow = owner ?? WindowHelper.GetActiveWindow();
|
||||||
@@ -457,7 +500,8 @@ public sealed class MessageBox : Window
|
|||||||
{
|
{
|
||||||
Message = messageBoxText,
|
Message = messageBoxText,
|
||||||
Owner = ownerWindow,
|
Owner = ownerWindow,
|
||||||
WindowStartupLocation = ownerIsNull ? WindowStartupLocation.CenterScreen : WindowStartupLocation.CenterOwner,
|
WindowStartupLocation =
|
||||||
|
ownerIsNull ? WindowStartupLocation.CenterScreen : WindowStartupLocation.CenterOwner,
|
||||||
ShowTitle = true,
|
ShowTitle = true,
|
||||||
Title = caption ?? string.Empty,
|
Title = caption ?? string.Empty,
|
||||||
Topmost = ownerIsNull,
|
Topmost = ownerIsNull,
|
||||||
@@ -465,7 +509,11 @@ public sealed class MessageBox : Window
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void SetButtonStatus(MessageBox messageBox, MessageBoxButton messageBoxButton, MessageBoxResult defaultResult)
|
private static void SetButtonStatus(
|
||||||
|
MessageBox messageBox,
|
||||||
|
MessageBoxButton messageBoxButton,
|
||||||
|
MessageBoxResult defaultResult
|
||||||
|
)
|
||||||
{
|
{
|
||||||
switch (messageBoxButton)
|
switch (messageBoxButton)
|
||||||
{
|
{
|
||||||
@@ -502,12 +550,14 @@ public sealed class MessageBox : Window
|
|||||||
if (defaultResult == MessageBoxResult.Cancel)
|
if (defaultResult == MessageBoxResult.Cancel)
|
||||||
{
|
{
|
||||||
messageBox._buttonOk.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
messageBox._buttonOk.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
||||||
messageBox._buttonCancel.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
messageBox._buttonCancel.Style =
|
||||||
|
ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
||||||
messageBox._buttonCancel.IsDefault = true;
|
messageBox._buttonCancel.IsDefault = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
messageBox._buttonOk.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
messageBox._buttonOk.Style =
|
||||||
|
ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
||||||
messageBox._buttonOk.IsDefault = true;
|
messageBox._buttonOk.IsDefault = true;
|
||||||
messageBox._buttonCancel.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
messageBox._buttonCancel.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
||||||
}
|
}
|
||||||
@@ -532,12 +582,14 @@ public sealed class MessageBox : Window
|
|||||||
if (defaultResult == MessageBoxResult.No)
|
if (defaultResult == MessageBoxResult.No)
|
||||||
{
|
{
|
||||||
messageBox._buttonYes.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
messageBox._buttonYes.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
||||||
messageBox._buttonNo.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
messageBox._buttonNo.Style =
|
||||||
|
ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
||||||
messageBox._buttonNo.IsDefault = true;
|
messageBox._buttonNo.IsDefault = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
messageBox._buttonYes.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
messageBox._buttonYes.Style =
|
||||||
|
ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
||||||
messageBox._buttonYes.IsDefault = true;
|
messageBox._buttonYes.IsDefault = true;
|
||||||
messageBox._buttonNo.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
messageBox._buttonNo.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
||||||
}
|
}
|
||||||
@@ -570,7 +622,8 @@ public sealed class MessageBox : Window
|
|||||||
if (defaultResult == MessageBoxResult.No)
|
if (defaultResult == MessageBoxResult.No)
|
||||||
{
|
{
|
||||||
messageBox._buttonYes.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
messageBox._buttonYes.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
||||||
messageBox._buttonNo.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
messageBox._buttonNo.Style =
|
||||||
|
ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
||||||
messageBox._buttonNo.IsDefault = true;
|
messageBox._buttonNo.IsDefault = true;
|
||||||
messageBox._buttonCancel.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
messageBox._buttonCancel.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
||||||
}
|
}
|
||||||
@@ -578,12 +631,14 @@ public sealed class MessageBox : Window
|
|||||||
{
|
{
|
||||||
messageBox._buttonYes.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
messageBox._buttonYes.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
||||||
messageBox._buttonNo.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
messageBox._buttonNo.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
||||||
messageBox._buttonCancel.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
messageBox._buttonCancel.Style =
|
||||||
|
ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
||||||
messageBox._buttonCancel.IsDefault = true;
|
messageBox._buttonCancel.IsDefault = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
messageBox._buttonYes.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
messageBox._buttonYes.Style =
|
||||||
|
ResourceHelper.GetResourceInternal<Style>("MessageBoxPrimaryButtonStyle");
|
||||||
messageBox._buttonYes.IsDefault = true;
|
messageBox._buttonYes.IsDefault = true;
|
||||||
messageBox._buttonNo.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
messageBox._buttonNo.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
||||||
messageBox._buttonCancel.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
messageBox._buttonCancel.Style = ResourceHelper.GetResourceInternal<Style>("MessageBoxButtonStyle");
|
||||||
@@ -618,7 +673,11 @@ public sealed class MessageBox : Window
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(iconKey)) return;
|
if (string.IsNullOrEmpty(iconKey))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
messageBox.ShowImage = true;
|
messageBox.ShowImage = true;
|
||||||
messageBox.Image = ResourceHelper.GetResourceInternal<Geometry>(iconKey);
|
messageBox.Image = ResourceHelper.GetResourceInternal<Geometry>(iconKey);
|
||||||
messageBox.ImageBrush = ResourceHelper.GetResourceInternal<Brush>(iconBrushKey);
|
messageBox.ImageBrush = ResourceHelper.GetResourceInternal<Brush>(iconBrushKey);
|
||||||
@@ -626,32 +685,34 @@ public sealed class MessageBox : Window
|
|||||||
|
|
||||||
private static bool IsValidMessageBoxButton(MessageBoxButton value)
|
private static bool IsValidMessageBoxButton(MessageBoxButton value)
|
||||||
{
|
{
|
||||||
return value == MessageBoxButton.OK
|
return value
|
||||||
|| value == MessageBoxButton.OKCancel
|
is MessageBoxButton.OK
|
||||||
|| value == MessageBoxButton.YesNo
|
or MessageBoxButton.OKCancel
|
||||||
|| value == MessageBoxButton.YesNoCancel;
|
or MessageBoxButton.YesNo
|
||||||
|
or MessageBoxButton.YesNoCancel;
|
||||||
}
|
}
|
||||||
|
|
||||||
[SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalse")]
|
|
||||||
private static bool IsValidMessageBoxImage(MessageBoxImage value)
|
private static bool IsValidMessageBoxImage(MessageBoxImage value)
|
||||||
{
|
{
|
||||||
return value == MessageBoxImage.Asterisk
|
return value
|
||||||
|| value == MessageBoxImage.Error
|
is MessageBoxImage.Asterisk
|
||||||
|| value == MessageBoxImage.Exclamation
|
or MessageBoxImage.Error
|
||||||
|| value == MessageBoxImage.Hand
|
or MessageBoxImage.Exclamation
|
||||||
|| value == MessageBoxImage.Information
|
or MessageBoxImage.Hand
|
||||||
|| value == MessageBoxImage.None
|
or MessageBoxImage.Information
|
||||||
|| value == MessageBoxImage.Question
|
or MessageBoxImage.None
|
||||||
|| value == MessageBoxImage.Stop
|
or MessageBoxImage.Question
|
||||||
|| value == MessageBoxImage.Warning;
|
or MessageBoxImage.Stop
|
||||||
|
or MessageBoxImage.Warning;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsValidMessageBoxResult(MessageBoxResult value)
|
private static bool IsValidMessageBoxResult(MessageBoxResult value)
|
||||||
{
|
{
|
||||||
return value == MessageBoxResult.Cancel
|
return value
|
||||||
|| value == MessageBoxResult.No
|
is MessageBoxResult.Cancel
|
||||||
|| value == MessageBoxResult.None
|
or MessageBoxResult.No
|
||||||
|| value == MessageBoxResult.OK
|
or MessageBoxResult.None
|
||||||
|| value == MessageBoxResult.Yes;
|
or MessageBoxResult.OK
|
||||||
|
or MessageBoxResult.Yes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -24,4 +24,6 @@ public class MessageBoxInfo
|
|||||||
public Style Style { get; set; }
|
public Style Style { get; set; }
|
||||||
|
|
||||||
public string StyleKey { get; set; }
|
public string StyleKey { get; set; }
|
||||||
|
|
||||||
|
public Window Owner { get; set; }
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user