@@ -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+ private 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,45 @@ 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
91+ if ( processNameBox . Text . ToLower ( ) == "timecontrol" ||
92+ processNameBox . Text . ToLower ( ) == "timecontrolconsole" )
9293 {
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 ) ;
94+ return ;
10195 }
102- CalculateTime ( ) ;
96+ TimeInput timeInput = new ( controller , processNameBox . Text ) ;
97+ timeInput . ShowDialog ( ) ;
10398 }
10499
105100 private void RemoveButton_Click ( object sender , EventArgs e ) //移除所有的已添加窗口
106101 {
107- if ( usageBox . SelectedIndex >= 0 )
108- appList . RemoveAt ( usageBox . SelectedIndex ) ;
109- CalculateTime ( ) ;
102+ //检测密码设置
103+ if ( unlockPasswordHash != 0 )
104+ {
105+ PasswordInput passwordInput = new ( unlockPasswordHash ) ;
106+ if ( passwordInput . ShowDialog ( ) == DialogResult . OK )
107+ controller . Remove ( ) ;
108+ }
109+ else
110+ controller . Remove ( ) ;
110111 }
111112
112113 private void RefreshButton_Click ( object sender , EventArgs e ) //重新获取所有软件所用时间
113114 {
114- CalculateTime ( ) ;
115+ controller . Refresh ( ) ;
115116 }
116117
117118 private void ProcessMonitorTimer_Tick ( object sender , EventArgs e )
118119 {
119- foreach ( App app in appList ) //计算进程时间
120- {
121- if ( Process . GetProcessesByName ( app . Name ) . Length != 0 )
122- { app . Run ( ) ; }
123- }
120+ controller . Run ( ) ;
121+ if ( autoRefreshBox . Checked )
122+ controller . Refresh ( ) ;
124123 if ( Process . GetProcessesByName ( "TimeControlConsole" ) . Length == 0 ) //检查保护程序状态
125124 {
126125 ProcessStartInfo process = new ( ) ;
127126 process . FileName = "TimeControlConsole.exe" ;
128127 Process . Start ( process ) ;
129128 }
130129 }
131- private void CalculateTime ( ) //将进程时间推送到ListBox控件
132- {
133- processMonitorTimer . Stop ( ) ;
134- ListBoxController . Refresh ( usageBox , appList ) ;
135- processMonitorTimer . Start ( ) ;
136- }
137130 private void ForceClose ( ) //可以正常关闭
138131 {
139132 isClosable = true ;
@@ -148,13 +141,17 @@ private void ControlPanel_Shown(object sender, EventArgs e)//启动隐藏参数
148141
149142 processMonitorTimer . Start ( ) ;
150143 }
151- private void unloackPassWordSetButton_Click ( object sender , EventArgs e ) //保存密码
144+ private void UnloackPasswordSetButton_Click ( object sender , EventArgs e ) //保存密码
152145 {
153146 unlockPasswordHash = unlockPasswordBox . Text . GetHashCode ( ) ; //保存哈希值
147+ PasswordSet ( ) ;
148+ File . WriteAllText ( PasswordFile . tcPassLocation , unlockPasswordHash . ToString ( ) ) ; //保存哈希值到文件
149+ }
150+ private void PasswordSet ( ) //密码设置后调用
151+ {
154152 unlockPasswordBox . Text = "" ;
155153 unlockPasswordBox . Enabled = false ;
156- unloackPassWordSetButton . Enabled = false ;
157- File . WriteAllText ( PasswordFile . tcPassLocation , unlockPasswordHash . ToString ( ) ) ; //保存哈希值到文件
154+ unloackPasswordSetButton . Enabled = false ;
158155 }
159156 }
160157}
0 commit comments