-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWPFMessageBoxWindow.xaml.cs
126 lines (102 loc) · 3.84 KB
/
WPFMessageBoxWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Media;
using System.Windows;
using System.Windows.Input;
namespace MessageBoxUtils
{
/// <summary>
/// Interaction logic for WPFMessageBoxWindow.xaml
/// </summary>
public partial class WPFMessageBoxWindow
{
[ThreadStatic] private static WPFMessageBoxWindow _messageBoxWindow;
private MessageBoxViewModel _viewModel;
public WPFMessageBoxWindow()
{
InitializeComponent();
}
public static MessageBoxResult Show(
Action<Window> setOwner,
string messageBoxText,
string caption,
MessageBoxButton button,
MessageBoxImage icon,
MessageBoxResult defaultResult,
MessageBoxOptions options,
int timeout)
{
if ((options & MessageBoxOptions.DefaultDesktopOnly) == MessageBoxOptions.DefaultDesktopOnly)
{
}
if ((options & MessageBoxOptions.ServiceNotification) == MessageBoxOptions.ServiceNotification)
{
}
_messageBoxWindow = new WPFMessageBoxWindow();
setOwner(_messageBoxWindow);
PlayMessageBeep(icon);
_messageBoxWindow._viewModel = new MessageBoxViewModel(_messageBoxWindow, caption, messageBoxText, button,
icon, defaultResult, options, timeout);
_messageBoxWindow.DataContext = _messageBoxWindow._viewModel;
_messageBoxWindow.ShowDialog();
return _messageBoxWindow._viewModel.Result;
}
private static void PlayMessageBeep(MessageBoxImage icon)
{
switch (icon)
{
//case MessageBoxImage.Hand:
//case MessageBoxImage.Stop:
case MessageBoxImage.Error:
SystemSounds.Hand.Play();
break;
//case MessageBoxImage.Exclamation:
case MessageBoxImage.Warning:
SystemSounds.Exclamation.Play();
break;
case MessageBoxImage.Question:
SystemSounds.Question.Play();
break;
//case MessageBoxImage.Asterisk:
case MessageBoxImage.Information:
SystemSounds.Asterisk.Play();
break;
default:
SystemSounds.Beep.Play();
break;
}
}
protected override void OnSourceInitialized(EventArgs e)
{
// removes the application icon from the window top left corner
// this is different than just hiding it
WindowHelper.RemoveIcon(this);
switch (_viewModel.Options)
{
case MessageBoxOptions.None:
break;
case MessageBoxOptions.RightAlign:
WindowHelper.SetRightAligned(this);
break;
case MessageBoxOptions.RtlReading:
break;
case MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading:
break;
}
// disable close button if needed and remove resize menu items from the window system menu
var systemMenuHelper = new SystemMenuHelper(this);
if (_viewModel.ButtonOption == MessageBoxButton.YesNo)
systemMenuHelper.DisableCloseButton = true;
systemMenuHelper.RemoveResizeMenu = true;
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
_viewModel.EscapeCommand.Execute(null);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
_viewModel.CloseCommand.Execute(null);
}
}
}