<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="WorkingStartTime" value="8:30" /> <add key="WorkingEndTime" value="17:30" /> <add key="WorkingHours" value="8" /> <add key="TotalRestHours" value="1" /> </appSettings> </configuration>
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using Microsoft.VisualBasic; using System.IO; using System.Diagnostics; using System.Configuration; using Microsoft.Win32; namespace WorkAssistant { public partial class frmMain : Form { [DllImport("kernel32.dll", CharSet = CharSet.Ansi)] private static extern IntPtr SetProcessShutdownParameters(int dwLevel, int dwFlags); private DateTime StartTime { get; set; } private DateTime WorkingStartTime { get; set; } private DateTime WorkingEndTime { get; set; } private bool ShutdownFlag { get; set; } public int WorkingHours { get; set; } public int TotalRestHours { get; set; } public frmMain() { InitializeComponent(); this.WorkingHours = 8; this.TotalRestHours = 1; } void frmMain_FormClosing(object sender, FormClosingEventArgs e) { throw new NotImplementedException(); } protected override void OnShown(EventArgs e) { base.OnShown(e); this.Hide(); } protected override void WndProc(ref Message m) { const int WM_QUERYENDSESSION = 0x011; const int WM_ENDSESSION = 0x16; var reason = SessionEndReasons.SystemShutdown; if (ShutdownFlag) { base.WndProc(ref m); return; } if (m.Msg == WM_QUERYENDSESSION) { return; } else if (m.Msg == WM_ENDSESSION) { if (m.LParam.ToInt32() == 0) { reason = SessionEndReasons.SystemShutdown; } else { reason = SessionEndReasons.Logoff; } var workOffTime = WorkingEndTime; if (StartTime > WorkingStartTime) { workOffTime = StartTime.AddHours(WorkingHours).AddHours(TotalRestHours); } if (workOffTime < WorkingEndTime) { workOffTime = WorkingEndTime; } if (DateTime.Now < workOffTime) { var msgResult = MessageBox.Show(string.Format("今天的开机时间是:{0},应该在{1}下班,工时还不足{2}小时,确定现在关机吗?", StartTime, workOffTime, WorkingHours), "关机确认", MessageBoxButtons.YesNo); if (msgResult == System.Windows.Forms.DialogResult.Yes) { msgResult = MessageBox.Show(string.Format("今天的开机时间是:{0},应该在{1}下班,工时还不足{2}小时,确定现在关机吗?", StartTime, workOffTime, WorkingHours), "再次确认", MessageBoxButtons.YesNo); if (msgResult != System.Windows.Forms.DialogResult.Yes) { return; } } else { return; } } ShutdownFlag = true; base.WndProc(ref m); switch (reason) { case SessionEndReasons.SystemShutdown: Shutdown(); break; case SessionEndReasons.Logoff: Logoff(); break; default: Shutdown(); break; } } else { base.WndProc(ref m); } } private void Form1_Load(object sender, EventArgs e) { IntPtr result = SetProcessShutdownParameters(0x4FF, 0); if (result.ToInt32() == 0) { //failed to set shutdown sequence } StartTime = GetStartTime(); WorkingStartTime = Convert.ToDateTime(ConfigurationManager.AppSettings["WorkingStartTime"]); WorkingEndTime = Convert.ToDateTime(ConfigurationManager.AppSettings["WorkingEndTime"]); WorkingHours = Convert.ToInt32(ConfigurationManager.AppSettings["WorkingHours"]); TotalRestHours = Convert.ToInt32(ConfigurationManager.AppSettings["TotalRestHours"]); } public DateTime GetStartTime() { EventLog eventLog = new EventLog("system", "."); var startTime = eventLog.Entries.Cast<EventLogEntry>() .Where(item => item.TimeWritten > DateTime.Today) .Min(item => item.TimeWritten); return startTime; } public void Shutdown() { SystemManager.WindowsController.ExitWindows(SystemManager.RestartOptions.ShutDown, false); } public void Logoff() { SystemManager.WindowsController.ExitWindows(SystemManager.RestartOptions.LogOff, false); } } }
原创,比较简单,关机的代码请参考的另一篇随笔,代码是一模一样的。