unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
//下面是最重要的一句 映射关机消息到一个过程
procedure windowsclose(var msg:tmessage);message wm_queryendsession;
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure tform1.windowsclose(var msg:tmessage);
begin
showmessage('cc');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
exitwindowsex(ewx_shutdown,0);
end;end.
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
//下面是最重要的一句 映射关机消息到一个过程
procedure windowsclose(var msg:tmessage);message wm_queryendsession;
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure tform1.windowsclose(var msg:tmessage);
begin
showmessage('cc');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
exitwindowsex(ewx_shutdown,0);
end;end.
至于如何
得到关机的时候弹出警告框应该可以通过拦截系统消息!!
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
rgExit: TRadioGroup;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
Begin
case rgexit.itemindex of 0: exitwindowsex(ewx_logoff,0); //注销当前用户并重新登录 1: exitwindowsex(ewx_reboot,0); //关闭Windows并重新启动 2: exitwindowsex(ewx_shutdown,0); //关闭Windows end;
End;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;end.
要实现“关闭时弹出一警告窗口”只须在程序主窗体的ONCLOSEQUERY事件中加入以下代码:
Procedure tform1.formclosequery(Sender:tobject;var canclose:Boolean);
begin
if messagedlg('警告!是否允许关闭?’,mtconfirmation,mbokcancel,0)=mrok then
Canclose:=False
else
Canclose:=False;
end;
如果把CanClose设为False,表示不允许关闭WINDOWSL;如果CanClose设为true,则允许关闭!
可以理解吗?是不是给点分!!!
CIMPUTER:计算机名;
MSG:显示的提示信息;
TIME:延迟的时间;
Force:是否强制关机;
REBOOT:是否重新启动}
function TimeShutDown(Computer:string;Msg:string;Time:Word;
Force:Boolean;Reboot:Boolean):Boolean;
var
rl,hToken:Cardinal;
tkp:TOKEN_PRIVILEGES;
begin
{获得用户关机特权,仅对NT内核}
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.Timer1Timer(Sender: TObject);
begin
edit1.Text:=formatdatetime('hh:mm:ss',now);
timer1.Enabled:=true;
if valbool then
begin
if Edit1.Text=Edit2.Text then
begin
TimeDelay:=15;
timer1.Enabled:=False;
if GetOperatingSystem='Windows NT/2000/XP' then
begin
{调用系统的关机提示窗口,只限于WINDOWSNT内核}
TimeShutDown(GetComputerName,
'系统即将要关机,请保存好你的数据退出!如要取消自动关机,请单击"放弃"按钮!',
TimeDelay,
True,
False );
bitbtn2.Enabled:=true;
timer2.Enabled:=true;
end
else
timer2.Enabled:=true;
end;
end;
end;