diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 44992023..45869b4b 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -149,7 +149,7 @@ namespace Netch.Forms // 如果勾选了关闭时退出,自动点击退出按钮 else { - Exit(true); + Exit(); } } } @@ -234,9 +234,7 @@ namespace Netch.Forms { MessageBoxX.Show(i18N.Translate("Please press Stop button first")); - Visible = true; - ShowInTaskbar = true; // 显示在系统任务栏 - WindowState = FormWindowState.Normal; // 还原窗体 + NotifyIcon_MouseDoubleClick(null, null); return; } @@ -248,13 +246,6 @@ namespace Netch.Forms ControlFun(); } - for (var i = 0; i < 16; i++) - { - if (State == State.Waiting || State == State.Stopped) - break; - Thread.Sleep(250); - } - SaveConfigs(); State = State.Terminating; } @@ -320,19 +311,21 @@ namespace Netch.Forms } } - private void SpeedPictureBox_Click(object sender, EventArgs e) + private async void SpeedPictureBox_Click(object sender, EventArgs e) { Enabled = false; StatusText(i18N.Translate("Testing")); - Task.Run(() => + try + { + await Task.Run(TestServer); + } + finally { - TestServer(); - Enabled = true; StatusText(i18N.Translate("Test done")); Refresh(); - }); + } } private void EditModePictureBox_Click(object sender, EventArgs e) diff --git a/Netch/Forms/Mode/Process.Designer.cs b/Netch/Forms/Mode/Process.Designer.cs index 2dd383f1..7b49a61f 100644 --- a/Netch/Forms/Mode/Process.Designer.cs +++ b/Netch/Forms/Mode/Process.Designer.cs @@ -1,4 +1,7 @@ -namespace Netch.Forms.Mode +using System; +using System.Windows.Forms; + +namespace Netch.Forms.Mode { partial class Process { @@ -31,8 +34,6 @@ System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Process)); this.ConfigurationGroupBox = new System.Windows.Forms.GroupBox(); this.UseCustomFilenameBox = new System.Windows.Forms.CheckBox(); - this.TimeDataButton = new System.Windows.Forms.RadioButton(); - this.StaySameButton = new System.Windows.Forms.RadioButton(); this.FilenameLabel = new System.Windows.Forms.Label(); this.FilenameTextBox = new System.Windows.Forms.TextBox(); this.ScanButton = new System.Windows.Forms.Button(); @@ -50,8 +51,6 @@ // ConfigurationGroupBox // this.ConfigurationGroupBox.Controls.Add(this.UseCustomFilenameBox); - this.ConfigurationGroupBox.Controls.Add(this.TimeDataButton); - this.ConfigurationGroupBox.Controls.Add(this.StaySameButton); this.ConfigurationGroupBox.Controls.Add(this.FilenameLabel); this.ConfigurationGroupBox.Controls.Add(this.FilenameTextBox); this.ConfigurationGroupBox.Controls.Add(this.ScanButton); @@ -69,35 +68,13 @@ // UseCustomFilenameBox // this.UseCustomFilenameBox.AutoSize = true; - this.UseCustomFilenameBox.Location = new System.Drawing.Point(84, 82); + this.UseCustomFilenameBox.Location = new System.Drawing.Point(84, 76); this.UseCustomFilenameBox.Name = "UseCustomFilenameBox"; this.UseCustomFilenameBox.Size = new System.Drawing.Size(152, 21); this.UseCustomFilenameBox.TabIndex = 9; this.UseCustomFilenameBox.Text = "Use Custom Filename"; this.UseCustomFilenameBox.UseVisualStyleBackColor = true; - this.UseCustomFilenameBox.CheckedChanged += new System.EventHandler(this.UseCustomFileNameBox_CheckedChanged); - // - // TimeDataButton - // - this.TimeDataButton.AutoSize = true; - this.TimeDataButton.Location = new System.Drawing.Point(197, 106); - this.TimeDataButton.Name = "TimeDataButton"; - this.TimeDataButton.Size = new System.Drawing.Size(84, 21); - this.TimeDataButton.TabIndex = 8; - this.TimeDataButton.TabStop = true; - this.TimeDataButton.Text = "Time data"; - this.TimeDataButton.UseVisualStyleBackColor = true; - // - // StaySameButton - // - this.StaySameButton.AutoSize = true; - this.StaySameButton.Location = new System.Drawing.Point(84, 106); - this.StaySameButton.Name = "StaySameButton"; - this.StaySameButton.Size = new System.Drawing.Size(107, 21); - this.StaySameButton.TabIndex = 7; - this.StaySameButton.TabStop = true; - this.StaySameButton.Text = "Stay the same"; - this.StaySameButton.UseVisualStyleBackColor = true; + this.UseCustomFilenameBox.CheckedChanged += new System.EventHandler(this.UseCustomFilenameBox_CheckedChanged); // // FilenameLabel // @@ -156,9 +133,9 @@ // this.RuleListBox.FormattingEnabled = true; this.RuleListBox.ItemHeight = 17; - this.RuleListBox.Location = new System.Drawing.Point(6, 134); + this.RuleListBox.Location = new System.Drawing.Point(6, 100); this.RuleListBox.Name = "RuleListBox"; - this.RuleListBox.Size = new System.Drawing.Size(328, 123); + this.RuleListBox.Size = new System.Drawing.Size(328, 157); this.RuleListBox.TabIndex = 2; this.RuleListBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.RuleListBox_MouseUp); // @@ -168,6 +145,7 @@ this.RemarkTextBox.Name = "RemarkTextBox"; this.RemarkTextBox.Size = new System.Drawing.Size(250, 23); this.RemarkTextBox.TabIndex = 1; + this.RemarkTextBox.TextChanged += new System.EventHandler(this.RemarkTextBox_TextChanged); // // RemarkLabel // @@ -195,9 +173,9 @@ this.ClientSize = new System.Drawing.Size(364, 397); this.Controls.Add(this.ControlButton); this.Controls.Add(this.ConfigurationGroupBox); - this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte) (134))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; - this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.Icon = ((System.Drawing.Icon) (resources.GetObject("$this.Icon"))); this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.MaximizeBox = false; this.Name = "Process"; @@ -210,7 +188,6 @@ this.ProcessGroupBox.ResumeLayout(false); this.ProcessGroupBox.PerformLayout(); this.ResumeLayout(false); - } #endregion @@ -226,8 +203,6 @@ private System.Windows.Forms.Button ControlButton; private System.Windows.Forms.Label FilenameLabel; private System.Windows.Forms.TextBox FilenameTextBox; - private System.Windows.Forms.RadioButton StaySameButton; - private System.Windows.Forms.RadioButton TimeDataButton; private System.Windows.Forms.CheckBox UseCustomFilenameBox; } } \ No newline at end of file diff --git a/Netch/Forms/Mode/Process.cs b/Netch/Forms/Mode/Process.cs index 0664c53e..af3409ed 100644 --- a/Netch/Forms/Mode/Process.cs +++ b/Netch/Forms/Mode/Process.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.WindowsAPICodePack.Dialogs; using Netch.Utils; @@ -31,8 +34,6 @@ namespace Netch.Forms.Mode mode.Rule.ForEach(i => RuleListBox.Items.Add(i)); EditMode = true; - StaySameButton.Enabled = false; - TimeDataButton.Enabled = false; FilenameTextBox.Enabled = false; FilenameLabel.Enabled = false; UseCustomFilenameBox.Enabled = false; @@ -105,30 +106,12 @@ namespace Netch.Forms.Mode RemarkLabel.Text = i18N.Translate(RemarkLabel.Text); FilenameLabel.Text = i18N.Translate(FilenameLabel.Text); UseCustomFilenameBox.Text = i18N.Translate(UseCustomFilenameBox.Text); - StaySameButton.Text = i18N.Translate(StaySameButton.Text); - TimeDataButton.Text = i18N.Translate(TimeDataButton.Text); AddButton.Text = i18N.Translate(AddButton.Text); ScanButton.Text = i18N.Translate(ScanButton.Text); ControlButton.Text = i18N.Translate(ControlButton.Text); - if (Global.Settings.ModeFileNameType == 0) - { - UseCustomFilenameBox.Checked = true; - StaySameButton.Enabled = false; - TimeDataButton.Enabled = false; - } - else if (Global.Settings.ModeFileNameType == 1) - { - FilenameTextBox.Enabled = false; - FilenameLabel.Enabled = false; - StaySameButton.Checked = true; - } - else - { - FilenameTextBox.Enabled = false; - FilenameLabel.Enabled = false; - TimeDataButton.Checked = true; - } + FilenameTextBox.Enabled = false; + FilenameLabel.Enabled = false; } private void ModeForm_FormClosing(object sender, FormClosingEventArgs e) @@ -141,44 +124,49 @@ namespace Netch.Forms.Mode /// private void RuleListBox_MouseUp(object sender, MouseEventArgs e) { - var strip = new ContextMenuStrip(); - strip.Items.Add(i18N.Translate("Delete")); + RuleListBox.SelectedIndex = RuleListBox.IndexFromPoint(e.X, e.Y); + if (RuleListBox.SelectedIndex == -1) + return; if (e.Button == MouseButtons.Right) { + var strip = new ContextMenuStrip(); + strip.Items.Add(i18N.Translate("Delete")); + strip.Items[0].Click += deleteRule_Click; strip.Show(RuleListBox, e.Location); //鼠标右键按下弹出菜单 - strip.MouseClick += deleteRule_Click; } } void deleteRule_Click(object sender, EventArgs e) { - if (RuleListBox.SelectedIndex != -1) - { - RuleListBox.Items.RemoveAt(RuleListBox.SelectedIndex); - } + if (RuleListBox.SelectedIndex == -1) return; + RuleListBox.Items.RemoveAt(RuleListBox.SelectedIndex); } - private void AddButton_Click(object sender, EventArgs e) + private async void AddButton_Click(object sender, EventArgs e) { - if (!string.IsNullOrWhiteSpace(ProcessNameTextBox.Text)) + await Task.Run(() => { - var process = ProcessNameTextBox.Text; - if (!process.EndsWith(".exe")) + if (!string.IsNullOrWhiteSpace(ProcessNameTextBox.Text)) { - process += ".exe"; - } + var process = ProcessNameTextBox.Text; + if (!process.EndsWith(".exe")) + { + process += ".exe"; + } - if (!RuleListBox.Items.Contains(process)) + if (!RuleListBox.Items.Contains(process)) + { + RuleListBox.Items.Add(process); + } + + RuleListBox.SelectedIndex = RuleListBox.Items.IndexOf(process); + ProcessNameTextBox.Text = string.Empty; + } + else { - RuleListBox.Items.Add(process); + MessageBoxX.Show(i18N.Translate("Please enter an process name (xxx.exe)")); } - - ProcessNameTextBox.Text = string.Empty; - } - else - { - MessageBoxX.Show(i18N.Translate("Please enter an process name (xxx.exe)")); - } + }); } private void ScanButton_Click(object sender, EventArgs e) @@ -204,7 +192,6 @@ namespace Netch.Forms.Mode if (EditMode) { // 编辑模式 - if (RuleListBox.Items.Count != 0) { var mode = new Models.Mode @@ -244,38 +231,18 @@ namespace Netch.Forms.Mode } else { - // 自定义文件名 - if (UseCustomFilenameBox.Checked) - { - Global.Settings.ModeFileNameType = 0; - } - // 使用和备注一致的文件名 - else if (StaySameButton.Checked) - { - Global.Settings.ModeFileNameType = 1; - FilenameTextBox.Text = RemarkTextBox.Text; - } - // 使用时间数据作为文件名 - else - { - Global.Settings.ModeFileNameType = 2; - FilenameTextBox.Text = ((long) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds).ToString(); - } - Configuration.Save(); - if (!string.IsNullOrWhiteSpace(RemarkTextBox.Text)) { - if (Global.Settings.ModeFileNameType == 0 && string.IsNullOrWhiteSpace(FilenameTextBox.Text)) + if (string.IsNullOrWhiteSpace(FilenameTextBox.Text)) { MessageBoxX.Show(i18N.Translate("Please enter a mode filename")); return; } - var ModeFilename = Path.Combine("mode", FilenameTextBox.Text); - + var modeFilename = Path.Combine("mode", FilenameTextBox.Text); // 如果文件已存在,返回 - if (File.Exists(ModeFilename + ".txt")) + if (File.Exists(modeFilename + ".txt")) { MessageBoxX.Show(i18N.Translate("File already exists.\n Please Change the filename")); return; @@ -306,7 +273,7 @@ namespace Netch.Forms.Mode Directory.CreateDirectory("mode"); } - File.WriteAllText(ModeFilename + ".txt", text); + File.WriteAllText(modeFilename + ".txt", text); MessageBoxX.Show(i18N.Translate("Mode added successfully")); @@ -325,22 +292,27 @@ namespace Netch.Forms.Mode } } - private void UseCustomFileNameBox_CheckedChanged(object sender, EventArgs e) + private async void RemarkTextBox_TextChanged(object sender, EventArgs e) { - if (UseCustomFilenameBox.Checked) + await Task.Run(() => { - StaySameButton.Enabled = false; - TimeDataButton.Enabled = false; - FilenameTextBox.Enabled = true; - FilenameLabel.Enabled = true; - } - else - { - StaySameButton.Enabled = true; - TimeDataButton.Enabled = true; - FilenameTextBox.Enabled = false; - FilenameLabel.Enabled = false; - } + if (!UseCustomFilenameBox.Checked) + { + var invalidFileChars = Path.GetInvalidFileNameChars(); + var fileName = new StringBuilder(RemarkTextBox.Text); + foreach (var c in invalidFileChars) + { + fileName.Replace(c, '_'); + } + + FilenameTextBox.Text = fileName.ToString(); + } + }); + } + + private void UseCustomFilenameBox_CheckedChanged(object sender, EventArgs e) + { + FilenameTextBox.Enabled = FilenameLabel.Enabled = ((CheckBox) sender).Checked; } } } \ No newline at end of file diff --git a/Netch/Models/Setting.cs b/Netch/Models/Setting.cs index 89958ffe..d9973a78 100644 --- a/Netch/Models/Setting.cs +++ b/Netch/Models/Setting.cs @@ -103,12 +103,6 @@ namespace Netch.Models /// public int RequestTimeout = 10000; - /// - /// 使用何种模式文件名 - /// 0 为自定义文件名,1 为使用和备注一致的文件名,2 为使用时间数据作为文件名 - /// - public int ModeFileNameType = 1; - /// /// HTTP 本地端口 /// diff --git a/Netch/Utils/i18N.cs b/Netch/Utils/i18N.cs index 31aaf795..e96eebf0 100644 --- a/Netch/Utils/i18N.cs +++ b/Netch/Utils/i18N.cs @@ -11,7 +11,6 @@ namespace Netch.Utils { public static class i18N { - /// /// 数据 /// @@ -50,6 +49,12 @@ namespace Netch.Utils // 从外置文件中加载语言 text = File.ReadAllText($"i18n\\{langCode}"); } + else + { + Logging.Error($"无法找到语言 {langCode}, 使用系统语言"); + // 加载系统语言 + LangCode = CultureInfo.CurrentCulture.Name; + } var data = JsonConvert.DeserializeObject>(text);