private
{ Private declarations }
public
{ Public declarations }
end;
var
formautoshut: Tformautoshut;
P,Ti1:Pchar;
Flags:Longint;
i:integer;
TimeDelay:integer;
atom:integer;
implementation
uses main;
{$R *.dfm}
{判断时间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 Tformautoshut.Timer1Timer(Sender: TObject);
begin
Edit1.Text:=FormatDateTime('hh:mm', Now);
{两个时间相等,计算机将在TimeDelay秒内强制关机}
if edit1.text=edit2.Text then
Begin
TimeDelay:=160;
timer1.Enabled:=False;
if GetOperatingSystem='Windows NT/2000/XP' then
begin
{调用系统的关机提示窗口,只限于Windows NT/2000/XP。}
TimedShutDown(getcomputername, ' 系统即将要关机!请保存好你的数据退出!如要取消自动关机,请再点击自动关机设置中的放弃!',
TimeDelay, true, false);
button2.Enabled :=true;
timer2.Enabled :=true;
end;
if GetOperatingSystem='Windows 95/98/98SE/Me' then
begin
timer2.Enabled :=true;//直接关机
end;
end;
end;
procedure Tformautoshut.Timer2Timer(Sender: TObject);
begin
button2.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;
{确定按钮}
procedure Tformautoshut.button1Click(Sender: TObject);
begin
button2.Enabled :=false;
label3.Caption :='提示:关机时间格式 HH:MM';
if timer1.Enabled =false then timer1.Enabled :=true;
{关机时间设置有效,程序将显示在托盘中,无效则提示。}
if IsValidTime(edit2.Text) then
begin
formautoshut.Hide;
formautoshut.close;
end
else
showmessage('提示:时间格式错误,'+chr(13)+
'请输入正确的关机时间 HH:MM。');
end;
{取消关机按钮}
procedure Tformautoshut.button2Click(Sender: TObject);
begin
if GetOperatingSystem='Windows NT/2000/XP' then
{对于Windows NT/2000/XP,取消关机}
begin
AbortSystemShutdown(pchar(getcomputername));
close;
end;
{停止倒记时}
if timer2.Enabled =true then timer2.Enabled :=false;
button2.Enabled :=false;
end;