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

📄 timershutdown.pas

📁 一个DELPHI的定时关机程序,可在系统托盘区运行。
💻 PAS
字号:
unit TimerShutDown;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, XPMan, StdCtrls, Buttons, ComCtrls, ExtCtrls, Menus, ImgList,
  AppEvnts;

type
  TForm1 = class(TForm)
    TrayIconTimer: TTrayIcon;
    TimerShutDown: TTimer;
    GroupBox1: TGroupBox;
    UpDown1: TUpDown;
    Edit1: TEdit;
    UpDown2: TUpDown;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    ImageList1: TImageList;
    PopupMenu1: TPopupMenu;
    RestoreMenu: TMenuItem;
    N2: TMenuItem;
    ExitMenu: TMenuItem;
    AboutMenu: TMenuItem;
    ApplicationEvents1: TApplicationEvents;
    RadioGroup1: TRadioGroup;
    CheckBox1: TCheckBox;

    procedure TimerShutDownTimer(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure ApplicationEvents1Minimize(Sender: TObject);
    procedure AboutMenuClick(Sender: TObject);
    procedure ExitMenuClick(Sender: TObject);
    procedure RestoreMenuClick(Sender: TObject);
  private
    { Private declarations }
    Count: Integer;
    procedure AdjustToken;
  public
    { Public declarations }

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.AboutMenuClick(Sender: TObject);
begin
  ShowMessage('飘云阁定时关机1.0版');
end;

procedure TForm1.AdjustToken;
var
  hdlProcessHandle: Cardinal;
  hdlTokenHandle: Cardinal;
  tmpLuid: Int64;
  tkp: TOKEN_PRIVILEGES;
  tkpNewButIgnored: TOKEN_PRIVILEGES;
  lBufferNeeded: Cardinal;
  Privilege: array[0..0] of _LUID_AND_ATTRIBUTES;
begin
  hdlProcessHandle := GetCurrentProcess; //get current process pseudohandle
  OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY), hdlTokenHandle);
  // Get the LUID for shutdown privilege.
  LookupPrivilegevalue('', 'SeShutdownPrivilege', tmpLuid);
  Privilege[0].Luid := tmpLuid;
  Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
  tkp.PrivilegeCount := 1; // One privilege to set
  tkp.Privileges[0] := Privilege[0];
  // Enable the shutdown privilege in the access token of this process.
  AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Sizeof(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded);
end;

procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
  TrayIconTimer.Visible := True;
//   Application.ShowMainForm:=False;
  ShowWindow(Application.Handle, SW_HIDE);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  try
    Count := StrToInt(Edit1.Text) * 60 + StrToInt(edit2.Text);
    TrayIconTimer.Hint := '距离' + RadioGroup1.Items[RadioGroup1.itemIndex] + '还有' + IntToStr(count div 60) + '小时' + IntToStr(count mod 60) + '分钟';
    TimerShutDown.Enabled := True;
    Application.Minimize;
    TrayIconTimer.Animate:=CheckBox1.Checked;
    TrayIconTimer.AnimateInterval:=TimerShutDown.Interval div 2;
    BitBtn1.Enabled:=False;
  except
    ShowMessage('Please input integer!');
  end;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
  TrayIconTimer.Visible := False;
  TimerShutDown.Enabled := False;
  Close;
end;

procedure TForm1.RestoreMenuClick(Sender: TObject);
begin
  ShowWindow(Application.Handle, SW_SHOWNORMAL);
end;

procedure TForm1.TimerShutDownTimer(Sender: TObject);
begin
  Dec(Count);
  TrayIconTimer.Hint := '距离' + RadioGroup1.Items[RadioGroup1.itemIndex] + '还有' + IntToStr(count div 60) + '小时' + IntToStr(count mod 60) + '分钟';
  if Count = 0 then
  begin
    AdjustToken;
    case RadioGroup1.ItemIndex of
      0:
        ExitWindowsEx(EWX_LOGOFF or EWX_FORCE, 0);

      1:
        ExitWindowsEx(EWX_REBOOT or EWX_FORCE, 0);

      2:
        ExitWindowsEx(EWX_POWEROFF or EWX_SHUTDOWN or EWX_FORCE, 0);

    end;
  end;
end;

procedure TForm1.ExitMenuClick(Sender: TObject);
begin
  Form1.Close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Count := 0;
end;


end.

⌨️ 快捷键说明

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