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

📄 unit1.pas

📁 一个自动关机的小程序,大家可以看看.还可以
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, Mask, Buttons, jpeg, TFlatCheckBoxUnit,
  TFlatButtonUnit, TFlatRadioButtonUnit, TFlatGroupBoxUnit;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Timer1: TTimer;
    Image1: TImage;
    Label2: TLabel;
    Image2: TImage;
    Lab2: TLabel;
    Timer2: TTimer;
    LabSt: TLabel;
    LabState: TLabel;
    LabNow: TLabel;
    GrB1: TFlatGroupBox;
    RB_JS: TFlatRadioButton;
    RB_DS: TFlatRadioButton;
    Label3: TLabel;
    Label4: TLabel;
    Edt_MM: TMaskEdit;
    Edt_H: TMaskEdit;
    Label6: TLabel;
    Edt_M: TMaskEdit;
    Bevel2: TBevel;
    Btn_ok: TFlatButton;
    Btn_cancel: TFlatButton;
    Btn_exit: TFlatButton;
    CkB_beforshut: TFlatCheckBox;
    Lab_CanShutdown: TLabel;
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure Btn_exitClick(Sender: TObject);
    procedure Btn_okClick(Sender: TObject);
    procedure Btn_cancelClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  m,n:integer;
implementation

uses Unit2;

{$R *.DFM}
//NT关机准备函数
function SetPrivilege(sPrivilegeName : string;bEnabled : boolean ): boolean;
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 //Give this privileges
    begin
      TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;
    end
    else begin //NOT Give this privileges
      TP.Privileges[ 0 ].Attributes := 0;
    end;
    dwRetLen := 0;
    //enables or disables privileges in the specified access token.
    Result := AdjustTokenPrivileges(Token,False,TP,SizeOf( TPPrev ),TPPrev,dwRetLen);
  end;
  CloseHandle( Token );
end;

//NT关机函数
function WinExitInNT( iFlags : integer ) : boolean;
begin
  Result := True;
  if( SetPrivilege( 'SeShutdownPrivilege',True ) )then
  begin
    if( not ExitWindowsEx( iFlags,0 ) )then
    begin
      Result := False;
    end;
    SetPrivilege( 'SeShutdownPrivilege',False )
  end
  else begin
  // handle errors...
  Result := False;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  m:=m+1;

  if m>=n then  begin
    if CkB_beforshut.Checked then begin
      form2.show;
      n:=n+1;
      exit;
    end;
    if Lab_CanShutdown.Caption='0' then begin
      // win9x
      if Win32Platform = VER_PLATFORM_WIN32_WINDOWS then
        ExitWindowsEx(EWX_FORCE+EWX_SHUTDOWN+EWX_POWEROFF,32);
      // winNT
      if Win32Platform = VER_PLATFORM_WIN32_NT then
        WinExitInNT(EWX_FORCE+EWX_SHUTDOWN+EWX_POWEROFF);
    end else Btn_cancelClick(Sender);
  end else
    edt_mm.text:=inttostr(n-m);
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
  labnow.caption:=TimeToStr(Time);
end;

procedure TForm1.Btn_exitClick(Sender: TObject);
begin
  close;
end;

procedure TForm1.Btn_okClick(Sender: TObject);
var
  hour,min,sec,msec:word;
begin
  m:=0;
  if rb_ds.Checked then begin
    if (trim(Edt_H.text)='')or(trim(Edt_M.text)='') then exit;
    try
      decodetime(now,hour,min,sec,msec);
      n:=(strtoint(trim(Edt_H.text)))*60+strtoint(trim(Edt_M.text));
      n:=abs(n-(hour*60+min));
      lab2.caption:='开始时间:';
      Timer2.Enabled:=false;
    except
      exit;
    end;
  end else begin
    if trim(Edt_MM.text)='' then exit;
    try
      n:=strtoint(trim(Edt_MM.text));
      lab2.caption:='现在时间:';
      Timer2.Enabled:=true;
    except
      exit;
    end;
  end;
  Timer1.Enabled:=true;
  GrB1.Enabled:=false;
  Btn_ok.Enabled:=false;
  Btn_cancel.Enabled:=true;
  labstate.caption:='已启动';
  hide;

end;

procedure TForm1.Btn_cancelClick(Sender: TObject);
begin
  Timer2.Enabled:=true;
  Timer1.Enabled:=false;
  Btn_ok.Enabled:=true;
  labstate.caption:='等待指示';
  grb1.Enabled:=true;
  lab2.caption:='现在时间:';

end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -