Skip to content

Commit 68ddac3

Browse files
committed
实时监控检测
1 parent b81156c commit 68ddac3

File tree

5 files changed

+98
-81
lines changed

5 files changed

+98
-81
lines changed

TimeControl/ControlPanel.Designer.cs

Lines changed: 13 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TimeControl/ControlPanel.cs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@ public partial class ControlPanel : Form
1818
{
1919
private bool hide = false;//指示启动后是否需要隐藏
2020
private bool isClosable = false;//指示当前是否可以关闭
21-
List<App> appList = new();//所有监控软件列表
2221
private int unlockPasswordHash = 0;//密码哈希值,用作比对
22+
ListController controller;//列表、计时控制器
2323
public ControlPanel(bool hide)
2424
{
2525
InitializeComponent();
2626
this.hide = hide;
2727
if (File.Exists(PasswordFile.tcPassLocation))//加载密码哈希值
2828
{
2929
unlockPasswordHash = Convert.ToInt32(File.ReadAllText(PasswordFile.tcPassLocation));
30+
PasswordSet();
3031
}
32+
controller=new(usageBox,processMonitorTimer);
3133
}
3234

3335
private void StartButton_Click(object sender, EventArgs e)//启动屏保程序
@@ -86,54 +88,29 @@ private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
8688

8789
private void AppAddButton_Click(object sender, EventArgs e)//添加打开的窗口
8890
{
89-
processMonitorTimer.Stop();
90-
Process[] processes=Process.GetProcessesByName(processNameBox.Text);
91-
try
92-
{
93-
foreach (Process process in processes)
94-
{
95-
appList.Add(new App(process.ProcessName, process.MainModule.FileName));
96-
}
97-
}
98-
catch (Exception ex)
99-
{
100-
MessageBox.Show("错误",ex.Message,MessageBoxButtons.OK,MessageBoxIcon.Error);
101-
}
102-
CalculateTime();
91+
controller.AddByName(processNameBox.Text);
10392
}
10493

10594
private void RemoveButton_Click(object sender, EventArgs e)//移除所有的已添加窗口
10695
{
107-
if (usageBox.SelectedIndex >=0)
108-
appList.RemoveAt(usageBox.SelectedIndex);
109-
CalculateTime();
96+
controller.Remove();
11097
}
11198

11299
private void RefreshButton_Click(object sender, EventArgs e)//重新获取所有软件所用时间
113100
{
114-
CalculateTime();
101+
controller.Refresh();
115102
}
116103

117104
private void ProcessMonitorTimer_Tick(object sender, EventArgs e)
118105
{
119-
foreach (App app in appList)//计算进程时间
120-
{
121-
if (Process.GetProcessesByName(app.Name).Length != 0)
122-
{ app.Run(); }
123-
}
106+
controller.Run();
124107
if (Process.GetProcessesByName("TimeControlConsole").Length == 0)//检查保护程序状态
125108
{
126109
ProcessStartInfo process = new();
127110
process.FileName = "TimeControlConsole.exe";
128111
Process.Start(process);
129112
}
130113
}
131-
private void CalculateTime()//将进程时间推送到ListBox控件
132-
{
133-
processMonitorTimer.Stop();
134-
ListBoxController.Refresh(usageBox, appList);
135-
processMonitorTimer.Start();
136-
}
137114
private void ForceClose()//可以正常关闭
138115
{
139116
isClosable = true;
@@ -148,13 +125,17 @@ private void ControlPanel_Shown(object sender, EventArgs e)//启动隐藏参数
148125

149126
processMonitorTimer.Start();
150127
}
151-
private void unloackPassWordSetButton_Click(object sender, EventArgs e)//保存密码
128+
private void UnloackPasswordSetButton_Click(object sender, EventArgs e)//保存密码
152129
{
153130
unlockPasswordHash = unlockPasswordBox.Text.GetHashCode();//保存哈希值
131+
PasswordSet();
132+
File.WriteAllText(PasswordFile.tcPassLocation, unlockPasswordHash.ToString());//保存哈希值到文件
133+
}
134+
private void PasswordSet()//密码设置后调用
135+
{
154136
unlockPasswordBox.Text = "";
155137
unlockPasswordBox.Enabled = false;
156-
unloackPassWordSetButton.Enabled = false;
157-
File.WriteAllText(PasswordFile.tcPassLocation, unlockPasswordHash.ToString());//保存哈希值到文件
138+
unloackPasswordSetButton.Enabled = false;
158139
}
159140
}
160141
}

TimeControl/ListBoxController.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

TimeControl/ListController.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows.Forms;
8+
9+
namespace TimeControl
10+
{
11+
internal class ListController
12+
{
13+
private ListBox listBox;
14+
private List<App> apps;
15+
private Timer timer;
16+
17+
public ListController(ListBox listBox,Timer timer)
18+
{
19+
this.listBox = listBox;
20+
apps = new List<App>();
21+
this.timer = timer;
22+
}
23+
24+
25+
public void Refresh()
26+
{
27+
timer.Stop();
28+
listBox.Items.Clear();
29+
foreach (App app in apps)
30+
{
31+
listBox.Items.Add(app.ReportApp());
32+
}
33+
timer.Start();
34+
}
35+
public void AddByName(string name)
36+
{
37+
timer.Stop();
38+
Process[] processes = Process.GetProcessesByName(name);
39+
try
40+
{
41+
foreach (Process process in processes)
42+
{
43+
apps.Add(new App(process.ProcessName, process.MainModule.FileName));
44+
}
45+
}
46+
catch (Exception ex)
47+
{
48+
MessageBox.Show("错误", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
49+
}
50+
this.Refresh();
51+
}
52+
public void Run()
53+
{
54+
foreach (App app in apps)//计算进程时间
55+
{
56+
if (Process.GetProcessesByName(app.Name).Length != 0)
57+
{ app.Run(); }
58+
}
59+
Refresh();
60+
}
61+
public void Remove()
62+
{
63+
timer.Stop();
64+
if (listBox.SelectedIndex >= 0)
65+
apps.RemoveAt(listBox.SelectedIndex);
66+
Refresh();
67+
}
68+
}
69+
}

TimeControl/TimeControl.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFramework>net6.0-windows</TargetFramework>
5+
<TargetFramework>net6.0-windows10.0.22000.0</TargetFramework>
66
<UseWindowsForms>true</UseWindowsForms>
77
<Authors>SamHou</Authors>
88
<Version>3.1.0</Version>
@@ -11,6 +11,7 @@
1111
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
1212
<ApplicationIcon>icons8_sand_watch.ico</ApplicationIcon>
1313
<PlatformTarget>AnyCPU</PlatformTarget>
14+
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
1415
</PropertyGroup>
1516

1617
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">

0 commit comments

Comments
 (0)