这是一个自动关机的程序 里面有 你想要的功能!<br><br>unit AutoShut1;<br>interface<br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, ExtCtrls, Menus,AppEvnts,shellapi;<br>type<br> TForm1 = class(TForm)<br> Timer1: TTimer;<br> Timer2: TTimer;<br> ApplicationEvents1: TApplicationEvents;<br> PopupMenu1: TPopupMenu;<br> Edit1: TEdit;<br> Edit2: TEdit;<br> Label1: TLabel;<br> Label2: TLabel;<br> Label3: TLabel;<br> Btn_OK: TButton;<br> Btn_Abort: TButton;<br> procedure Timer1Timer(Sender: TObject);<br> procedure TrayMenu(Var Msg:TMessage); message WM_USER;<br> procedure TimeSetClick(Sender: TObject);<br> procedure ExitClick(Sender: TObject);<br> procedure Btn_OKClick(Sender: TObject);<br> procedure Btn_AbortClick(Sender: TObject);<br> procedure Timer2Timer(Sender: TObject);<br> procedure Edit2KeyPress(Sender: TObject; var Key: Char);<br> procedure WMQueryEndSession (var Msg : TWMQueryEndSession);<br> message WM_QueryEndSession;<br> procedure FormCreate(Sender: TObject);<br> procedure FormDestroy(Sender: TObject);<br> procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);<br> private<br> { Private declarations }<br> Tray:NOTIFYICONDATA;<br> procedure ShowInTray();<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br> P,Ti1
char;<br> Flags:Longint;<br> i:integer;<br> {关机延迟时间}<br> TimeDelay:integer;<br> atom:integer;<br>implementation<br>{$R *.dfm}<br><br>{未到自动关机时间,系统要关闭时,截获关机消息<br>wm_queryendsession,让用户决定是否关机}<br>procedure TForm1.WMQueryEndSession (var Msg : TWMQueryEndSession);<br>begin<br> if MessageDlg('真的要关闭Windows吗?',mtConfirmation,[mbYes,mbNo], 0) = mrNo then<br> Msg.Result := 0<br> else<br> Msg.Result := 1;<br>end;<br><br>{判断时间S格式是否是有效}<br>function IsValidTime(s:string):bool;<br>begin<br> if Length(s)<>5 then IsValidTime:=False<br> else<br> begin<br> if (s[1]<'0') or (s[1]>'2') or (s[2]<'0') or<br> (s[2]>'9') or (s[3] <> ':') or<br> (s[4]<'0') or (s[4]>'5') or<br> (s[5]<'0') or (s[5]>'9')then IsValidTime:=False<br> else<br> IsValidTime:=True;<br> end;<br>end;<br><br>{判断是哪类操作系统,以确定关机方式}<br>function GetOperatingSystem: string;<br>var osVerInfo: TOSVersionInfo;<br>begin<br> Result :='';<br> osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);<br> if GetVersionEx(osVerInfo) then<br> case osVerInfo.dwPlatformId of<br> VER_PLATFORM_WIN32_NT:<br> begin<br> Result := 'Windows NT/2000/XP'<br> end;<br> VER_PLATFORM_WIN32_WINDOWS:<br> begin<br> Result := 'Windows 95/98/98SE/Me';<br> end;<br> end;<br>end;<br><br>{获得计算机名}<br>function GetComputerName: string;<br>var<br> buffer: array[0..MAX_COMPUTERNAME_LENGTH + 1] of Char;<br> Size: Cardinal;<br>begin<br> Size := MAX_COMPUTERNAME_LENGTH + 1;<br> Windows.GetComputerName(@buffer, Size);<br> Result := strpas(buffer);<br>end;<br><br>{定时关机函数 ,各参数的意义如下:<br>Computer: 计算机名;Msg:显示的提示信息;<br>Time:时间延迟; Force:是否强制关机;<br>Reboot: 是否重启动}<br>function TimedShutDown(Computer: string; Msg: string;<br> Time: Word; Force: Boolean; Reboot: Boolean): Boolean;<br>var<br> rl: Cardinal;<br> hToken: Cardinal;<br> tkp: TOKEN_PRIVILEGES;<br>begin<br> {获得用户关机特权,仅对Windows NT/2000/XP}<br> OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken);<br> if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) then<br> begin<br> tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br> tkp.PrivilegeCount := 1;<br> AdjustTokenPrivileges(hToken, False, tkp, 0, nil, rl);<br> end;<br> Result := InitiateSystemShutdown(PChar(Computer), PChar(Msg), Time, Force, Reboot)<br>end;<br><br>{窗体最小化后,显示在托盘中}<br>procedure tform1.ShowInTray;<br>Begin<br>Tray.cbSize:=sizeof(Tray);<br>Tray.Wnd:=Self.Handle;<br>Tray.uFlags:=NIF_ICON+NIF_MESSAGE+NIF_TIP;<br>Tray.uCallbackMessage:=WM_USER;<br>Tray.hIcon:=application.Icon.Handle ;<br>Tray.szTip:='定时关机';<br>Shell_NotifyIcon(NIM_ADD,@Tray);<br>End;<br><br>{右键单击托盘中的图标,显示快捷菜单}<br>procedure Tform1.TrayMenu(var Msg:TMessage);<br>var<br>X,Y:Tpoint;<br>J,K:Integer;<br>Begin<br>GetCursorPos(X);<br>GetCursorPos(Y);<br>J:=X.X;<br>K:=Y.Y;<br>if Msg.LParam=WM_RBUTTONDOWN then PopupMenu1.Popup(J,K);<br>End;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br>Edit1.Text:=FormatDateTime('hh:mm', Now);<br>{两个时间相等,计算机将在TimeDelay秒内强制关机}<br>if edit1.text=edit2.Text then<br>Begin<br>TimeDelay:=30;<br>timer1.Enabled:=False;<br>if GetOperatingSystem='Windows NT/2000/XP' then<br> begin<br> {调用系统的关机提示窗口,只限于Windows NT/2000/XP。}<br> TimedShutDown(getcomputername, '系统将要关机!',<br> TimeDelay, true, false);<br> btn_abort.Enabled :=true;<br> timer2.Enabled :=true;<br> end;<br>if GetOperatingSystem='Windows 95/98/98SE/Me' then<br> begin<br> timer2.Enabled :=true;<br> {在顶层显示本程序的窗口,显示时间倒记时}<br> Application.Restore;<br> SetWindowPos(Handle,HWND_TOPMOST,Left,Top,Width,Height,<br> SWP_NOACTIVATE);<br> end;<br>end;<br>end;<br><br>procedure TForm1.Timer2Timer(Sender: TObject);<br>begin<br> btn_abort.Enabled :=true;<br> label3.Caption :='离关机时间还有'+inttostr(timedelay)+'秒。';<br> if timedelay>0 then timedelay:=timedelay-1<br> else<br> begin<br> timer2.Enabled :=false;<br> {强制Windows 95/98/98SE/Me关机}<br> ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE,0);<br> end;<br>end;<br><br>{通过控件PopupMenu1定义的快捷菜单,包括"设置关机时间"和"退出"。<br>PopupMenu1的AutoPopup为False,下面是"设置关机时间"的代码}<br>procedure TForm1.TimeSetClick(Sender: TObject);<br>begin<br> {设置本程序窗口位于最顶层}<br> SetWindowPos(Handle,HWND_TOPMOST,Left,Top,Width,Height,<br> SWP_NOACTIVATE);<br> ShowWindow(Application.Handle,SW_NORMAL);<br> edit2.SetFocus ;<br> edit2.SelectAll ;<br>end;<br>{快捷菜单中"退出"的代码}<br>procedure TForm1.ExitClick(Sender: TObject);<br>begin<br> {如果已经开始倒记时,禁止退出,而是显示程序窗口}<br> if Timer2.Enabled=false then<br> begin<br> Application.Terminate;<br> end<br> else ShowWindow(Application.Handle,SW_NORMAL);<br>end;<br><br>{确定按钮}<br>procedure TForm1.Btn_OKClick(Sender: TObject);<br>begin<br> btn_abort.Enabled :=false;<br> label3.Caption :='提示:关机时间格式 HH:MM';<br> if timer1.Enabled =false then timer1.Enabled :=true;<br> {关机时间设置有效,程序将显示在托盘中,无效则提示。}<br> if IsValidTime(edit2.Text) then<br> begin<br> ShowWindow(Application.Handle,sw_minimize);<br> ShowWindow(Application.Handle,sw_hide);<br> ShowInTray;<br> end<br> else<br> showmessage('提示:时间格式错误,'+chr(13)+<br> '请输入正确的关机时间 HH:MM。');<br>end;<br><br>{取消关机按钮}<br>procedure TForm1.Btn_AbortClick(Sender: TObject);<br>begin<br> if GetOperatingSystem='Windows NT/2000/XP' then<br> {对于Windows NT/2000/XP,取消关机}<br> begin<br> AbortSystemShutdown(pchar(getcomputername));<br> end;<br> {停止倒记时}<br> if timer2.Enabled =true then timer2.Enabled :=false;<br> btn_abort.Enabled :=false;<br>end;<br><br>{输入关机时间后,可直接按回车}<br>procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);<br>begin<br> if (key=#13) then Btn_OK.Click;<br>end;<br><br>{搜寻系统原子表看是否程序已运行}<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> {如果没运行则在表中增加信息 }<br> if GlobalFindAtom('PROGRAM_RUNNING') = 0 then<br> atom := GlobalAddAtom('PROGRAM_RUNNING')<br> else begin<br> {如果程序已运行则显示信息然后退出 }<br> MessageDlg('程序已经在运行!',mtWarning,[mbOK],0);<br> Halt;<br> end;<br>end;<br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br> {程序退出时,从原子表中移走信息}<br> GlobalDeleteAtom(atom);<br> {删除托盘中的图标}<br> Shell_NotifyIcon(NIM_DELETE,@Tray);<br>end;<br><br>procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);<br>begin<br> {如果已经开始倒记时,禁止关闭程序窗口}<br> if timer2.Enabled =true then canclose:=false;<br>end;<br><br>end.