⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unitshutdown.pas

📁 界面精美
💻 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 + -