-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateDialog.cs
More file actions
191 lines (167 loc) · 7.33 KB
/
UpdateDialog.cs
File metadata and controls
191 lines (167 loc) · 7.33 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
namespace MidiForwarder
{
public class UpdateDialog : Form
{
private readonly string currentVersion;
private readonly string latestVersion;
private readonly string releaseNotes;
// 控件引用,用于语言切换时更新文本
private Label? versionLabel;
private Label? releaseNotesTitleLabel;
private CheckBox? ignoreVersionCheckBox;
private Button? yesButton;
private Button? noButton;
public bool ShouldOpenDownloadPage { get; private set; } = false;
public bool ShouldIgnoreThisVersion { get; private set; } = false;
public UpdateDialog(string currentVersion, string latestVersion, string releaseNotes)
{
this.currentVersion = currentVersion;
this.latestVersion = latestVersion;
this.releaseNotes = releaseNotes;
// 订阅语言切换事件
LocalizationManager.LanguageChanged += OnLanguageChanged;
InitializeComponents();
}
private void OnLanguageChanged(object? sender, EventArgs e)
{
if (!IsDisposed && InvokeRequired)
{
Invoke(new Action(UpdateLocalizedText));
}
else if (!IsDisposed)
{
UpdateLocalizedText();
}
}
private void UpdateLocalizedText()
{
if (IsDisposed) return;
Text = LocalizationManager.GetString("UpdateAvailableTitle");
if (versionLabel != null)
{
versionLabel.Text = string.Format(LocalizationManager.GetString("UpdateAvailableMessageCurrentVersion"), currentVersion) + "\n" +
string.Format(LocalizationManager.GetString("UpdateAvailableMessageLatestVersion"), latestVersion);
}
if (releaseNotesTitleLabel != null)
releaseNotesTitleLabel.Text = LocalizationManager.GetString("UpdateReleaseNotes");
if (ignoreVersionCheckBox != null)
ignoreVersionCheckBox.Text = LocalizationManager.GetString("UpdateIgnoreThisVersion");
if (yesButton != null)
yesButton.Text = LocalizationManager.GetString("UpdateDialogYesButton");
if (noButton != null)
noButton.Text = LocalizationManager.GetString("UpdateDialogNoButton");
}
private void InitializeComponents()
{
// 设置对话框属性
Text = LocalizationManager.GetString("UpdateAvailableTitle");
Size = new Size(520, 480);
StartPosition = FormStartPosition.CenterScreen;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = true;
Padding = new Padding(20);
// 版本信息标签(小字号)
versionLabel = new Label
{
Text = string.Format(LocalizationManager.GetString("UpdateAvailableMessageCurrentVersion"), currentVersion) + "\n" +
string.Format(LocalizationManager.GetString("UpdateAvailableMessageLatestVersion"), latestVersion),
AutoSize = false,
Size = new Size(460, 35),
Location = new Point(20, 15),
Font = new Font("Microsoft YaHei", 9),
TextAlign = ContentAlignment.TopLeft
};
// 更新内容标题
releaseNotesTitleLabel = new Label
{
Text = LocalizationManager.GetString("UpdateReleaseNotes"),
AutoSize = true,
Location = new Point(20, 55),
Font = new Font("Microsoft YaHei", 10, FontStyle.Bold)
};
// 更新内容文本框(支持滚动,不可选中)
var releaseNotesTextBox = new TextBox
{
Text = releaseNotes,
Multiline = true,
ReadOnly = true,
ScrollBars = ScrollBars.Vertical,
Size = new Size(467, 315),
Location = new Point(20, 77),
Font = new Font("Microsoft YaHei", 9),
BackColor = SystemColors.Window,
BorderStyle = BorderStyle.FixedSingle,
TabStop = false
};
// 禁止文本选中
releaseNotesTextBox.MouseDown += (s, e) => { };
releaseNotesTextBox.MouseUp += (s, e) => { };
releaseNotesTextBox.KeyDown += (s, e) => e.Handled = true;
releaseNotesTextBox.GotFocus += (s, e) => ActiveControl = null;
// 忽略此版本复选框
ignoreVersionCheckBox = new CheckBox
{
Text = LocalizationManager.GetString("UpdateIgnoreThisVersion"),
AutoSize = true,
Location = new Point(20, 400),
Font = new Font("Microsoft YaHei", 9)
};
// 是按钮
yesButton = new Button
{
Text = LocalizationManager.GetString("UpdateDialogYesButton"),
Size = new Size(100, 32),
Location = new Point(280, 400),
DialogResult = DialogResult.Yes,
Font = new Font("Microsoft YaHei", 9)
};
yesButton.Click += (s, e) =>
{
ShouldOpenDownloadPage = true;
ShouldIgnoreThisVersion = ignoreVersionCheckBox?.Checked ?? false;
DialogResult = DialogResult.Yes;
Close();
};
// 否按钮
noButton = new Button
{
Text = LocalizationManager.GetString("UpdateDialogNoButton"),
Size = new Size(100, 32),
Location = new Point(390, 400),
DialogResult = DialogResult.No,
Font = new Font("Microsoft YaHei", 9)
};
noButton.Click += (s, e) =>
{
ShouldOpenDownloadPage = false;
ShouldIgnoreThisVersion = ignoreVersionCheckBox?.Checked ?? false;
DialogResult = DialogResult.No;
Close();
};
// 添加控件
if (versionLabel != null) Controls.Add(versionLabel);
if (releaseNotesTitleLabel != null) Controls.Add(releaseNotesTitleLabel);
Controls.Add(releaseNotesTextBox);
if (ignoreVersionCheckBox != null) Controls.Add(ignoreVersionCheckBox);
if (yesButton != null) Controls.Add(yesButton);
if (noButton != null) Controls.Add(noButton);
// 设置默认按钮
AcceptButton = yesButton;
CancelButton = noButton;
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
// 取消订阅语言切换事件,防止内存泄漏
LocalizationManager.LanguageChanged -= OnLanguageChanged;
base.OnFormClosed(e);
}
public static (bool shouldDownload, bool shouldIgnore) ShowUpdateDialog(string currentVersion, string latestVersion, string releaseNotes)
{
using var dialog = new UpdateDialog(currentVersion, latestVersion, releaseNotes);
dialog.ShowDialog();
return (dialog.ShouldOpenDownloadPage, dialog.ShouldIgnoreThisVersion);
}
}
}