Skip to content

Commit

Permalink
Add shadow effect to messagebox
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yanjinhuagood committed Jun 28, 2023
1 parent f39fa88 commit ede0dbd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 25 additions & 25 deletions src/WPFDevelopers.Shared/Controls/MessageBox/MessageBox.cs
Original file line number Diff line number Diff line change
@@ -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<Window>().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;
}
}
}
42 changes: 34 additions & 8 deletions src/WPFDevelopers.Shared/Controls/MessageBox/WPFMessageBox.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -57,7 +62,6 @@ public WPFMessageBox(string message, string caption, MessageBoxButton button)
{
_titleString = caption;
_messageString = message;
;
}

public WPFMessageBox(string message, string caption, MessageBoxImage image)
Expand All @@ -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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/WPFDevelopers.Shared/Styles/Styles.MessageBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<Setter Property="UseLayoutRounding" Value="True" />
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
<Setter Property="TextOptions.TextRenderingMode" Value="ClearType" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="WindowStyle" Value="ToolWindow" />
<Setter Property="FontFamily" Value="{StaticResource WD.NormalFontFamily}" />
<Setter Property="Template">
<Setter.Value>
Expand Down Expand Up @@ -47,8 +47,7 @@
Margin="0,6"
HorizontalAlignment="Right"
IsTabStop="False"
Style="{DynamicResource WD.WindowButtonStyle}"
ToolTip="Close">
Style="{DynamicResource WD.WindowButtonStyle}">
<Path
Width="10"
Height="10"
Expand Down

0 comments on commit ede0dbd

Please sign in to comment.