-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassEditor.xaml.cs
346 lines (320 loc) · 13.4 KB
/
ClassEditor.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
namespace signup
{
/// <summary>
/// ClassEditor.xaml 的交互逻辑
/// </summary>
public partial class ClassEditor : HandyControl.Controls.GlowWindow
{
private bool RichTextBox_ChangedByCode = false;
public event EventHandler LoadSettingsFromEditor;
public ClassEditor()
{
InitializeComponent();
}
//写入文件内容实现
private void Editor_CreateFileContent(string filePath, string Content)
{
try
{
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.Write(Content);
}
}
catch (Exception ex)
{
MessageBox.Show($"写入文件时出错:{ex.Message}。", "友情提醒:");
}
}
//读取配置文件实现
private void Editor_LoadSettings(string filePath, bool Editor_Init)
{
if (!File.Exists(filePath))
{
MessageBox.Show("指定的配置文件不存在,本次导入操作将被终止。", "友情提醒:");
return;
}
else
{
string Settings_Load = File.ReadAllText(filePath);
//验证配置文件合法性
for (int cptime = 1; cptime <= 63; cptime++)
{
if (Settings_Load.Contains("[" + cptime.ToString() + "]"))
{
continue;
}
else
{
MessageBox.Show("在读入配置文件时出错:无法解析指定的配置文件。", "友情提醒:");
if (Editor_Init == true)
{
this.Close();
}
return;
}
}
Global.Editor_Temp_Settings[0] = "Done";
for (int cptime = 1; cptime <= 63; cptime++)
{
Global.Editor_Temp_Settings[cptime] = "";
}
//读取配置文件算法实现
int Settings_Load_Length = Settings_Load.Length;
string Target_Button = "";
string Target_Type = "None";
for (int cptime = 0; cptime <= Settings_Load_Length - 1; cptime++)
{
if (Settings_Load[cptime] == '[')
{
Target_Button = "";
Target_Type = "Button";
continue;
}
else if (Settings_Load[cptime] == ']')
{
Target_Type = "Content";
continue;
}
else
{
if (Target_Type == "Button")
{
Target_Button += Settings_Load[cptime];
continue;
}
else if (Target_Type == "Content")
{
try
{
Global.Editor_Temp_Settings[int.Parse(Target_Button)] += Settings_Load[cptime];
}
catch (FormatException ex)
{
MessageBox.Show($"在读入配置文件时出错:对于{Target_Button},无法在窗口中找到该按钮。", "友情提醒:");
return;
}
catch(IndexOutOfRangeException ex)
{
MessageBox.Show($"在读入配置文件时出错:无法解析指定的配置文件。", "友情提醒:");
return;
}
continue;
}
}
}
Global.Editor_SettingsNow = Settings_Load;
//更新按钮
for (int cptime = 1; cptime <= 63; cptime++)
{
Button button = this.FindName("Btn" + cptime) as Button;
if (button != null)
{
button.Content = Global.Editor_Temp_Settings[cptime];
}
else
{
MessageBox.Show("在查找按钮控件时发生错误!", "友情提醒:");
return;
}
}
}
}
//"直接编辑本机方案"按钮被按下事件
private void Btn_EditSelf_Click(object sender, RoutedEventArgs e)
{
Editor_CreateFileContent(@"D:\signup\Editor_Settings.ini", Global.SettingsNow);
Editor_LoadSettings(@"D:\signup\Editor_Settings.ini",true);
try
{
File.Delete(@"D:\signup\Editor_Settings.ini");
}
catch(IOException ex)
{
MessageBox.Show($"在处理编辑器临时文件时出错:{ex.Message}。", "友情提醒:");
}
RichTextBox_ChangedByCode = true;
Editor_RichTextBox.Document.Blocks.Clear();
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Run(Global.SettingsNow));
RichTextBox_ChangedByCode = true;
Editor_RichTextBox.Document.Blocks.Add(paragraph);
Editor_TabContrl.SelectedIndex = 1;
}
//"导入其它配置文件"按钮被按下事件
private void Btn_EditOthers_Click(object sender,RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "选择文件";
openFileDialog.Filter="配置文件 (*.ini)|*.ini";
if(openFileDialog.ShowDialog()==true)
{
string filePath = openFileDialog.FileName;
Editor_LoadSettings(filePath,true);
RichTextBox_ChangedByCode = true;
Editor_RichTextBox.Document.Blocks.Clear();
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Run(Global.Editor_SettingsNow));
RichTextBox_ChangedByCode = true;
Editor_RichTextBox.Document.Blocks.Add(paragraph);
Editor_TabContrl.SelectedIndex = 1;
}
else
{
return;
}
}
//滚动条进行滚动时执行的操作
private void Editor_ScrollBar_Scroll(object sender, ScrollEventArgs e)
{
Editor_RichTextBox.ScrollToVerticalOffset(e.NewValue);
}
//文本框内容更新时执行的事件
private void Editor_RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
//调整滚动条的样式
UpdateScrollBarProperties();
//检查是否是由后端代码更新内容
if(RichTextBox_ChangedByCode==true)
{
RichTextBox_ChangedByCode = false;
return;
}
//验证更改内容合法性
FlowDocument document = Editor_RichTextBox.Document;
TextRange textRange = new TextRange(document.ContentStart, document.ContentEnd);
string text = textRange.Text;
for(int cptime=1;cptime<=63;cptime++)
{
if(text.Contains("["+cptime.ToString()+"]"))
{
continue;
}
else
{
RichTextBox_ChangedByCode = true;
Editor_RichTextBox.Document.Blocks.Clear();
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Run(Global.Editor_SettingsNow));
RichTextBox_ChangedByCode = true;
Editor_RichTextBox.Document.Blocks.Add(paragraph);
MessageBox.Show("更改配置文件时出错:更改内容不合法。", "友情提醒:");
return;
}
}
//更新按钮和临时配置文件
Editor_CreateFileContent(@"D:\signup\Temp_Editor_Settings.ini", text);
Editor_LoadSettings(@"D:\signup\Temp_Editor_Settings.ini",false);
try
{
File.Delete(@"D:\signup\Temp_Editor_Settings.ini");
}
catch (IOException ex)
{
MessageBox.Show($"在处理编辑器临时文件时出错:{ex.Message}。", "友情提醒:");
}
Global.Editor_SettingsNow = text;
}
private void UpdateScrollBarProperties()
{
// 确保控件已初始化
if (Editor_RichTextBox == null || Editor_ScrollBar == null)
{
return;
}
// 获取文本框内容高度
double contentHeight = Editor_RichTextBox.ExtentHeight;
// 设置滚动条的最大值
Editor_ScrollBar.Maximum = contentHeight - Editor_RichTextBox.ViewportHeight;
// 更新滚动条的ViewportSize(可视区域大小)
Editor_ScrollBar.ViewportSize = Editor_RichTextBox.ViewportHeight;
}
//签到按钮被点击事件
private void Editor_Btn_Signup_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
button.Content = Editor_TextBox.Text;
string SelectedButton = "";
for(int cptime=4;cptime<=button.Name.Length ;cptime++)
{
SelectedButton +=button.Name[cptime - 1];
}
string input = Global.Editor_SettingsNow;
int startpoint = input.IndexOf("["+SelectedButton+"]");
int endpoint = input.IndexOf("[" + (int.Parse(SelectedButton)+1).ToString() + "]");
if((startpoint!=-1 && endpoint!=-1)||(SelectedButton=="63"))
{
if(SelectedButton=="63")
{
if(startpoint!=-1)
{
input=input.Remove(startpoint+4);
input += button.Content;
Global.Editor_SettingsNow = input;
RichTextBox_ChangedByCode = true;
Editor_RichTextBox.Document.Blocks.Clear();
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Run(input));
RichTextBox_ChangedByCode = true;
Editor_RichTextBox.Document.Blocks.Add(paragraph);
Editor_TabContrl.SelectedIndex = 1;
return;
}
else
{
MessageBox.Show($"在配置文件中查找[{SelectedButton}]时出错,请重新载入配置文件!", "友情提醒:");
return;
}
}
else
{
string beforestr = input.Remove(startpoint + 2 + SelectedButton.Length);
string afterstr = input.Substring(endpoint);
string sum=beforestr+button.Content+afterstr;
Global.Editor_SettingsNow = sum;
RichTextBox_ChangedByCode = true;
Editor_RichTextBox.Document.Blocks.Clear();
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Run(sum));
RichTextBox_ChangedByCode = true;
Editor_RichTextBox.Document.Blocks.Add(paragraph);
return;
}
}
else
{
MessageBox.Show($"在配置文件中查找[{SelectedButton}]时出错,请重新载入配置文件!","友情提醒:");
return;
}
}
//保存到本机使用按钮被点击事件
private void Btn_Save_Click(object sender, RoutedEventArgs e)
{
Editor_CreateFileContent(@"D:\signup\settings.ini", Global.Editor_SettingsNow);
LoadSettingsFromEditor.Invoke(this, EventArgs.Empty);
MessageBox.Show("保存成功!", "友情提醒:");
}
//导出到本地使用按钮被点击事件
private void Btn_ToLocal_Click(object sender,RoutedEventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "选择要保存的路径";
saveFileDialog.Filter = "配置文件(*.ini)|*.ini";
saveFileDialog.DefaultExt = "ini";
saveFileDialog.AddExtension = true;
if(saveFileDialog.ShowDialog()==true)
{
string Filepath = saveFileDialog.FileName;
Editor_CreateFileContent(Filepath, Global.Editor_SettingsNow);
MessageBox.Show("保存成功!", "友情提醒:");
}
}
}
}