-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomMessageBox.xaml.cs
More file actions
118 lines (103 loc) · 3.86 KB
/
CustomMessageBox.xaml.cs
File metadata and controls
118 lines (103 loc) · 3.86 KB
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
using System;
using System.Windows;
using System.Windows.Input;
namespace VivaldiUpdater
{
public partial class CustomMessageBox : Window
{
public enum MessageBoxButtons
{
OK,
OKCancel,
YesNo
}
public enum MessageBoxResult
{
None,
OK,
Cancel,
Yes,
No
}
public MessageBoxResult Result { get; private set; } = MessageBoxResult.None;
private CustomMessageBox()
{
InitializeComponent();
}
public static MessageBoxResult Show(string message, string title = "Message", MessageBoxButtons buttons = MessageBoxButtons.OK, Window owner = null)
{
var msgBox = new CustomMessageBox();
msgBox.TitleText.Text = title;
msgBox.MessageText.Text = message;
// Set Owner if provided, otherwise try to find the main window
Window targetOwner = owner;
if (targetOwner == null && Application.Current != null && Application.Current.MainWindow != null && Application.Current.MainWindow.IsVisible)
{
targetOwner = Application.Current.MainWindow;
}
if (targetOwner != null)
{
msgBox.Owner = targetOwner;
msgBox.Width = targetOwner.ActualWidth;
msgBox.Height = targetOwner.ActualHeight;
msgBox.Left = targetOwner.Left;
msgBox.Top = targetOwner.Top;
}
else
{
msgBox.WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
// Configure buttons
switch (buttons)
{
case MessageBoxButtons.OK:
msgBox.OkButton.Visibility = Visibility.Visible;
msgBox.CancelButton.Visibility = Visibility.Collapsed;
msgBox.OkButton.Content = Properties.Resources.text_ok ?? "确定";
break;
case MessageBoxButtons.OKCancel:
msgBox.OkButton.Visibility = Visibility.Visible;
msgBox.CancelButton.Visibility = Visibility.Visible;
msgBox.OkButton.Content = Properties.Resources.text_ok ?? "确定";
msgBox.CancelButton.Content = Properties.Resources.text_cancel ?? "取消";
break;
case MessageBoxButtons.YesNo:
msgBox.OkButton.Visibility = Visibility.Visible;
msgBox.CancelButton.Visibility = Visibility.Visible;
msgBox.OkButton.Content = Properties.Resources.text_yes ?? "是";
msgBox.CancelButton.Content = Properties.Resources.text_no ?? "否";
break;
}
msgBox.ShowDialog();
return msgBox.Result;
}
private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
{
DragMove();
}
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
if (OkButton.Content.ToString() == "是")
Result = MessageBoxResult.Yes;
else
Result = MessageBoxResult.OK;
Close();
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
if (CancelButton.Content.ToString() == "否")
Result = MessageBoxResult.No;
else
Result = MessageBoxResult.Cancel;
Close();
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Result = MessageBoxResult.Cancel;
Close();
}
}
}