-
Notifications
You must be signed in to change notification settings - Fork 8
/
ModernMessageBox.xaml.cs
154 lines (145 loc) · 5.13 KB
/
ModernMessageBox.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ValheimSaveShield
{
/// <summary>
/// Interaction logic for ModernMessageBox.xaml
/// </summary>
public partial class ModernMessageBox : Window
{
private MessageBoxResult _result;
private static MessageBoxResult _defaultResult = MessageBoxResult.Cancel;
public MessageBoxResult Result
{
get
{
return _result;
}
}
public ModernMessageBox(Window owner) : this()
{
if (owner.IsLoaded)
{
this.Owner = owner;
WindowStartupLocation = WindowStartupLocation.CenterOwner;
}
}
public ModernMessageBox()
{
InitializeComponent();
WindowStartupLocation = WindowStartupLocation.CenterOwner;
_result = _defaultResult;
}
public MessageBoxResult Show(string message) {
return Show(message, "", MessageBoxButton.OK, MessageBoxImage.Information, _defaultResult);
}
public MessageBoxResult Show(string message, string title, MessageBoxButton buttons, MessageBoxImage image)
{
return ShowDialog(message, title, buttons, image, _defaultResult);
}
public MessageBoxResult Show(string message, string title, MessageBoxButton buttons, MessageBoxImage image, MessageBoxResult defaultResult)
{
return ShowDialog(message, title, buttons, image, defaultResult);
}
public MessageBoxResult ShowDialog(string message, string title, MessageBoxButton buttons, MessageBoxImage image, MessageBoxResult defaultResult)
{
_result = defaultResult;
Title = title;
lblMessage.Text = message;
if (buttons != MessageBoxButton.OK && buttons != MessageBoxButton.OKCancel)
{
btnOK.Visibility = Visibility.Collapsed;
}
if (buttons != MessageBoxButton.YesNo && buttons != MessageBoxButton.YesNoCancel)
{
btnYes.Visibility = Visibility.Collapsed;
btnNo.Visibility = Visibility.Collapsed;
}
if (buttons != MessageBoxButton.OKCancel && buttons != MessageBoxButton.YesNoCancel)
{
btnCancel.Visibility = Visibility.Collapsed;
}
if (image == MessageBoxImage.Asterisk)
{
imgIcon.Source = ToImageSource(SystemIcons.Asterisk);
}
else if (image == MessageBoxImage.Error)
{
imgIcon.Source = ToImageSource(SystemIcons.Error);
}
else if (image == MessageBoxImage.Exclamation)
{
imgIcon.Source = ToImageSource(SystemIcons.Exclamation);
}
else if (image == MessageBoxImage.Hand)
{
imgIcon.Source = ToImageSource(SystemIcons.Hand);
}
else if (image == MessageBoxImage.Information)
{
imgIcon.Source = ToImageSource(SystemIcons.Information);
}
else if (image == MessageBoxImage.Question)
{
imgIcon.Source = ToImageSource(SystemIcons.Question);
}
else if (image == MessageBoxImage.Stop)
{
imgIcon.Source = ToImageSource(SystemIcons.Hand);
}
else if (image == MessageBoxImage.Warning)
{
imgIcon.Source = ToImageSource(SystemIcons.Warning);
}
else if (image == MessageBoxImage.None)
{
imgIcon.Visibility = Visibility.Collapsed;
}
base.ShowDialog();
return _result;
}
private ImageSource ToImageSource(Icon icon)
{
return Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
this._result = MessageBoxResult.OK;
Close();
}
private void btnYes_Click(object sender, RoutedEventArgs e)
{
this._result = MessageBoxResult.Yes;
Close();
}
private void btnNo_Click(object sender, RoutedEventArgs e)
{
this._result = MessageBoxResult.No;
Close();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this._result = MessageBoxResult.Cancel;
Close();
}
private void Window_ContentRendered(object sender, EventArgs e)
{
InvalidateMeasure();
InvalidateVisual();
}
}
}