@@ -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}
0 commit comments