WPF 实现带蒙版的 MessageBox 消息提示框
作者:WPFDevelopersOrg
原文链接:
- 框架使用大于等于.NET40;
- Visual Studio 2022;
- 项目使用 MIT 开源许可协议;
- Nuget Install-Package WPFDevelo 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;
一、Me 代码如下;
using System; using Sy; using Sy; using Sy; using Sy; using Sy.Controls; using Sy.Media; namespace WPFDevelo.Controls { public static class MessageBox { public static MessageBoxResult Show(string messageBoxText) { var msg = new WPFMessageBox(messageBoxText); return GetWindow(msg); } public static MessageBoxResult Show(string messageBoxText, string caption) { var msg = new WPFMessageBox(messageBoxText, caption); return GetWindow(msg); } public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button) { var msg = new WPFMessageBox(messageBoxText, caption, button); return GetWindow(msg); } public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxImage icon) { var msg = new WPFMessageBox(messageBoxText, caption, icon); return GetWindow(msg); } public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon) { var msg = new WPFMessageBox(messageBoxText, caption,button,icon); return GetWindow(msg); } static MessageBoxResult GetWindow(WPFMessageBox msg) { m = WindowS; Window win = null; if > 0) win = A;Window>().FirstOrDefault(o => o.IsActive); if (win != null) { var layer = new Grid() { Background = new SolidColorBru(128, 0, 0, 0)) }; UIElement original = win.Content as UIElement; win.Content = null; var container = new Grid(); con(original); con(layer); win.Content = container; m = win; m(); con(); win.Content = original; } else m(); return m; } } }
二、S 代码如下;
<ResourceDictionary xmlns="; xmlns:x="; xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:wpfsc="clr-namespace:WPFDevelo.Controls"> <Re; <ResourceDictionary Source="../Themes/Basic;/> <ResourceDictionary Source="../Themes/Basic;/> </Re; <Style TargetType="{x:Type wpfsc:WPFMessageBox}"> <Setter Property="Foreground" Value="{DynamicResource PrimaryTextSolidColorBrush}" /> <Setter Property="Background" Value="{DynamicResource WhiteSolidColorBrush}" /> <Setter Property="BorderBrush" Value="{DynamicResource PrimaryNormalSolidColorBrush}" /> <Setter Property="SizeToContent" Value="WidthAndHeight" /> <Setter Property="ResizeMode" Value="NoResize" /> <Setter Property="ShowInTaskbar" Value="False" /> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="UseLayoutRounding" Value="True" /> <Setter Property="Tex; Value="Display" /> <Setter Property="Tex; Value="ClearType" /> <Setter Property="WindowStyle" Value="None" /> <Setter Property="FontFamily" Value="{DynamicResource NormalFontFamily}" /> <Setter Property="Template"> <Se; <ControlTemplate TargetType="{x:Type wpfsc:WPFMessageBox}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition /> <RowDefinition/> <; <Grid Grid.Row="0"> <DockPanel Margin="20,0,0,0"> <TextBlock x:Name="PART_Title" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="{DynamicResource TitleFontSize}" Foreground="{DynamicResource PrimaryTextSolidColorBrush}"/> <Button Name="PART_CloseButton" Margin="0,6" ToolTip="Close" HorizontalAlignment="Right" IsTabStop="False" Style="{DynamicResource WindowButtonStyle}"> <Path Width="10" Height="10" HorizontalAlignment="Center" VerticalAlignment="Center" Data="{DynamicResource PathMetroWindowClose}" Fill="{DynamicResource PrimaryTextSolidColorBrush}" Stretch="Fill" /> </Button> </DockPanel> </Grid> <Grid Grid.Row="1" Margin="20"> <DockPanel> <Path x:Name="PART_Path" Data="{DynamicResource PathInformation}" Fill="{DynamicResource PrimaryNormalSolidColorBrush}" Height="25" Width="25" Stretch="Fill"></Path> <TextBlock x:Name="PART_Message" TextWrapping="Wrap" MaxWidth="500" Width="Auto" VerticalAlignment="Center" FontSize="{DynamicResource NormalFontSize}" Padding="10,0" Foreground="{DynamicResource RegularTextSolidColorBrush}"/> </DockPanel> </Grid> <Grid Grid.Row="2" Margin="140,20,10,10"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> <Button x:Name="PART_ButtonCancel" Content="取消" Visibility="Collapsed"/> <Button x:Name="PART_ButtonOK" Style="{DynamicResource PrimaryButton}" Margin="10,0,0,0" Content="确认"/> </StackPanel> </Grid> </Grid> </Border> </ControlTemplate> </Se; </Setter> </Style> </ResourceDictionary>
三、WPFMe 代码如下;
using System; using Sy; using Sy; using Sy; using Sy; using Sy.Controls; using Sy.Media; using Sy.Shapes; namespace WPFDevelo.Controls { [TemplatePart(Name = TitleTemplateName, Type = typeof(TextBlock))] [TemplatePart(Name = CloseButtonTemplateName, Type = typeof(Button))] [TemplatePart(Name = MessageTemplateName, Type = typeof(TextBlock))] [TemplatePart(Name = ButtonCancelTemplateName, Type = typeof(Button))] [TemplatePart(Name = ButtonCancelTemplateName, Type = typeof(Button))] [TemplatePart(Name = PathTemplateName, Type = typeof(Path))] public sealed class WPFMessageBox : Window { private const string TitleTemplateName = "PART_Title"; private const string CloseButtonTemplateName = "PART_CloseButton"; private const string MessageTemplateName = "PART_Message"; private const string ButtonCancelTemplateName = "PART_ButtonCancel"; private const string ButtonOKTemplateName = "PART_ButtonOK"; private const string PathTemplateName = "PART_Path"; private string _messageString; private string _titleString; private Geometry _geometry; private SolidColorBrush _solidColorBrush; private Visibility _cancelVisibility = Vi; private Visibility _okVisibility; private TextBlock _title; private TextBlock _message; private Button _closeButton; private Button _buttonCancel; private Button _buttonOK; private Path _path; static WPFMessageBox() { De(typeof(WPFMessageBox), new FrameworkPropertyMetadata(typeof(WPFMessageBox))); } public override void OnApplyTemplate() { ba(); _title = GetTemplateChild(TitleTemplateName) as TextBlock; _message = GetTemplateChild(MessageTemplateName) as TextBlock; if (_title == null || _message == null) throw new InvalidOperationException("the title or message control is null!"); _ = _titleString; _me = _messageString; _path = GetTemplateChild(PathTemplateName) as Path; if (_path != null) { _ = _geometry; _ = _solidColorBrush; } _closeButton = GetTemplateChild(CloseButtonTemplateName) as Button; if (_closeButton != null) _clo += _closeButton_Click; _buttonCancel = GetTemplateChild(ButtonCancelTemplateName) as Button; if (_buttonCancel != null) { _bu = _cancelVisibility; _bu += _buttonCancel_Click; } _buttonOK = GetTemplateChild(ButtonOKTemplateName) as Button; if (_buttonOK != null) { _bu = _okVisibility; _bu += _buttonOK_Click; } if (Owner == null) { BorderThickness = new Thickness(1); WindowStartupLocation = WindowS; } } private void _buttonOK_Click(object sender, RoutedEventArgs e) { Result = Me; Close(); } private void _buttonCancel_Click(object sender, RoutedEventArgs e) { Result = Me; Close(); } private void _closeButton_Click(object sender, RoutedEventArgs e) { Close(); } protected override void OnClosed(EventArgs e) { ba(e); if (Owner == null) return; var grid = Owner.Content as Grid; UIElement original = Vi(grid, 0) as UIElement; grid.C(original); Owner.Content = original; } public MessageBoxResult Result { get; set; } public WPFMessageBox(string message) { _messageString = message; } public WPFMessageBox(string message, string caption) { _titleString = caption; _messageString = message; } public WPFMessageBox(string message, string caption, MessageBoxButton button) { _titleString = caption; _messageString = message; ; } public WPFMessageBox(string message, string caption, MessageBoxImage image) { _titleString = caption; _messageString = message; DisplayImage(image); } public WPFMessageBox(string message, string caption, MessageBoxButton button, MessageBoxImage image) { _titleString = caption; _messageString = message; DisplayImage(image); DisplayButtons(button); } private void DisplayButtons(MessageBoxButton button) { switch (button) { case Me: case Me: _cancelVisibility = Vi; _okVisibility = Vi; break; //case MeCancel: // break; default: _okVisibility = Vi; break; } } private void DisplayImage(MessageBoxImage image) { switch (image) { case Me: _geometry = A["PathWarning"] as Geometry; _solidColorBrush = A["WarningSolidColorBrush"] as SolidColorBrush; break; case Me: _geometry = A["PathError"] as Geometry; _solidColorBrush = A["DangerSolidColorBrush"] as SolidColorBrush; break; case Me: _geometry = A["PathWarning"] as Geometry; _solidColorBrush = A["SuccessSolidColorBrush"] as SolidColorBrush; break; case Me: _geometry = A["PathQuestion"] as Geometry; _solidColorBrush = A["PrimaryNormalSolidColorBrush"] as SolidColorBrush; break; default: break; } } } }
Nuget Install-Package WPFDevelo