放假期间,小朋友玩起了电脑,然后停不下来,需要采取一些措施
既然身为程序员,那就以程序的方式解决
主要实现功能:
1.监控程序进程,当进程运行时间超过一定时间后,锁屏或自动关机,让小朋友无法操作;
2.保存上次运行时间,防止重启后,时间判断被重置等问题;(开机自启动需要自行设置)
3.有对应设置页面,通过快捷键实现;
两个主要界面截图
主要实现功能如上,具体代码如下:
1.监控按钮事件:
SHIFT+F2:弹出设置页面进行维护对应监控进程、运行时长间隔;
SHIFT+F9:关闭监控程序;
屏蔽ALT+F4关闭事件
对一些系统组合键不做处理,如果要处理就比较麻烦,所以暂时忽略,小朋友也不会懂这么多的,所以无所谓
if (e.KeyCode == Keys.F2 && e.Shift) { this.Hide(); timer1.Enabled = false; FrmSetup frm = new FrmSetup(); frm.ShowDialog(); timer1.Enabled = true; e.Handled = true; this.Show(); } else if (e.KeyCode == Keys.F9 && e.Shift) { _needClose = true; this.Close(); e.Handled = true; } //Ctrl+ Alt+ Delete无法识别,要其他处理 //win无法识别 else if (e.KeyCode == Keys.F4 && e.Alt)// || e.KeyCode == Keys.LWin || e.KeyCode == Keys.RWin || e.KeyCode == Keys.Menu)// || e.KeyCode == Keys.Delete && e.Control && e.Alt) { e.Handled = true; }
2.屏蔽窗体关闭按钮事件
//const int WM_SYSCOMMAND = 0x112; //const int SC_CLOSE = 0xF060; //const int SC_MINIMIZE = 0xF020; //const int SC_MAXIMIZE = 0xF030; protected override void WndProc(ref Message m) { var mParam = m.WParam.ToInt32(); if (!_needClose && mParam == 0xF060) { return; } base.WndProc(ref m); }
3.定时器定时监测事件
private void timer1_Tick(object sender, EventArgs e) { this.TopMost = false; if(_controlProList.Count < 0) { return; } if (_existsProcessList.Count < 1 && _startTime.AddMinutes(_ticks) <= DateTime.Now) { foreach (var item in _controlProList) { var processs = Process.GetProcessesByName(item); if (processs.Length > 0 && processs[0].StartTime.AddMinutes(_ticks) <= DateTime.Now) { _existsProcessList.Add(processs[0]); if (_startTime< processs[0].StartTime) { _startTime = processs[0].StartTime; } RestData(_startTime); continue; } _count = 60; } if (_existsProcessList.Count <= 0) { return; } } this.TopMost = true; this.Show(); _count-= (int)(timer1.Interval/1000); label1.Text = $"距离{(_isClose == 1?"关机":"结束")}还有:{_count:00000}秒"; label2.Text = $"请让电脑休息{_ticks}分钟!"; if (_count <= 0) { _existsProcessList.ForEach((p) => p.Kill()); _existsProcessList.Clear(); this.Hide(); if (_isClose == 1) { //创建一个进程 System.Diagnostics.Process.Start("shutdown","-s -t 0");//关机程序 } else { InitData(); } } } private void RestData(DateTime dt) { var filePath = Path.Combine(_basePath, _startTimeFileName); if(!Directory.Exists(_basePath)) { Directory.CreateDirectory(_basePath); } File.WriteAllText(filePath, dt.ToString("yyyy-MM-dd HH:mm:ss")); } private void InitData() { _count = 60; if (!Directory.Exists(_basePath)) { Directory.CreateDirectory(_basePath); } DateTime oldStartTime; var filePath = Path.Combine(_basePath, _startTimeFileName); if (File.Exists(filePath)) { if (!DateTime.TryParse(File.ReadAllText(filePath).Trim(), out oldStartTime)) { oldStartTime = DateTime.Now.Date; } } else { RestData(DateTime.Now); oldStartTime = DateTime.Now.Date; } _startTime = oldStartTime; filePath = Path.Combine(_basePath, _controlorPro); if (!File.Exists(filePath)) { _ticks = 20; } else { var str = File.ReadAllText(filePath).Split(new char[] { ';', ',', '\r', '\t', '\n', }, StringSplitOptions.RemoveEmptyEntries); _controlProList.Clear(); if (str.Length > 0) { _controlProList.AddRange(str); } if (_controlProList.Count > 0) { if (int.TryParse(_controlProList[0], out _ticks)) { _controlProList.RemoveAt(0); if (_controlProList.Count > 0 && int.TryParse(_controlProList[0], out _isClose)) { _controlProList.RemoveAt(0); } } } else { _ticks = 20; } } }
源代码下载如下:
链接: https://pan.baidu.com/s/1AltN9HbZysbBRXEWBFN0Vw?pwd=4kq7 提取码: 4kq7 复制这段内容后打开百度网盘手机App,操作更方便哦