WPF 实现带蒙版的 MessageBox 消息提示框

打印 上一主题 下一主题

主题 922|帖子 922|积分 2766

WPF 实现带蒙版的 MessageBox 消息提示框

WPF 实现带蒙版的 MessageBox 消息提示框
作者:WPFDevelopersOrg
原文链接:    https://github.com/WPFDevelopersOrg/WPFDevelopers.Minimal


  • 框架使用大于等于.NET40;
  • Visual Studio 2022;
  • 项目使用 MIT 开源许可协议;
  • Nuget Install-Package WPFDevelopers.Minimal 3.2.6-preview
MessageBox

  • 实现MessageBox的Show五种方法;

    • Show(string messageBoxText) 传入Msg参数;
    • Show(string messageBoxText, string caption) 传入Msg与标题参数;
    • Show(string messageBoxText, string caption, MessageBoxButton button) 传入Msg与标题、操作按钮参数;
    • Show(string messageBoxText, string caption, MessageBoxImage icon) 传入Msg与标题、消息图片参数;
    • Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon) 传入Msg与标题、操作按钮、消息图片参数;

  • 拿到父级Window窗体的内容Content,放入一个Grid里,再在容器里放入一个半透明的Grid,最后将整个Grid赋给父级Window窗体的内容Content;
一、MessageBox.cs 代码如下;
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. namespace WPFDevelopers.Minimal.Controls
  9. {
  10.     public static class MessageBox
  11.     {
  12.         public static MessageBoxResult Show(string messageBoxText)
  13.         {
  14.             var msg = new WPFMessageBox(messageBoxText);
  15.             return GetWindow(msg);
  16.         }
  17.         public static MessageBoxResult Show(string messageBoxText, string caption)
  18.         {
  19.             var msg = new WPFMessageBox(messageBoxText, caption);
  20.             return GetWindow(msg);
  21.         }
  22.         public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button)
  23.         {
  24.             var msg = new WPFMessageBox(messageBoxText, caption, button);
  25.             return GetWindow(msg);
  26.         }
  27.         public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxImage icon)
  28.         {
  29.             var msg = new WPFMessageBox(messageBoxText, caption, icon);
  30.             return GetWindow(msg);
  31.         }
  32.         public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon)
  33.         {
  34.             var msg = new WPFMessageBox(messageBoxText, caption,button,icon);
  35.             return GetWindow(msg);
  36.         }
  37.         static MessageBoxResult GetWindow(WPFMessageBox msg)
  38.         {
  39.             msg.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  40.             Window win = null;
  41.             if (Application.Current.Windows.Count > 0)
  42.                 win = Application.Current.Windows.OfType<Window>().FirstOrDefault(o => o.IsActive);
  43.             if (win != null)
  44.             {
  45.                 var layer = new Grid() { Background = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0)) };
  46.                 UIElement original = win.Content as UIElement;
  47.                 win.Content = null;
  48.                 var container = new Grid();
  49.                 container.Children.Add(original);
  50.                 container.Children.Add(layer);
  51.                 win.Content = container;
  52.                 msg.Owner = win;
  53.                 msg.ShowDialog();
  54.                 container.Children.Clear();
  55.                 win.Content = original;
  56.             }
  57.             else
  58.                 msg.Show();
  59.             return msg.Result;
  60.         }
  61.     }
  62. }
复制代码
二、Styles.MessageBox.xaml 代码如下;
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3.                     xmlns:sys="clr-namespace:System;assembly=mscorlib"
  4.                     xmlns:wpfsc="clr-namespace:WPFDevelopers.Minimal.Controls">
  5.    
  6.     <ResourceDictionary.MergedDictionaries>
  7.         <ResourceDictionary Source="../Themes/Basic/ControlBasic.xaml"/>
  8.         <ResourceDictionary Source="../Themes/Basic/Animations.xaml"/>
  9.     </ResourceDictionary.MergedDictionaries>
  10.    
  11. </ResourceDictionary>
  12.                
复制代码
三、WPFMessageBox.cs 代码如下;
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using System.Windows.Shapes;
  9. namespace WPFDevelopers.Minimal.Controls
  10. {
  11.     [TemplatePart(Name = TitleTemplateName, Type = typeof(TextBlock))]
  12.     [TemplatePart(Name = CloseButtonTemplateName, Type = typeof(Button))]
  13.     [TemplatePart(Name = MessageTemplateName, Type = typeof(TextBlock))]
  14.     [TemplatePart(Name = ButtonCancelTemplateName, Type = typeof(Button))]
  15.     [TemplatePart(Name = ButtonCancelTemplateName, Type = typeof(Button))]
  16.     [TemplatePart(Name = PathTemplateName, Type = typeof(Path))]
  17.     public sealed class WPFMessageBox : Window
  18.     {
  19.         private const string TitleTemplateName = "PART_Title";
  20.         private const string CloseButtonTemplateName = "PART_CloseButton";
  21.         private const string MessageTemplateName = "PART_Message";
  22.         private const string ButtonCancelTemplateName = "PART_ButtonCancel";
  23.         private const string ButtonOKTemplateName = "PART_ButtonOK";
  24.         private const string PathTemplateName = "PART_Path";
  25.         private string _messageString;
  26.         private string _titleString;
  27.         private Geometry _geometry;
  28.         private SolidColorBrush _solidColorBrush;
  29.         private Visibility _cancelVisibility = Visibility.Collapsed;
  30.         private Visibility _okVisibility;
  31.         private TextBlock _title;
  32.         private TextBlock _message;
  33.         private Button _closeButton;
  34.         private Button _buttonCancel;
  35.         private Button _buttonOK;
  36.         private Path _path;
  37.         static WPFMessageBox()
  38.         {
  39.             DefaultStyleKeyProperty.OverrideMetadata(typeof(WPFMessageBox), new FrameworkPropertyMetadata(typeof(WPFMessageBox)));
  40.         }
  41.         public override void OnApplyTemplate()
  42.         {
  43.             base.OnApplyTemplate();
  44.             _title = GetTemplateChild(TitleTemplateName) as TextBlock;
  45.             _message = GetTemplateChild(MessageTemplateName) as TextBlock;
  46.             if (_title == null || _message == null)
  47.                 throw new InvalidOperationException("the title or message control is null!");
  48.             _title.Text = _titleString;
  49.             _message.Text = _messageString;
  50.             _path = GetTemplateChild(PathTemplateName) as Path;
  51.             if (_path != null)
  52.             {
  53.                 _path.Data = _geometry;
  54.                 _path.Fill = _solidColorBrush;
  55.             }
  56.             _closeButton = GetTemplateChild(CloseButtonTemplateName) as Button;
  57.             if (_closeButton != null)
  58.                 _closeButton.Click += _closeButton_Click;
  59.             _buttonCancel = GetTemplateChild(ButtonCancelTemplateName) as Button;
  60.             if (_buttonCancel != null)
  61.             {
  62.                 _buttonCancel.Visibility = _cancelVisibility;
  63.                 _buttonCancel.Click += _buttonCancel_Click;
  64.             }
  65.             _buttonOK = GetTemplateChild(ButtonOKTemplateName) as Button;
  66.             if (_buttonOK != null)
  67.             {
  68.                 _buttonOK.Visibility = _okVisibility;
  69.                 _buttonOK.Click += _buttonOK_Click;
  70.             }
  71.             if (Owner == null)
  72.             {
  73.                 BorderThickness = new Thickness(1);
  74.                 WindowStartupLocation = WindowStartupLocation.CenterScreen;
  75.             }
  76.         }
  77.         private void _buttonOK_Click(object sender, RoutedEventArgs e)
  78.         {
  79.             Result = MessageBoxResult.OK;
  80.             Close();
  81.         }
  82.         private void _buttonCancel_Click(object sender, RoutedEventArgs e)
  83.         {
  84.             Result = MessageBoxResult.Cancel;
  85.             Close();
  86.         }
  87.         private void _closeButton_Click(object sender, RoutedEventArgs e)
  88.         {
  89.             Close();
  90.         }
  91.         protected override void OnClosed(EventArgs e)
  92.         {
  93.             base.OnClosed(e);
  94.             if (Owner == null)
  95.                 return;
  96.             var grid = Owner.Content as Grid;
  97.             UIElement original = VisualTreeHelper.GetChild(grid, 0) as UIElement;
  98.             grid.Children.Remove(original);
  99.             Owner.Content = original;
  100.         }
  101.         public MessageBoxResult Result { get; set; }
  102.         public WPFMessageBox(string message)
  103.         {
  104.             
  105.             _messageString = message;
  106.         }
  107.         public WPFMessageBox(string message, string caption)
  108.         {
  109.             _titleString = caption;
  110.             _messageString = message;
  111.            
  112.         }
  113.         public WPFMessageBox(string message, string caption, MessageBoxButton button)
  114.         {
  115.             _titleString = caption;
  116.             _messageString = message; ;
  117.         }
  118.         public WPFMessageBox(string message, string caption, MessageBoxImage image)
  119.         {
  120.             _titleString = caption;
  121.             _messageString = message;
  122.             DisplayImage(image);
  123.         }
  124.         public WPFMessageBox(string message, string caption, MessageBoxButton button, MessageBoxImage image)
  125.         {
  126.             _titleString = caption;
  127.             _messageString = message;
  128.             DisplayImage(image);
  129.             DisplayButtons(button);
  130.         }
  131.         private void DisplayButtons(MessageBoxButton button)
  132.         {
  133.             switch (button)
  134.             {
  135.                 case MessageBoxButton.OKCancel:
  136.                 case MessageBoxButton.YesNo:
  137.                     _cancelVisibility = Visibility.Visible;
  138.                     _okVisibility = Visibility.Visible;
  139.                     break;
  140.                 //case MessageBoxButton.YesNoCancel:
  141.                 //    break;
  142.                 default:
  143.                     _okVisibility = Visibility.Visible;
  144.                     break;
  145.             }
  146.         }
  147.         private void DisplayImage(MessageBoxImage image)
  148.         {
  149.             switch (image)
  150.             {
  151.                 case MessageBoxImage.Warning:
  152.                     _geometry = Application.Current.Resources["PathWarning"] as Geometry;
  153.                     _solidColorBrush = Application.Current.Resources["WarningSolidColorBrush"] as SolidColorBrush;
  154.                     break;
  155.                 case MessageBoxImage.Error:
  156.                     _geometry = Application.Current.Resources["PathError"] as Geometry;
  157.                     _solidColorBrush = Application.Current.Resources["DangerSolidColorBrush"] as SolidColorBrush;
  158.                     break;
  159.                 case MessageBoxImage.Information:
  160.                     _geometry = Application.Current.Resources["PathWarning"] as Geometry;
  161.                     _solidColorBrush = Application.Current.Resources["SuccessSolidColorBrush"] as SolidColorBrush;
  162.                     break;
  163.                 case MessageBoxImage.Question:
  164.                     _geometry = Application.Current.Resources["PathQuestion"] as Geometry;
  165.                     _solidColorBrush = Application.Current.Resources["PrimaryNormalSolidColorBrush"] as SolidColorBrush;
  166.                     break;
  167.                 default:
  168.                     break;
  169.             }
  170.         }
  171.     }
  172. }
复制代码
Nuget Install-Package WPFDevelopers.Minimal



其他基础控件

1.Window
2.Button
3.CheckBox
4.ComboBox
5.DataGrid
6.DatePicker
7.Expander
8.GroupBox
9.ListBox
10.ListView
11.Menu
12.PasswordBox
13.TextBox
14.RadioButton
15.ToggleButton
16.Slider
17.TreeView
18.TabControl

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

卖不甜枣

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表