Skip to content

Commit ede0dbd

Browse files
committed
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
1 parent f39fa88 commit ede0dbd

File tree

4 files changed

+62
-37
lines changed

4 files changed

+62
-37
lines changed

src/WPFDevelopers.Samples.Shared/ExampleViews/BasicControlsExample.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private void btnInformation_Click(object sender, RoutedEventArgs e)
9696

9797
private void btnWarning_Click(object sender, RoutedEventArgs e)
9898
{
99-
MessageBox.Show("当前文件不存在!", "警告", MessageBoxImage.Warning);
99+
MessageBox.Show("执行此操作可能导致文件无法打开!", "警告", MessageBoxImage.Warning, App.CurrentMainWindow);
100100
}
101101

102102
private void btnError_Click(object sender, RoutedEventArgs e)
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
11
using System.Linq;
2-
using System.Security.Cryptography;
32
using System.Windows;
4-
using System.Windows.Controls;
5-
using System.Windows.Documents;
6-
using System.Windows.Media;
7-
using System.Windows.Media.Media3D;
8-
using System.Windows.Shapes;
93
using WPFDevelopers.Helpers;
10-
using WPFDevelopers.Utilities;
114

125
namespace WPFDevelopers.Controls
136
{
147
public static class MessageBox
158
{
16-
public static MessageBoxResult Show(string messageBoxText)
9+
public static MessageBoxResult Show(string messageBoxText,Window owner = null)
1710
{
1811
var msg = new WPFMessageBox(messageBoxText);
19-
return GetWindow(msg);
12+
return GetWindow(msg, owner);
2013
}
2114

22-
public static MessageBoxResult Show(string messageBoxText, string caption)
15+
public static MessageBoxResult Show(string messageBoxText, string caption, Window owner = null)
2316
{
2417
var msg = new WPFMessageBox(messageBoxText, caption);
25-
return GetWindow(msg);
18+
return GetWindow(msg, owner);
2619
}
2720

28-
public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button)
21+
public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, Window owner = null)
2922
{
3023
var msg = new WPFMessageBox(messageBoxText, caption, button);
31-
return GetWindow(msg);
24+
return GetWindow(msg, owner);
3225
}
3326

34-
public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxImage icon)
27+
public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxImage icon, Window owner = null)
3528
{
3629
var msg = new WPFMessageBox(messageBoxText, caption, icon);
37-
return GetWindow(msg);
30+
return GetWindow(msg, owner);
3831
}
3932

4033
public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button,
41-
MessageBoxImage icon)
34+
MessageBoxImage icon, Window owner = null)
4235
{
4336
var msg = new WPFMessageBox(messageBoxText, caption, button, icon);
44-
return GetWindow(msg);
37+
return GetWindow(msg, owner);
4538
}
4639

47-
private static MessageBoxResult GetWindow(WPFMessageBox msg)
40+
private static MessageBoxResult GetWindow(WPFMessageBox msg, Window owner = null)
4841
{
49-
try
42+
msg.WindowStartupLocation = WindowStartupLocation.CenterOwner;
43+
if (owner != null)
44+
{
45+
msg.CreateMask();
46+
msg.Owner = owner;
47+
msg.ShowDialog();
48+
}
49+
else
5050
{
51-
msg.WindowStartupLocation = WindowStartupLocation.CenterOwner;
5251
Window win = null;
5352
if (Application.Current.Windows.Count > 0)
5453
win = Application.Current.Windows.OfType<Window>().FirstOrDefault(o => o.IsActive);
5554
if (win != null)
5655
{
56+
if (win.WindowState == WindowState.Minimized)
57+
msg.BorderThickness = new Thickness(1);
5758
msg.CreateMask();
5859
msg.Owner = win;
5960
msg.ShowDialog();
6061
}
6162
else
63+
{
64+
msg.BorderThickness = new Thickness(1);
6265
msg.Show();
63-
return msg.Result;
64-
}
65-
catch (System.Exception)
66-
{
67-
throw;
66+
}
6867
}
68+
return msg.Result;
6969
}
7070
}
7171
}

src/WPFDevelopers.Shared/Controls/MessageBox/WPFMessageBox.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using System;
1+
#if NET40
2+
using Microsoft.Windows.Shell;
3+
#else
4+
using System.Windows.Shell;
5+
# endif
6+
using System;
27
using System.Windows;
38
using System.Windows.Controls;
49
using System.Windows.Media;
@@ -57,7 +62,6 @@ public WPFMessageBox(string message, string caption, MessageBoxButton button)
5762
{
5863
_titleString = caption;
5964
_messageString = message;
60-
;
6165
}
6266

6367
public WPFMessageBox(string message, string caption, MessageBoxImage image)
@@ -80,6 +84,7 @@ public WPFMessageBox(string message, string caption, MessageBoxButton button, Me
8084
public override void OnApplyTemplate()
8185
{
8286
base.OnApplyTemplate();
87+
8388
_title = GetTemplateChild(TitleTemplateName) as TextBlock;
8489
_message = GetTemplateChild(MessageTemplateName) as TextBox;
8590

@@ -117,6 +122,25 @@ public override void OnApplyTemplate()
117122
BorderThickness = new Thickness(1);
118123
WindowStartupLocation = WindowStartupLocation.CenterScreen;
119124
}
125+
126+
#if NET40
127+
var chrome = new WindowChrome
128+
{
129+
CaptionHeight = 40,
130+
GlassFrameThickness = new Thickness(1),
131+
};
132+
WindowChrome.SetIsHitTestVisibleInChrome(_closeButton, true);
133+
WindowChrome.SetWindowChrome(this, chrome);
134+
#else
135+
var chrome = new WindowChrome
136+
{
137+
CaptionHeight = 40,
138+
GlassFrameThickness = new Thickness(1),
139+
UseAeroCaptionButtons = false
140+
};
141+
WindowChrome.SetIsHitTestVisibleInChrome(_closeButton, true);
142+
WindowChrome.SetWindowChrome(this, chrome);
143+
#endif
120144
}
121145

122146
private void _buttonOK_Click(object sender, RoutedEventArgs e)
@@ -145,8 +169,6 @@ private void DisplayButtons(MessageBoxButton button)
145169
_cancelVisibility = Visibility.Visible;
146170
_okVisibility = Visibility.Visible;
147171
break;
148-
//case MessageBoxButton.YesNoCancel:
149-
// break;
150172
default:
151173
_okVisibility = Visibility.Visible;
152174
break;
@@ -159,19 +181,23 @@ private void DisplayImage(MessageBoxImage image)
159181
{
160182
case MessageBoxImage.Warning:
161183
_geometry = (Geometry) Application.Current.TryFindResource("WD.WarningGeometry");
162-
_solidColorBrush = (SolidColorBrush) Application.Current.TryFindResource("WD.WarningSolidColorBrush");
184+
_solidColorBrush =
185+
(SolidColorBrush) Application.Current.TryFindResource("WD.WarningSolidColorBrush");
163186
break;
164187
case MessageBoxImage.Error:
165188
_geometry = (Geometry) Application.Current.TryFindResource("WD.ErrorGeometry");
166-
_solidColorBrush = (SolidColorBrush) Application.Current.TryFindResource("WD.DangerSolidColorBrush");
189+
_solidColorBrush =
190+
(SolidColorBrush) Application.Current.TryFindResource("WD.DangerSolidColorBrush");
167191
break;
168192
case MessageBoxImage.Information:
169193
_geometry = (Geometry) Application.Current.TryFindResource("WD.WarningGeometry");
170-
_solidColorBrush = (SolidColorBrush) Application.Current.TryFindResource("WD.SuccessSolidColorBrush");
194+
_solidColorBrush =
195+
(SolidColorBrush) Application.Current.TryFindResource("WD.SuccessSolidColorBrush");
171196
break;
172197
case MessageBoxImage.Question:
173198
_geometry = (Geometry) Application.Current.TryFindResource("WD.QuestionGeometry");
174-
_solidColorBrush = (SolidColorBrush) Application.Current.TryFindResource("WD.NormalSolidColorBrush");
199+
_solidColorBrush =
200+
(SolidColorBrush) Application.Current.TryFindResource("WD.NormalSolidColorBrush");
175201
break;
176202
}
177203
}

src/WPFDevelopers.Shared/Styles/Styles.MessageBox.xaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<Setter Property="UseLayoutRounding" Value="True" />
2020
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
2121
<Setter Property="TextOptions.TextRenderingMode" Value="ClearType" />
22-
<Setter Property="WindowStyle" Value="None" />
22+
<Setter Property="WindowStyle" Value="ToolWindow" />
2323
<Setter Property="FontFamily" Value="{StaticResource WD.NormalFontFamily}" />
2424
<Setter Property="Template">
2525
<Setter.Value>
@@ -47,8 +47,7 @@
4747
Margin="0,6"
4848
HorizontalAlignment="Right"
4949
IsTabStop="False"
50-
Style="{DynamicResource WD.WindowButtonStyle}"
51-
ToolTip="Close">
50+
Style="{DynamicResource WD.WindowButtonStyle}">
5251
<Path
5352
Width="10"
5453
Height="10"

0 commit comments

Comments
 (0)