From ede0dbde58c7844d9a33736f4f0bfe15d29a184d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=A9=9A=E9=8F=B5?= Date: Wed, 28 Jun 2023 23:21:12 +0800 Subject: [PATCH] Add shadow effect to messagebox 1.Add shadow effect to messagebox 2.If no window has focus, add a border to the pop-up messagebox control 3.Add draggable Messagebox 4.Add the input parameter owner to specify the parent form --- .../ExampleViews/BasicControlsExample.xaml.cs | 2 +- .../Controls/MessageBox/MessageBox.cs | 50 +++++++++---------- .../Controls/MessageBox/WPFMessageBox.cs | 42 +++++++++++++--- .../Styles/Styles.MessageBox.xaml | 5 +- 4 files changed, 62 insertions(+), 37 deletions(-) diff --git a/src/WPFDevelopers.Samples.Shared/ExampleViews/BasicControlsExample.xaml.cs b/src/WPFDevelopers.Samples.Shared/ExampleViews/BasicControlsExample.xaml.cs index c902ca13..94624ba0 100644 --- a/src/WPFDevelopers.Samples.Shared/ExampleViews/BasicControlsExample.xaml.cs +++ b/src/WPFDevelopers.Samples.Shared/ExampleViews/BasicControlsExample.xaml.cs @@ -96,7 +96,7 @@ private void btnInformation_Click(object sender, RoutedEventArgs e) private void btnWarning_Click(object sender, RoutedEventArgs e) { - MessageBox.Show("当前文件不存在!", "警告", MessageBoxImage.Warning); + MessageBox.Show("执行此操作可能导致文件无法打开!", "警告", MessageBoxImage.Warning, App.CurrentMainWindow); } private void btnError_Click(object sender, RoutedEventArgs e) diff --git a/src/WPFDevelopers.Shared/Controls/MessageBox/MessageBox.cs b/src/WPFDevelopers.Shared/Controls/MessageBox/MessageBox.cs index c0fa9bd7..c3da4790 100644 --- a/src/WPFDevelopers.Shared/Controls/MessageBox/MessageBox.cs +++ b/src/WPFDevelopers.Shared/Controls/MessageBox/MessageBox.cs @@ -1,71 +1,71 @@ using System.Linq; -using System.Security.Cryptography; using System.Windows; -using System.Windows.Controls; -using System.Windows.Documents; -using System.Windows.Media; -using System.Windows.Media.Media3D; -using System.Windows.Shapes; using WPFDevelopers.Helpers; -using WPFDevelopers.Utilities; namespace WPFDevelopers.Controls { public static class MessageBox { - public static MessageBoxResult Show(string messageBoxText) + public static MessageBoxResult Show(string messageBoxText,Window owner = null) { var msg = new WPFMessageBox(messageBoxText); - return GetWindow(msg); + return GetWindow(msg, owner); } - public static MessageBoxResult Show(string messageBoxText, string caption) + public static MessageBoxResult Show(string messageBoxText, string caption, Window owner = null) { var msg = new WPFMessageBox(messageBoxText, caption); - return GetWindow(msg); + return GetWindow(msg, owner); } - public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button) + public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, Window owner = null) { var msg = new WPFMessageBox(messageBoxText, caption, button); - return GetWindow(msg); + return GetWindow(msg, owner); } - public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxImage icon) + public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxImage icon, Window owner = null) { var msg = new WPFMessageBox(messageBoxText, caption, icon); - return GetWindow(msg); + return GetWindow(msg, owner); } public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, - MessageBoxImage icon) + MessageBoxImage icon, Window owner = null) { var msg = new WPFMessageBox(messageBoxText, caption, button, icon); - return GetWindow(msg); + return GetWindow(msg, owner); } - private static MessageBoxResult GetWindow(WPFMessageBox msg) + private static MessageBoxResult GetWindow(WPFMessageBox msg, Window owner = null) { - try + msg.WindowStartupLocation = WindowStartupLocation.CenterOwner; + if (owner != null) + { + msg.CreateMask(); + msg.Owner = owner; + msg.ShowDialog(); + } + else { - msg.WindowStartupLocation = WindowStartupLocation.CenterOwner; Window win = null; if (Application.Current.Windows.Count > 0) win = Application.Current.Windows.OfType().FirstOrDefault(o => o.IsActive); if (win != null) { + if (win.WindowState == WindowState.Minimized) + msg.BorderThickness = new Thickness(1); msg.CreateMask(); msg.Owner = win; msg.ShowDialog(); } else + { + msg.BorderThickness = new Thickness(1); msg.Show(); - return msg.Result; - } - catch (System.Exception) - { - throw; + } } + return msg.Result; } } } \ No newline at end of file diff --git a/src/WPFDevelopers.Shared/Controls/MessageBox/WPFMessageBox.cs b/src/WPFDevelopers.Shared/Controls/MessageBox/WPFMessageBox.cs index 8c3602e0..f9bc1abc 100644 --- a/src/WPFDevelopers.Shared/Controls/MessageBox/WPFMessageBox.cs +++ b/src/WPFDevelopers.Shared/Controls/MessageBox/WPFMessageBox.cs @@ -1,4 +1,9 @@ -using System; +#if NET40 +using Microsoft.Windows.Shell; +#else +using System.Windows.Shell; +# endif +using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -57,7 +62,6 @@ public WPFMessageBox(string message, string caption, MessageBoxButton button) { _titleString = caption; _messageString = message; - ; } public WPFMessageBox(string message, string caption, MessageBoxImage image) @@ -80,6 +84,7 @@ public WPFMessageBox(string message, string caption, MessageBoxButton button, Me public override void OnApplyTemplate() { base.OnApplyTemplate(); + _title = GetTemplateChild(TitleTemplateName) as TextBlock; _message = GetTemplateChild(MessageTemplateName) as TextBox; @@ -117,6 +122,25 @@ public override void OnApplyTemplate() BorderThickness = new Thickness(1); WindowStartupLocation = WindowStartupLocation.CenterScreen; } + +#if NET40 + var chrome = new WindowChrome + { + CaptionHeight = 40, + GlassFrameThickness = new Thickness(1), + }; + WindowChrome.SetIsHitTestVisibleInChrome(_closeButton, true); + WindowChrome.SetWindowChrome(this, chrome); +#else + var chrome = new WindowChrome + { + CaptionHeight = 40, + GlassFrameThickness = new Thickness(1), + UseAeroCaptionButtons = false + }; + WindowChrome.SetIsHitTestVisibleInChrome(_closeButton, true); + WindowChrome.SetWindowChrome(this, chrome); +#endif } private void _buttonOK_Click(object sender, RoutedEventArgs e) @@ -145,8 +169,6 @@ private void DisplayButtons(MessageBoxButton button) _cancelVisibility = Visibility.Visible; _okVisibility = Visibility.Visible; break; - //case MessageBoxButton.YesNoCancel: - // break; default: _okVisibility = Visibility.Visible; break; @@ -159,19 +181,23 @@ private void DisplayImage(MessageBoxImage image) { case MessageBoxImage.Warning: _geometry = (Geometry) Application.Current.TryFindResource("WD.WarningGeometry"); - _solidColorBrush = (SolidColorBrush) Application.Current.TryFindResource("WD.WarningSolidColorBrush"); + _solidColorBrush = + (SolidColorBrush) Application.Current.TryFindResource("WD.WarningSolidColorBrush"); break; case MessageBoxImage.Error: _geometry = (Geometry) Application.Current.TryFindResource("WD.ErrorGeometry"); - _solidColorBrush = (SolidColorBrush) Application.Current.TryFindResource("WD.DangerSolidColorBrush"); + _solidColorBrush = + (SolidColorBrush) Application.Current.TryFindResource("WD.DangerSolidColorBrush"); break; case MessageBoxImage.Information: _geometry = (Geometry) Application.Current.TryFindResource("WD.WarningGeometry"); - _solidColorBrush = (SolidColorBrush) Application.Current.TryFindResource("WD.SuccessSolidColorBrush"); + _solidColorBrush = + (SolidColorBrush) Application.Current.TryFindResource("WD.SuccessSolidColorBrush"); break; case MessageBoxImage.Question: _geometry = (Geometry) Application.Current.TryFindResource("WD.QuestionGeometry"); - _solidColorBrush = (SolidColorBrush) Application.Current.TryFindResource("WD.NormalSolidColorBrush"); + _solidColorBrush = + (SolidColorBrush) Application.Current.TryFindResource("WD.NormalSolidColorBrush"); break; } } diff --git a/src/WPFDevelopers.Shared/Styles/Styles.MessageBox.xaml b/src/WPFDevelopers.Shared/Styles/Styles.MessageBox.xaml index a201ba87..a768594a 100644 --- a/src/WPFDevelopers.Shared/Styles/Styles.MessageBox.xaml +++ b/src/WPFDevelopers.Shared/Styles/Styles.MessageBox.xaml @@ -19,7 +19,7 @@ - + @@ -47,8 +47,7 @@ Margin="0,6" HorizontalAlignment="Right" IsTabStop="False" - Style="{DynamicResource WD.WindowButtonStyle}" - ToolTip="Close"> + Style="{DynamicResource WD.WindowButtonStyle}">