From 0d511b032598cce80575003bdd511c78b7ac38b9 Mon Sep 17 00:00:00 2001 From: LovelyWei <16489620+LovelyWei@users.noreply.github.com> Date: Tue, 18 Feb 2020 15:22:32 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=80=E6=9C=BA?= =?UTF-8?q?=E8=87=AA=E5=90=AF=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Netch/Forms/SettingForm.Designer.cs | 15 +++++++++++- Netch/Forms/SettingForm.cs | 38 +++++++++++++++++++++++++++++ Netch/Models/Setting.cs | 5 ++++ Netch/Netch.csproj | 12 +++++++++ Netch/Resources/zh-CN | 1 + 5 files changed, 70 insertions(+), 1 deletion(-) diff --git a/Netch/Forms/SettingForm.Designer.cs b/Netch/Forms/SettingForm.Designer.cs index a72ef5ece2..5e23daeeec 100644 --- a/Netch/Forms/SettingForm.Designer.cs +++ b/Netch/Forms/SettingForm.Designer.cs @@ -50,6 +50,7 @@ private void InitializeComponent() this.ControlButton = new System.Windows.Forms.Button(); this.GlobalBypassIPsButton = new System.Windows.Forms.Button(); this.BehaviorGroupBox = new System.Windows.Forms.GroupBox(); + this.RunAtStartup = new System.Windows.Forms.CheckBox(); this.MinimizeWhenStartedCheckBox = new System.Windows.Forms.CheckBox(); this.ProfileCount_Label = new System.Windows.Forms.Label(); this.ProfileCount_TextBox = new System.Windows.Forms.TextBox(); @@ -261,6 +262,7 @@ private void InitializeComponent() // // BehaviorGroupBox // + this.BehaviorGroupBox.Controls.Add(this.RunAtStartup); this.BehaviorGroupBox.Controls.Add(this.MinimizeWhenStartedCheckBox); this.BehaviorGroupBox.Controls.Add(this.ProfileCount_Label); this.BehaviorGroupBox.Controls.Add(this.ProfileCount_TextBox); @@ -275,6 +277,16 @@ private void InitializeComponent() this.BehaviorGroupBox.TabStop = false; this.BehaviorGroupBox.Text = "Behavior"; // + // RunAtStartup + // + this.RunAtStartup.AutoSize = true; + this.RunAtStartup.Location = new System.Drawing.Point(120, 130); + this.RunAtStartup.Name = "RunAtStartup"; + this.RunAtStartup.Size = new System.Drawing.Size(109, 21); + this.RunAtStartup.TabIndex = 11; + this.RunAtStartup.Text = "Run at startup"; + this.RunAtStartup.UseVisualStyleBackColor = true; + // // MinimizeWhenStartedCheckBox // this.MinimizeWhenStartedCheckBox.AutoSize = true; @@ -305,7 +317,7 @@ private void InitializeComponent() // CheckUpdateWhenOpenedCheckBox // this.CheckUpdateWhenOpenedCheckBox.AutoSize = true; - this.CheckUpdateWhenOpenedCheckBox.Location = new System.Drawing.Point(120, 130); + this.CheckUpdateWhenOpenedCheckBox.Location = new System.Drawing.Point(120, 157); this.CheckUpdateWhenOpenedCheckBox.Name = "CheckUpdateWhenOpenedCheckBox"; this.CheckUpdateWhenOpenedCheckBox.Size = new System.Drawing.Size(190, 21); this.CheckUpdateWhenOpenedCheckBox.TabIndex = 8; @@ -406,5 +418,6 @@ private void InitializeComponent() private System.Windows.Forms.Label ProfileCount_Label; private System.Windows.Forms.TextBox ProfileCount_TextBox; private System.Windows.Forms.CheckBox MinimizeWhenStartedCheckBox; + private System.Windows.Forms.CheckBox RunAtStartup; } } \ No newline at end of file diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index 5940862610..3ee47e919a 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -1,6 +1,8 @@ using System; +using System.Collections; using System.Net; using System.Windows.Forms; +using TaskScheduler; namespace Netch.Forms { @@ -58,6 +60,7 @@ private void SettingForm_Load(object sender, EventArgs e) StartWhenOpenedCheckBox.Checked = Global.Settings.StartWhenOpened; CheckUpdateWhenOpenedCheckBox.Checked = Global.Settings.CheckUpdateWhenOpened; MinimizeWhenStartedCheckBox.Checked = Global.Settings.MinimizeWhenStarted; + RunAtStartup.Checked = Global.Settings.RunAtStartup; Socks5PortTextBox.Text = Global.Settings.Socks5LocalPort.ToString(); HTTPPortTextBox.Text = Global.Settings.HTTPLocalPort.ToString(); @@ -74,6 +77,7 @@ private void SettingForm_Load(object sender, EventArgs e) StopWhenExitedCheckBox.Text = Utils.i18N.Translate(StopWhenExitedCheckBox.Text); StartWhenOpenedCheckBox.Text = Utils.i18N.Translate(StartWhenOpenedCheckBox.Text); MinimizeWhenStartedCheckBox.Text = Utils.i18N.Translate(MinimizeWhenStartedCheckBox.Text); + RunAtStartup.Text = Utils.i18N.Translate(RunAtStartup.Text); CheckUpdateWhenOpenedCheckBox.Text = Utils.i18N.Translate(CheckUpdateWhenOpenedCheckBox.Text); ProfileCount_Label.Text = Utils.i18N.Translate(ProfileCount_Label.Text); @@ -136,6 +140,40 @@ private void ControlButton_Click(object sender, EventArgs e) Global.Settings.StartWhenOpened = StartWhenOpenedCheckBox.Checked; Global.Settings.CheckUpdateWhenOpened = CheckUpdateWhenOpenedCheckBox.Checked; Global.Settings.MinimizeWhenStarted = MinimizeWhenStartedCheckBox.Checked; + Global.Settings.RunAtStartup = RunAtStartup.Checked; + + // 开机自启判断 + TaskSchedulerClass scheduler = new TaskSchedulerClass(); + scheduler.Connect(null, null, null, null); + ITaskFolder folder = scheduler.GetFolder("\\"); + IRegisteredTaskCollection tasks_exists = folder.GetTasks(1); + + if (RunAtStartup.Checked) + { + if (((IList)tasks_exists).Contains("Netch Startup")) + folder.DeleteTask("Netch Startup", 0); + + ITaskDefinition task = scheduler.NewTask(0); + task.RegistrationInfo.Author = "Netch"; + task.RegistrationInfo.Description = "Netch run at startup."; + task.Principal.RunLevel = _TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST; + + task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON); + IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC); + action.Path = System.Windows.Forms.Application.ExecutablePath; + + + task.Settings.ExecutionTimeLimit = "PT0S"; + task.Settings.DisallowStartIfOnBatteries = false; + task.Settings.RunOnlyIfIdle = false; + + folder.RegisterTaskDefinition("Netch Startup", task, (int)_TASK_CREATION.TASK_CREATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); + } + else + { + if (((IList)tasks_exists).Contains("Netch Startup")) + folder.DeleteTask("Netch Startup", 0); + } try { diff --git a/Netch/Models/Setting.cs b/Netch/Models/Setting.cs index cd74666b6d..4488f0bcd8 100644 --- a/Netch/Models/Setting.cs +++ b/Netch/Models/Setting.cs @@ -68,6 +68,11 @@ public class Setting /// public bool MinimizeWhenStarted = false; + /// + /// 是否开机启动软件 + /// + public bool RunAtStartup = false; + /// /// 是否打开软件时检查更新 /// diff --git a/Netch/Netch.csproj b/Netch/Netch.csproj index 7990343a47..56858fbbbd 100644 --- a/Netch/Netch.csproj +++ b/Netch/Netch.csproj @@ -52,6 +52,18 @@ + + + e34cb9f1-c7f7-424c-be29-027dcc09363a + 1 + 0 + tlbimp + 0 + false + false + + + diff --git a/Netch/Resources/zh-CN b/Netch/Resources/zh-CN index d5e27f32f3..701284aed6 100644 --- a/Netch/Resources/zh-CN +++ b/Netch/Resources/zh-CN @@ -120,6 +120,7 @@ "Settings": "设置", "Start when opened": "打开软件时启动加速", "Minimize when started": "启动加速后隐藏", + "Run at startup": "开机自动启动", "Local Port": "本地端口", "Allow other Devices to connect": "允许其他设备连入", "Netmask": "子网掩码", From c39ae5f12f90362ca2c4dfd96ed8e4c728f19787 Mon Sep 17 00:00:00 2001 From: LovelyWei <16489620+LovelyWei@users.noreply.github.com> Date: Tue, 18 Feb 2020 18:21:15 +0800 Subject: [PATCH 2/3] fix a bug --- Netch/Forms/SettingForm.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index 3ee47e919a..d1350af6c4 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -146,11 +146,17 @@ private void ControlButton_Click(object sender, EventArgs e) TaskSchedulerClass scheduler = new TaskSchedulerClass(); scheduler.Connect(null, null, null, null); ITaskFolder folder = scheduler.GetFolder("\\"); - IRegisteredTaskCollection tasks_exists = folder.GetTasks(1); + bool taskIsExists = false; + try + { + folder.GetTask("Netch Startup"); + taskIsExists = true; + } + catch (Exception) { } if (RunAtStartup.Checked) { - if (((IList)tasks_exists).Contains("Netch Startup")) + if (taskIsExists) folder.DeleteTask("Netch Startup", 0); ITaskDefinition task = scheduler.NewTask(0); @@ -171,7 +177,7 @@ private void ControlButton_Click(object sender, EventArgs e) } else { - if (((IList)tasks_exists).Contains("Netch Startup")) + if (taskIsExists) folder.DeleteTask("Netch Startup", 0); } From 11afbc91abc8f26cd851313b81107ed77a7be64c Mon Sep 17 00:00:00 2001 From: LovelyWei <16489620+LovelyWei@users.noreply.github.com> Date: Tue, 18 Feb 2020 18:22:35 +0800 Subject: [PATCH 3/3] Merge branch 'master' into dev --- Netch/Forms/MainForm.cs | 42 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 4eaa9bd87f..558571e2d5 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -1,4 +1,4 @@ -using Netch.Controllers; +using Netch.Controllers; using System; using System.Collections.Generic; using System.Diagnostics; @@ -791,6 +791,26 @@ private void ControlButton_Click(object sender, EventArgs e) // UsedBandwidthLabel.Visible = UploadSpeedLabel.Visible = DownloadSpeedLabel.Visible = true; // MainController.pNFController.OnBandwidthUpdated += OnBandwidthUpdated; + // 如果勾选启动后最小化 + if (Global.Settings.MinimizeWhenStarted) + { + WindowState = FormWindowState.Minimized; + NotifyIcon.Visible = true; + + if (IsFirstOpened) + { + // 显示提示语 + NotifyIcon.ShowBalloonTip(5, + UpdateChecker.Name, + Utils.i18N.Translate("Netch is now minimized to the notification bar, double click this icon to restore."), + ToolTipIcon.Info); + + IsFirstOpened = false; + } + + Hide(); + } + ControlButton.Enabled = true; ControlButton.Text = Utils.i18N.Translate("Stop"); @@ -836,26 +856,6 @@ private void ControlButton_Click(object sender, EventArgs e) } State = Models.State.Started; - - // 如果勾选启动后最小化 - if (Global.Settings.MinimizeWhenStarted) - { - WindowState = FormWindowState.Minimized; - NotifyIcon.Visible = true; - - if (IsFirstOpened) - { - // 显示提示语 - NotifyIcon.ShowBalloonTip(5, - UpdateChecker.Name, - Utils.i18N.Translate("Netch is now minimized to the notification bar, double click this icon to restore."), - ToolTipIcon.Info); - - IsFirstOpened = false; - } - - Hide(); - } } else {