?? unitshutdown.pas
字號:
unit UnitShutDown;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls;
type
TFrmShutDown = class(TForm)
RadioGroup1: TRadioGroup;
Label1: TLabel;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
RadioButton4: TRadioButton;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function SetPrivilege(PrivilegeName:String;Enable:Boolean):Boolean;
//設置系統權限(提升或降低)
procedure ShutDownSystem(EWX_Type:Integer);//根據關機類型,執行操作
end;
var
FrmShutDown: TFrmShutDown;
implementation
uses Myfunction;
{$R *.dfm}
const
EWX_FORCE=4; //強制關閉所有程序
EWX_LOGOFF=0; //注銷
EWX_SHUTDOWN=1; //關閉計算機
EWX_REBOOT=2; //關閉并重新啟動計算機
EWX_POWEROFF=8; //關閉系統并切斷電源
function TFrmShutDown.SetPrivilege(PrivilegeName:String;Enable:Boolean):Boolean;
var
NewState,PreviousState:TTokenPrivileges;
Token: THandle;
dwRetLen:DWORD;
begin
result:=False;
OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,token);
NewState.PrivilegeCount:=1;
if(LookupPrivilegeValue(nil,PChar(PrivilegeName),NewState.Privileges[0].Luid))then
begin
if Enable then //2
NewState.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED
else
NewState.Privileges[0].Attributes:=0;
dwRetLen:=0;
Result:=AdjustTokenPrivileges(token,False,NewState,SizeOf(PreviousState),PreviousState,dwRetLen);
end; // end of if
CloseHandle(token);
end;
Procedure TFrmShutDown.ShutDownSystem(EWX_Type:Integer);
begin
if GetWinVer=2 then //如果當前系統為winNT ,則需要提升權限
begin
SetPrivilege('SeShutdownPrivilege',True); //提升系統權限到可以關機
if (not ExitWindowsEx(EWX_Type,0))then
SetPrivilege('SeShutdownPrivilege',False);//如果關機不成功,還將權限設置回去
end
else //如果當前系統為WIN9X,或者其他,則直接調用API函數
ExitWindowsEx(EWX_Type,0);
end;
procedure TFrmShutDown.FormShow(Sender: TObject);
begin
RadioButton1.Checked:=True;
end;
procedure TFrmShutDown.FormCreate(Sender: TObject);
begin
if GetWinVer=2 then
begin
label1.Caption:=Label1.Caption+'WindowsNT/2000';
end // end of then
else
begin
label1.Caption:=Label1.Caption+'Windows95/98';
end; // end of else
end;
procedure TFrmShutDown.BitBtn1Click(Sender: TObject);
begin
if Application.MessageBox('你是否決定執行本操作?','系統詢問'
,MB_OKCANCEL+MB_OK)=IDOK then
begin
//根據用戶選擇的操作類型,執行相應的操作
if (RadioButton1.Checked)then
ShutDownSystem(EWX_SHUTDOWN)
else if (RadioButton2.Checked)then
ShutDownSystem(EWX_REBOOT)
else if (RadioButton3.Checked)then
ShutDownSystem(EWX_LOGOFF)
else if (RadioButton4.Checked)then
ShutDownSystem(EWX_POWEROFF)
end;
end;
procedure TFrmShutDown.BitBtn2Click(Sender: TObject);
begin
Close;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -