diff --git a/TCSetup/TCSetup.vdproj b/TCSetup/TCSetup.vdproj index 16d26ff..c1c0595 100644 --- a/TCSetup/TCSetup.vdproj +++ b/TCSetup/TCSetup.vdproj @@ -209,24 +209,24 @@ { "Name" = "8:Microsoft Visual Studio" "ProductName" = "8:TimeControl" - "ProductCode" = "8:{58790286-6BF3-4D15-BE41-3514CFB9D21B}" - "PackageCode" = "8:{15145DC8-E457-4268-AA72-57CBF07212D1}" + "ProductCode" = "8:{363F9324-4EDC-45ED-8286-322CA5109544}" + "PackageCode" = "8:{07EBB234-3576-4FAD-8A56-24A64982FEB6}" "UpgradeCode" = "8:{A7DC5BC4-7E3E-4B58-A0AD-3C9AC9F873C1}" "AspNetVersion" = "8:4.0.30319.0" "RestartWWWService" = "11:FALSE" "RemovePreviousVersions" = "11:TRUE" "DetectNewerInstalledVersion" = "11:TRUE" "InstallAllUsers" = "11:FALSE" - "ProductVersion" = "8:3.1.3" + "ProductVersion" = "8:3.2.0" "Manufacturer" = "8:SamHou" "ARPHELPTELEPHONE" = "8:" - "ARPHELPLINK" = "8:" + "ARPHELPLINK" = "8:https://github.com/SamHou2007/ComputerTimeControl" "Title" = "8:TCSetup" "Subject" = "8:" "ARPCONTACT" = "8:SamHou" "Keywords" = "8:" "ARPCOMMENTS" = "8:TimeControl安装程序" - "ARPURLINFOABOUT" = "8:" + "ARPURLINFOABOUT" = "8:https://github.com/SamHou2007" "ARPPRODUCTICON" = "8:_9BF8F60AD5F2474D82994CD2D95F44A1" "ARPIconIndex" = "3:0" "SearchPath" = "8:" @@ -823,7 +823,7 @@ { "{5259A561-127C-4D43-A0A1-72F10C7B3BF8}:_5003B007B32D429B83CC5372AB9FC0AF" { - "SourcePath" = "8:..\\TimeControlConsole\\obj\\Debug\\net6.0-windows10.0.22000.0\\apphost.exe" + "SourcePath" = "8:..\\TimeControlConsole\\obj\\Release\\net6.0-windows10.0.22000.0\\apphost.exe" "TargetName" = "8:" "Tag" = "8:" "Folder" = "8:_57FF55C0487B4507A7ADC64F8E5966A5" @@ -851,7 +851,7 @@ } "{5259A561-127C-4D43-A0A1-72F10C7B3BF8}:_B7C31D1C4E42443D89CA87F9596A43B7" { - "SourcePath" = "8:..\\TimeControl\\obj\\Debug\\net6.0-windows10.0.22000.0\\apphost.exe" + "SourcePath" = "8:..\\TimeControl\\obj\\Release\\net6.0-windows10.0.22000.0\\apphost.exe" "TargetName" = "8:" "Tag" = "8:" "Folder" = "8:_57FF55C0487B4507A7ADC64F8E5966A5" diff --git a/TimeControl/App.cs b/TimeControl/App.cs index 9c3e565..a2565ce 100644 --- a/TimeControl/App.cs +++ b/TimeControl/App.cs @@ -7,34 +7,39 @@ namespace TimeControl { + [Serializable] public class App { private readonly string name; - public string Name { get { return name; }} + + public string Name + { get { return name; } } + internal int time; + /// /// 返回进程的简要概述 /// /// 进程的简要概述 public override string ToString() { - return Name + " 已使用 " + time+" 秒!"; + return Name + " 已使用 " + time + " 秒!"; } - public App(string name,int time) + + public App(string name, int time) { this.time = time; this.name = name; } + /// /// 运行一次(一秒) /// - public virtual void Run(StreamWriter streamWriter) + public virtual void Run() { time++; - streamWriter.WriteLine(Name); - streamWriter.WriteLine(time); - streamWriter.WriteLine("//"); } + /// /// 重设时间 /// @@ -43,4 +48,4 @@ public void Reset() time = 0; } } -} +} \ No newline at end of file diff --git a/TimeControl/AppController.cs b/TimeControl/AppController.cs index ff69198..9fe02b4 100644 --- a/TimeControl/AppController.cs +++ b/TimeControl/AppController.cs @@ -9,63 +9,21 @@ namespace TimeControl { + [Serializable] public class AppController { - private FileStream fileStream = new(TimeControlFile.TimeFileLocation, - FileMode.OpenOrCreate, - FileAccess.ReadWrite, FileShare.None); - private StreamWriter streamWriter; - private ListBox listBox; private List apps; - private Timer timer; public AppController(ListBox listBox, Timer timer) { - streamWriter = new(fileStream); - this.listBox = listBox; apps = new List(); - this.timer = timer; - StreamReader streamReader = new(fileStream); - int lineNumber = 1; - string name = null; - int time = 0; - int timeLimit = 0; - while (!streamReader.EndOfStream)//读取文件,添加进程 - { - string line = streamReader.ReadLine(); - if (line == "//") - { - if (timeLimit == 0) - apps.Add(new App(name, time)); - else - apps.Add(new LimitedApp(name, time, timeLimit)); - - lineNumber = 1; - name = null; - time = 0; - timeLimit = 0; - - continue; - } - else - { - if (lineNumber == 1) - name = line; - else if (lineNumber == 2) - time = Convert.ToInt32(line); - else if (lineNumber == 3) - timeLimit = Convert.ToInt32(line); - - lineNumber++; - } - } - Refresh(); + Refresh(timer, listBox); } /// /// 刷新列表显示 /// - public void Refresh() + public void Refresh(Timer timer, ListBox listBox) { timer.Stop(); listBox.Items.Clear(); @@ -75,11 +33,12 @@ public void Refresh() } timer.Start(); } + /// /// 根据名称添加进程 /// /// 要添加的进程名称 - public void AddByName(string name) + public void AddByName(string name, Timer timer, ListBox listBox) { timer.Stop(); Process[] processes = Process.GetProcessesByName(name); @@ -94,14 +53,15 @@ public void AddByName(string name) { MessageBox.Show("错误", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); } - this.Refresh(); + Refresh(timer, listBox); } + /// /// 根据名称添加时间受限的进程 /// /// 进程名称 /// 限制时长(秒) - public void AddByName(string name, int limitTime) + public void AddByName(string name, int limitTime, Timer timer, ListBox listBox) { timer.Stop(); Process[] processes = Process.GetProcessesByName(name); @@ -116,14 +76,14 @@ public void AddByName(string name, int limitTime) { MessageBox.Show("错误", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); } - this.Refresh(); + Refresh(timer, listBox); } + /// /// 跟踪所有进程,增加一秒 /// public void Run() { - fileStream.SetLength(0); foreach (App app in apps)//计算进程时间 { if (Process.GetProcessesByName(app.Name).Length != 0) @@ -131,32 +91,40 @@ public void Run() if (app is LimitedApp) { LimitedApp limitedApp = app as LimitedApp; - limitedApp.Run(streamWriter); + limitedApp.Run(); } else - app.Run(streamWriter); + app.Run(); } } - streamWriter.Flush(); } + /// /// 移除所列表所选的进程 /// - public void Remove() + public void Remove(Timer timer, ListBox listBox) { timer.Stop(); if (listBox.SelectedIndex >= 0) apps.RemoveAt(listBox.SelectedIndex); - Refresh(); + Refresh(timer, listBox); } + /// /// 删除所有监控 /// - public void RemoveAll() + public void RemoveAll(Timer timer, ListBox listBox) { timer.Stop(); apps.Clear(); - Refresh(); + Refresh(timer, listBox); + } + + public void ResetAll(Timer timer, ListBox listBox) + { + foreach (App app in apps) + app.Reset(); + Refresh(timer, listBox); } } -} +} \ No newline at end of file diff --git a/TimeControl/ControlPanel.Designer.cs b/TimeControl/ControlPanel.Designer.cs index 48d7262..cb9ae24 100644 --- a/TimeControl/ControlPanel.Designer.cs +++ b/TimeControl/ControlPanel.Designer.cs @@ -36,6 +36,7 @@ private void InitializeComponent() this.controlTab = new System.Windows.Forms.TabControl(); this.startNow = new System.Windows.Forms.TabPage(); this.processMonitor = new System.Windows.Forms.TabPage(); + this.clearButton = new System.Windows.Forms.Button(); this.autoRefreshBox = new System.Windows.Forms.CheckBox(); this.refreshButton = new System.Windows.Forms.Button(); this.processNameBox = new System.Windows.Forms.TextBox(); @@ -54,7 +55,7 @@ private void InitializeComponent() this.contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components); this.ExitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.processMonitorTimer = new System.Windows.Forms.Timer(this.components); - this.clearButton = new System.Windows.Forms.Button(); + this.resetButton = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.timeBox)).BeginInit(); this.controlTab.SuspendLayout(); this.startNow.SuspendLayout(); @@ -122,6 +123,7 @@ private void InitializeComponent() // // processMonitor // + this.processMonitor.Controls.Add(this.resetButton); this.processMonitor.Controls.Add(this.clearButton); this.processMonitor.Controls.Add(this.autoRefreshBox); this.processMonitor.Controls.Add(this.refreshButton); @@ -137,10 +139,20 @@ private void InitializeComponent() this.processMonitor.Text = "进程计时"; this.processMonitor.UseVisualStyleBackColor = true; // + // clearButton + // + this.clearButton.Location = new System.Drawing.Point(529, 287); + this.clearButton.Name = "clearButton"; + this.clearButton.Size = new System.Drawing.Size(143, 45); + this.clearButton.TabIndex = 7; + this.clearButton.Text = "删除所有监控"; + this.clearButton.UseVisualStyleBackColor = true; + this.clearButton.Click += new System.EventHandler(this.ClearButton_Click); + // // autoRefreshBox // this.autoRefreshBox.AutoSize = true; - this.autoRefreshBox.Location = new System.Drawing.Point(557, 78); + this.autoRefreshBox.Location = new System.Drawing.Point(554, 47); this.autoRefreshBox.Name = "autoRefreshBox"; this.autoRefreshBox.Size = new System.Drawing.Size(91, 24); this.autoRefreshBox.TabIndex = 6; @@ -149,7 +161,7 @@ private void InitializeComponent() // // refreshButton // - this.refreshButton.Location = new System.Drawing.Point(528, 156); + this.refreshButton.Location = new System.Drawing.Point(528, 125); this.refreshButton.Name = "refreshButton"; this.refreshButton.Size = new System.Drawing.Size(143, 51); this.refreshButton.TabIndex = 5; @@ -159,14 +171,14 @@ private void InitializeComponent() // // processNameBox // - this.processNameBox.Location = new System.Drawing.Point(528, 213); + this.processNameBox.Location = new System.Drawing.Point(529, 182); this.processNameBox.Name = "processNameBox"; this.processNameBox.Size = new System.Drawing.Size(143, 27); this.processNameBox.TabIndex = 4; // // removeButton // - this.removeButton.Location = new System.Drawing.Point(528, 108); + this.removeButton.Location = new System.Drawing.Point(528, 77); this.removeButton.Name = "removeButton"; this.removeButton.Size = new System.Drawing.Size(143, 42); this.removeButton.TabIndex = 2; @@ -176,7 +188,7 @@ private void InitializeComponent() // // appAddButton // - this.appAddButton.Location = new System.Drawing.Point(528, 246); + this.appAddButton.Location = new System.Drawing.Point(528, 215); this.appAddButton.Name = "appAddButton"; this.appAddButton.Size = new System.Drawing.Size(143, 66); this.appAddButton.TabIndex = 1; @@ -304,15 +316,15 @@ private void InitializeComponent() this.processMonitorTimer.Interval = 1000; this.processMonitorTimer.Tick += new System.EventHandler(this.ProcessMonitorTimer_Tick); // - // clearButton + // resetButton // - this.clearButton.Location = new System.Drawing.Point(528, 318); - this.clearButton.Name = "clearButton"; - this.clearButton.Size = new System.Drawing.Size(143, 45); - this.clearButton.TabIndex = 7; - this.clearButton.Text = "删除所有监控"; - this.clearButton.UseVisualStyleBackColor = true; - this.clearButton.Click += new System.EventHandler(this.ClearButton_Click); + this.resetButton.Location = new System.Drawing.Point(529, 338); + this.resetButton.Name = "resetButton"; + this.resetButton.Size = new System.Drawing.Size(142, 40); + this.resetButton.TabIndex = 8; + this.resetButton.Text = "重置所有进程时间"; + this.resetButton.UseVisualStyleBackColor = true; + this.resetButton.Click += new System.EventHandler(this.resetButton_Click); // // ControlPanel // @@ -368,5 +380,6 @@ private void InitializeComponent() private System.Windows.Forms.Button refreshButton; private System.Windows.Forms.CheckBox autoRefreshBox; private System.Windows.Forms.Button clearButton; + private System.Windows.Forms.Button resetButton; } } \ No newline at end of file diff --git a/TimeControl/ControlPanel.cs b/TimeControl/ControlPanel.cs index 5258c04..8eaaeed 100644 --- a/TimeControl/ControlPanel.cs +++ b/TimeControl/ControlPanel.cs @@ -10,6 +10,7 @@ using System.Runtime.InteropServices; using System.Diagnostics; using System.Threading; +using System.Runtime.Serialization.Formatters.Binary; using System.IO; namespace TimeControl @@ -20,6 +21,7 @@ public partial class ControlPanel : Form private bool isClosable = false;//指示当前是否可以关闭 private string unlockPasswordHash = "";//密码哈希值,用作比对 private AppController controller;//列表、计时控制器 + public ControlPanel(bool hide) { InitializeComponent(); @@ -29,7 +31,17 @@ public ControlPanel(bool hide) unlockPasswordHash = File.ReadAllText(TimeControlFile.PassLocation); PasswordSet(); } - controller = new(usageBox, processMonitorTimer); + BinaryFormatter formatter = new BinaryFormatter(); + if (File.Exists(TimeControlFile.TimeFileLocation)) + { + using (Stream stream = File.OpenRead + (TimeControlFile.TimeFileLocation)) + { + controller = (AppController)formatter.Deserialize(stream); + } + } + else + controller = new(usageBox, processMonitorTimer); } private void StartButton_Click(object sender, EventArgs e)//启动屏保程序 @@ -72,7 +84,7 @@ private void ControlPanel_FormClosing(object sender, FormClosingEventArgs e)// private void ExitToolStripMenuItem_Click(object sender, EventArgs e)//正常退出程序 { PasswordInput passwordInput = new(unlockPasswordHash); - if (!string.IsNullOrEmpty( unlockPasswordHash))//检测是否设置了管理码 + if (!string.IsNullOrEmpty(unlockPasswordHash))//检测是否设置了管理码 { if (passwordInput.ShowDialog() == DialogResult.OK) ForceClose(); @@ -93,7 +105,7 @@ private void AppAddButton_Click(object sender, EventArgs e)//添加进程 { return; } - TimeInput timeInput = new(controller, processNameBox.Text);//打开进程限时控制窗口 + TimeInput timeInput = new(controller, processNameBox.Text, processMonitorTimer, usageBox);//打开进程限时控制窗口 timeInput.ShowDialog(); } @@ -102,32 +114,44 @@ private void RemoveButton_Click(object sender, EventArgs e)//移除窗口 //检测密码设置 if (PasswordCheck()) { - controller.Remove(); + controller.Remove(processMonitorTimer, usageBox); } } private void RefreshButton_Click(object sender, EventArgs e)//重新获取所有软件所用时间 { - controller.Refresh(); + UpdateForm(); + } + + private void UpdateForm() + { + controller.Refresh(processMonitorTimer, usageBox); } private void ProcessMonitorTimer_Tick(object sender, EventArgs e) { controller.Run(); if (autoRefreshBox.Checked) - controller.Refresh(); + UpdateForm(); if (Process.GetProcessesByName("TimeControlConsole").Length == 0)//检查保护程序状态 { ProcessStartInfo process = new(); process.FileName = "TimeControlConsole.exe"; Process.Start(process); } + using (Stream stream = File.Create(TimeControlFile.TimeFileLocation)) + { + BinaryFormatter formatter = new(); + formatter.Serialize(stream, controller); + } } + private void ForceClose()//可以正常关闭 { isClosable = true; Close(); } + private void ControlPanel_Shown(object sender, EventArgs e)//启动隐藏参数支持 { if (hide) @@ -136,14 +160,16 @@ private void ControlPanel_Shown(object sender, EventArgs e)//启动隐藏参数 } processMonitorTimer.Start(); + UpdateForm(); } + private void UnloackPasswordSetButton_Click(object sender, EventArgs e)//保存密码 { - - unlockPasswordHash = Password.ComputeHash( unlockPasswordBox.Text);//保存哈希值 + unlockPasswordHash = Password.ComputeHash(unlockPasswordBox.Text);//保存哈希值 File.WriteAllText(TimeControlFile.PassLocation, unlockPasswordHash.ToString());//保存哈希值到文件 PasswordSet(); } + private void PasswordSet()//密码设置后调用 { unlockPasswordBox.Text = ""; @@ -155,12 +181,13 @@ private void ClearButton_Click(object sender, EventArgs e)//移除所有的已 { if (PasswordCheck()) { - controller.RemoveAll(); + controller.RemoveAll(processMonitorTimer, usageBox); } } + private bool PasswordCheck()//检测密码是否正确 { - if (!string.IsNullOrEmpty( unlockPasswordHash)) + if (!string.IsNullOrEmpty(unlockPasswordHash)) { PasswordInput passwordInput = new(unlockPasswordHash); if (passwordInput.ShowDialog() == DialogResult.OK) @@ -171,5 +198,10 @@ private bool PasswordCheck()//检测密码是否正确 else return true; } + + private void resetButton_Click(object sender, EventArgs e) + { + controller.ResetAll(processMonitorTimer, usageBox); + } } -} +} \ No newline at end of file diff --git a/TimeControl/LimitedApp.cs b/TimeControl/LimitedApp.cs index 7c25db7..20cfd06 100644 --- a/TimeControl/LimitedApp.cs +++ b/TimeControl/LimitedApp.cs @@ -9,17 +9,20 @@ namespace TimeControl { + [Serializable] public class LimitedApp : App { private readonly int timeLimit; - public LimitedApp(string name,int time,int timeLimit) : base(name,time) + + public LimitedApp(string name, int time, int timeLimit) : base(name, time) { this.timeLimit = timeLimit; } + /// /// 运行一次(一秒),并根据情况显示警告或关闭进程 /// - public override void Run(StreamWriter streamWriter) + public override void Run() { time++; if (time == timeLimit - 30) @@ -27,15 +30,12 @@ public override void Run(StreamWriter streamWriter) LimitWarningWindow warningWindow = new(this); warningWindow.Show(); } - if (time>=timeLimit) + if (time >= timeLimit) { Ban(); } - streamWriter.WriteLine(Name); - streamWriter.WriteLine(time); - streamWriter.WriteLine(timeLimit); - streamWriter.WriteLine("//"); } + /// /// 返回时间受限进程的简要概述 /// @@ -44,8 +44,9 @@ public override string ToString() { return base.ToString() + "进程时间限制为:" + timeLimit + "秒"; } + /// - /// 禁用掉该程序 + /// 关闭掉该程序,并提前设置为完全禁用 /// public void Ban() { @@ -57,4 +58,4 @@ public void Ban() time = timeLimit; } } -} +} \ No newline at end of file diff --git a/TimeControl/TimeControl.csproj b/TimeControl/TimeControl.csproj index ba01615..b81f8de 100644 --- a/TimeControl/TimeControl.csproj +++ b/TimeControl/TimeControl.csproj @@ -5,7 +5,7 @@ net6.0-windows10.0.22000.0 true SamHou - 3.1.3 + 3.2.0 TimeControl.Program False diff --git a/TimeControl/TimeControlFile.cs b/TimeControl/TimeControlFile.cs index 37e6e9a..2745534 100644 --- a/TimeControl/TimeControlFile.cs +++ b/TimeControl/TimeControlFile.cs @@ -10,7 +10,8 @@ internal class TimeControlFile { public static readonly string PassLocation = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData) + "\\TCPass.txt";//获取密码位置 + public static readonly string TimeFileLocation = Environment.GetFolderPath - (Environment.SpecialFolder.ApplicationData) + "\\TCTimeData.txt"; + (Environment.SpecialFolder.ApplicationData) + "\\TCTimeData.dat"; } -} +} \ No newline at end of file diff --git a/TimeControl/TimeInput.Designer.cs b/TimeControl/TimeInput.Designer.cs index ac91dcf..3fa901a 100644 --- a/TimeControl/TimeInput.Designer.cs +++ b/TimeControl/TimeInput.Designer.cs @@ -37,15 +37,15 @@ private void InitializeComponent() // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(68, 35); + this.label1.Location = new System.Drawing.Point(58, 41); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(288, 20); + this.label1.Size = new System.Drawing.Size(402, 20); this.label1.TabIndex = 0; - this.label1.Text = "输入进程限时,0则表示不限制时间(秒)"; + this.label1.Text = "输入进程限时,1则表示不限制时间(秒),0表示完全禁用"; // // okButton // - this.okButton.Location = new System.Drawing.Point(139, 140); + this.okButton.Location = new System.Drawing.Point(212, 128); this.okButton.Name = "okButton"; this.okButton.Size = new System.Drawing.Size(110, 53); this.okButton.TabIndex = 1; @@ -55,7 +55,7 @@ private void InitializeComponent() // // timeBox // - this.timeBox.Location = new System.Drawing.Point(120, 87); + this.timeBox.Location = new System.Drawing.Point(196, 85); this.timeBox.Maximum = new decimal(new int[] { 200000, 0, @@ -69,7 +69,7 @@ private void InitializeComponent() // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(394, 218); + this.ClientSize = new System.Drawing.Size(539, 218); this.Controls.Add(this.timeBox); this.Controls.Add(this.okButton); this.Controls.Add(this.label1); diff --git a/TimeControl/TimeInput.cs b/TimeControl/TimeInput.cs index 887c11b..0c1751e 100644 --- a/TimeControl/TimeInput.cs +++ b/TimeControl/TimeInput.cs @@ -14,29 +14,32 @@ public partial class TimeInput : Form { private AppController appController; private readonly string appName; + private Timer timer; + private ListBox listBox; + /// /// 创建一个新的输入窗口,并添加进程 /// /// 进程列表控制器,用于添加进程 /// 需要添加进程的名称 - public TimeInput(AppController appController, string appName) + public TimeInput(AppController appController, string appName, Timer timer, ListBox listBox) { InitializeComponent(); this.appController = appController; this.appName = appName; + this.timer = timer; + this.listBox = listBox; } private void OkButton_Click(object sender, EventArgs e) { if (timeBox.Value == 0) - { - appController.AddByName(appName); - } + appController.AddByName(appName, 0, timer, listBox); + else if (timeBox.Value == 1) + appController.AddByName(appName, timer, listBox); else - { - appController.AddByName(appName, Convert.ToInt32(timeBox.Value)); - } + appController.AddByName(appName, Convert.ToInt32(timeBox.Value), timer, listBox); Close(); } } -} +} \ No newline at end of file