using System.Windows; namespace Sln.Wcs.UI.Controls { public partial class CustomMessageBox : Window { public MessageBoxResult Result { get; private set; } = MessageBoxResult.None; public CustomMessageBox() { InitializeComponent(); } public static MessageBoxResult Show(string message, string title = "提示", MessageBoxButton button = MessageBoxButton.OK) { var window = new CustomMessageBox { TxtMessage = { Text = message }, Title = title, TxtTitle = { Text = title } }; if (button == MessageBoxButton.OK) { window.PanelOneButton.Visibility = Visibility.Visible; window.PanelTwoButtons.Visibility = Visibility.Collapsed; if (title.Contains("错误") || title.Contains("失败") || title.Contains("警告")) { window.TxtIcon.Text = "✕"; window.TxtIcon.Foreground = new System.Windows.Media.SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#F44336")); } else { window.TxtIcon.Text = "✓"; window.TxtIcon.Foreground = new System.Windows.Media.SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#4CAF50")); } } else if (button == MessageBoxButton.YesNo) { window.PanelOneButton.Visibility = Visibility.Collapsed; window.PanelTwoButtons.Visibility = Visibility.Visible; window.TxtIcon.Text = "?"; window.TxtIcon.Foreground = new System.Windows.Media.SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFC107")); } window.ShowDialog(); return window.Result; } public static void ShowSuccess(string message, Window owner = null) { var window = new CustomMessageBox { TxtMessage = { Text = message }, Title = "提示", TxtTitle = { Text = "提示" }, PanelOneButton = { Visibility = Visibility.Visible }, PanelTwoButtons = { Visibility = Visibility.Collapsed } }; window.TxtIcon.Text = "✓"; window.TxtIcon.Foreground = new System.Windows.Media.SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#4CAF50")); if (owner != null) { window.Owner = owner; } window.ShowDialog(); } public static MessageBoxResult ShowConfirm(string message, Window owner = null) { var window = new CustomMessageBox { TxtMessage = { Text = message }, Title = "确认", TxtTitle = { Text = "确认" }, PanelOneButton = { Visibility = Visibility.Collapsed }, PanelTwoButtons = { Visibility = Visibility.Visible } }; window.TxtIcon.Text = "?"; window.TxtIcon.Foreground = new System.Windows.Media.SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFC107")); if (owner != null) { window.Owner = owner; } window.ShowDialog(); return window.Result; } private void BtnOk_Click(object sender, RoutedEventArgs e) { Result = MessageBoxResult.OK; Close(); } private void BtnYes_Click(object sender, RoutedEventArgs e) { Result = MessageBoxResult.Yes; Close(); } private void BtnNo_Click(object sender, RoutedEventArgs e) { Result = MessageBoxResult.No; Close(); } private void BtnClose_Click(object sender, RoutedEventArgs e) { Result = MessageBoxResult.Cancel; Close(); } } }