为了省电和减少硬件的损坏几率,在暂时不使用电脑时,可将电脑设为省电模式,即休眠模式。在这种模式下,显示器和硬
盘会关闭。要关机但有任务正在运行时,可以设置定时关机。下面介绍了当鼠标、键盘在一定时间内无操作时,系统就转为休眠或待机 及定时关机的实现方法。
以下是 核心 等 系统下实现关机、休NT (WinXP/2000/2003 )眠的代码。
( 获取键盘、鼠标的空闲时间1)
function LastInput: DWord;
var
LInput: TLastInputInfo;
begin
LInput.cbSize := SizeOf(TLastInputInfo);
( )
GetLastInputInfo(LInput);
Result := GetTickCount - LInput.dwTime;
end;
(取得系统操作权限2)unction SetPrivilege(sPrivilegeName: string; bEnabled: Boolean): Boolean;
f
var
TPPrev,TP: TTokenPrivileges; Token: THandle; dwRetLen: DWord;
begin
Result := False;
OpenProcessToken (GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY,Token);
TP.PrivilegeCount := 1;
if( LookupPrivilegeValue(Nil,PChar(sPrivilegeName),TP.Privileges[ 0 ].LUID ))
then
begin
if(bEnabled) then
TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED
else
TP.Privileges[ 0 ].Attributes := 0;
dwRetLen := 0;
Result := AdjustTokenPrivileges (Token,False,TP,SizeOf ( TPPrev ),TPPrev,
dwRetLen);
end;
CloseHandle(Token );
end;
(实现休眠操作3)
procedure TForm1.SleepClick(Sender: TObject);
begin
鼠标键盘已经有 秒没有使用
self.Caption := Format('
%d ', [(LastInput)div
1000]);
if (LastInput)div 1000=strtoint(edit1.text)*60 then
begin
获取操作权限!
SetPrivilege('SeShutdownPrivilege', True );//
SetSystemPowerState(true,true);
end;
end;
( 实现关机操作4)
function WinExitNT(iFlags: Integer ): Boolean;
begin
Result := True;
if (SetPrivilege('SeShutdownPrivilege',True)) then
begin
if(not ExitWindowsEx(iFlags,0))then Result := False;
SetPrivilege('SeShutdownPrivilege',False)
end
else
Result := False;
end;
关 机 时 调 用 方 法 :
WinExitNT (EWX_FORCE +

EWX_POWEROFF)