这是一个自动关机的程序 里面有 你想要的功能!
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
char;
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:Int