四、源程序:
unit AutoShut1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Menus,AppEvnts,shellapi;
type
TForm1 = class(TForm)
Timer1: TTimer;
Timer2: TTimer;
ApplicationEvents1: TApplicationEvents;
PopupMenu1: TPopupMenu;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Btn_OK: TButton;
Btn_Abort: TButton;
procedure Timer1Timer(Sender: TObject);
procedure TrayMenu(Var Msg:TMessage); message WM_USER;
procedure TimeSetClick(Sender: TObject);
procedure ExitClick(Sender: TObject);
procedure Btn_OKClick(Sender: TObject);
procedure Btn_AbortClick(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
procedure Edit2KeyPress(Sender: TObject; var Key: Char);
procedure WMQueryEndSession (var Msg : TWMQueryEndSession);
message WM_QueryEndSession;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
{ Private declarations }
Tray:NOTIFYICONDATA;
procedure ShowInTray();
public
{ Public declarations }
end;
var
Form1: TForm1;
P,Ti1:Pchar;
Flags:Longint;
i:integer;
{關機延遲時間}
TimeDelay:integer;
atom:integer;
implementation
{$R *.dfm}
{未到自動關機時間,系統要關閉時,截獲關機消息
wm_queryendsession,讓用戶決定是否關機}
procedure TForm1.WMQueryEndSession (var Msg : TWMQueryEndSession);
begin
if MessageDlg(’真的要關閉Windows嗎?’,mtConfirmation,[mbYes,mbNo], 0) = mrNo then
Msg.Result := 0
else
Msg.Result := 1;
end;
{判斷時間S格式是否是有效}
function IsValidTime(s:string):bool;
begin
if Length(s)<>5 then IsValidTime:=False
else
begin
if(s[1]<’0’)or(s[1]>’2’)or(s[2]<’0’)or
(s[2]>’9’) or (s[3] <> ’:’) or
(s[4]<’0’) or (s[4]>’5’) or
(s[5]<’0’) or (s[5]>’9’)then IsValidTime:=False
else
IsValidTime:=True;
end;
end;
{判斷是哪類操作系統,以確定關機方式}
function GetOperatingSystem: string;
var osVerInfo: TOSVersionInfo;
begin
Result :=’’;
osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
if GetVersionEx(osVerInfo) then
case osVerInfo.dwPlatformId of
VER_PLATFORM_WIN32_NT:
begin
Result := ’Windows NT/2000/XP’
end;
VER_PLATFORM_WIN32_Windows:
begin
Result := ’Windows 95/98/98SE/Me’;
end;
end;
end;
{獲得計算機名}
function GetComputerName: string;
var
buffer: array[0..MAX_COMPUTERNAME_LENGTH + 1] of Char;
Size: Cardinal;
begin
Size := MAX_COMPUTERNAME_LENGTH + 1;
Windows.GetComputerName(@buffer, Size);
Result := strpas(buffer);
end;
{定時關機函數 ,各參數的意義如下:
Computer: 計算機名;Msg:顯示的提示信息;
Time:時間延遲; Force:是否強制關機;
Reboot: 是否重啟動}
function TimedShutDown(Computer: string; Msg: string;
Time: Word; Force: Boolean; Reboot: Boolean): Boolean;
var
rl: Cardinal;
hToken: Cardinal;
tkp: TOKEN_PRIVILEGES;
begin
{獲得用戶關機特權,僅對Windows NT/2000/XP}
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken);
if LookupPrivilegeValue(nil, ’SeShutdownPrivilege’, tkp.Privileges[0].Luid) then
begin
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
tkp.PrivilegeCount := 1;
AdjustTokenPrivileges(hToken, False, tkp, 0, nil, rl);
end;
Result := InitiateSystemShutdown(PChar(Computer), PChar(Msg), Time, Force, Reboot)
end;
{窗體最小化後,顯示在托盤中}
procedure tform1.ShowInTray;
Begin
Tray.cbSize:=sizeof(Tray);
Tray.Wnd:=Self.Handle;
Tray.uFlags:=NIF_ICON+NIF_MESSAGE+NIF_TIP;
Tray.uCallbackMessage:=WM_USER;
Tray.hIcon:=application.Icon.Handle ;
Tray.szTip:=’定時關機’;
Shell_NotifyIcon(NIM_ADD,@Tray);
End;
{右鍵單擊托盤中的圖標,顯示快捷菜單}
procedure Tform1.TrayMenu(var Msg:TMessage);
var
X,Y:Tpoint;
J,K:Integer;
Begin
GetCursorPos(X);
GetCursorPos(Y);
J:=X.X;
K:=Y.Y;
if Msg.LParam=WM_RBUTTONDOWN then PopupMenu1.Popup(J,K);
End;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Edit1.Text:=FormatDateTime(’hh:mm’, Now);
{兩個時間相等,計算機將在TimeDelay秒內強制關機}
if edit1.text=edit2.Text then
Begin
TimeDelay:=30;
timer1.Enabled:=False;
if GetOperatingSystem=’Windows NT/2000/XP’ then
begin
{調用系統的關機提示窗口,只限於Windows NT/2000/XP。}
TimedShutDown(getcomputername, ’系統將要關機!’,
TimeDelay, true, false);
btn_abort.Enabled :=true;
timer2.Enabled :=true;
end;
if GetOperatingSystem=’Windows 95/98/98SE/Me’ then
begin
timer2.Enabled :=true;
{在頂層顯示本程序的窗口,顯示時間倒記時}
Application.Restore;
SetWindowPos(Handle,HWND_TOPMOST,Left,Top,Width,Height,
SWP_NOACTIVATE);
end;
end;
end;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
btn_abort.Enabled :=true;
label3.Caption :=’離關機時間還有’+inttostr(timedelay)+’秒。’;
if timedelay>0 then timedelay:=timedelay-1
else
begin
timer2.Enabled :=false;
{強制Windows 95/98/98SE/Me關機}
ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE,0);
end;
end;
{通過控件PopupMenu1定義的快捷菜單,包括"設置關機時間"和"退出"。
PopupMenu1的AutoPopup為False,下面是"設置關機時間"的代碼}
procedure TForm1.TimeSetClick(Sender: TObject);
begin
{設置本程序窗口位於最頂層}
SetWindowPos(Handle,HWND_TOPMOST,Left,Top,Width,Height,
SWP_NOACTIVATE);
ShowWindow(Application.Handle,SW_NORMAL);
edit2.SetFocus ;
edit2.SelectAll ;
end;
{快捷菜單中"退出"的代碼}
procedure TForm1.ExitClick(Sender: TObject);
begin
{如果已經開始倒記時,禁止退出,而是顯示程序窗口}
if Timer2.Enabled=false then
begin
Application.Terminate;
end
else ShowWindow(Application.Handle,SW_NORMAL);
end;
{確定按鈕}
procedure TForm1.Btn_OKClick(Sender: TObject);
begin
btn_abort.Enabled :=false;
label3.Caption :=’提示:關機時間格式 HH:MM’;
if timer1.Enabled =false then timer1.Enabled :=true;
{關機時間設置有效,程序將顯示在托盤中,無效則提示。}
if IsValidTime(edit2.Text) then
begin
ShowWindow(Application.Handle,sw_minimize);
ShowWindow(Application.Handle,sw_hide);
ShowInTray;
end
else
showmessage(’提示:時間格式錯誤,’+chr(13)+
’請輸入正確的關機時間 HH:MM。’);
end;
{取消關機按鈕}
procedure TForm1.Btn_AbortClick(Sender: TObject);
begin
if GetOperatingSystem=’Windows NT/2000/XP’ then
{對於Windows NT/2000/XP,取消關機}
begin
AbortSystemShutdown(pchar(getcomputername));
end;
{停止倒記時}
if timer2.Enabled =true then timer2.Enabled :=false;
btn_abort.Enabled :=false;
end;
{輸入關機時間後,可直接按回車}
procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
if (key=#13) then Btn_OK.Click;
end;
{搜尋系統原子表看是否程序已運行}
procedure TForm1.FormCreate(Sender: TObject);
begin
{如果沒運行則在表中增加信息 }
if GlobalFindAtom(’PROGRAM_RUNNING’) = 0 then
atom := GlobalAddAtom(’PROGRAM_RUNNING’)
else begin
{如果程序已運行則顯示信息然後退出 }
MessageDlg(’程序已經在運行!’,mtWarning,[mbOK],0);
Halt;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
{程序退出時,從原子表中移走信息}
GlobalDeleteAtom(atom);
{刪除托盤中的圖標}
Shell_NotifyIcon(NIM_DELETE,@Tray);
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
{如果已經開始倒記時,禁止關閉程序窗口}
if timer2.Enabled =true then canclose:=false;
end;
end.
五、說明:本程序在Windows XP下,用Delphi 6.0開發,在Windows 95/98/Me和Windows NT/2000/XP下運行成功。