using System; using System.Drawing; using System.Windows; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; namespace XP.Common.GeneralForm.Views; /// /// 自定义消息框,可替代标准 MessageBox /// public partial class HexMessageBox : Window { public MessageBoxResult Result { get; set; } public string MessageText { get => (string)GetValue(MessageTextProperty); set => SetValue(MessageTextProperty, value); } public static readonly DependencyProperty MessageTextProperty = DependencyProperty.Register("MessageText", typeof(string), typeof(HexMessageBox), new PropertyMetadata("")); public new ImageSource Icon { get => (ImageSource)GetValue(IconProperty); set => SetValue(IconProperty, value); } public new static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(HexMessageBox), new PropertyMetadata(null)); public MessageBoxButton MessageBoxButton { get => (MessageBoxButton)GetValue(MessageBoxButtonProperty); set => SetValue(MessageBoxButtonProperty, value); } public static readonly DependencyProperty MessageBoxButtonProperty = DependencyProperty.Register("MessageBoxButton", typeof(MessageBoxButton), typeof(HexMessageBox), new PropertyMetadata(MessageBoxButton.OK)); public HexMessageBox(string messageText, MessageBoxButton messageBoxButton, MessageBoxImage image) { InitializeComponent(); Left = (SystemParameters.PrimaryScreenWidth - Width) / 2; Top = (SystemParameters.PrimaryScreenHeight - Height) / 2; MessageText = messageText; IntPtr iconHandle = SystemIcons.Information.Handle; switch (image) { case MessageBoxImage.Error: iconHandle = SystemIcons.Error.Handle; break; case MessageBoxImage.Question: iconHandle = SystemIcons.Question.Handle; break; case MessageBoxImage.Warning: iconHandle = SystemIcons.Warning.Handle; break; case MessageBoxImage.Information: iconHandle = SystemIcons.Information.Handle; break; } Icon = Imaging.CreateBitmapSourceFromHIcon(iconHandle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); Icon.Freeze(); MessageBoxButton = messageBoxButton; Result = MessageBoxResult.None; Topmost = true; } /// /// 显示模态对话框并返回结果 /// public static MessageBoxResult Show(string messageText, MessageBoxButton messageBoxButton, MessageBoxImage image) { return Application.Current.Dispatcher.Invoke(() => { var window = new HexMessageBox(messageText, messageBoxButton, image); window.ShowDialog(); return window.Result; }); } /// /// 显示非模态消息 /// public static HexMessageBox ShowMessage(string messageText, MessageBoxImage image) { HexMessageBox? messageBox = null; Application.Current.Dispatcher.Invoke(() => { messageBox = new HexMessageBox(messageText, MessageBoxButton.OK, image); messageBox.Show(); }); return messageBox!; } private void Yes_Click(object sender, RoutedEventArgs e) { Result = MessageBoxResult.Yes; Close(); } private void No_Click(object sender, RoutedEventArgs e) { Result = MessageBoxResult.No; Close(); } private void OK_Click(object sender, RoutedEventArgs e) { Result = MessageBoxResult.OK; Close(); } private void Cancel_Click(object sender, RoutedEventArgs e) { Result = MessageBoxResult.Cancel; Close(); } }