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

📄 unit1.pas

📁 定时关机程序
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, SUIButton, StdCtrls, SUIEdit, SUIImagePanel, ExtCtrls, SUIForm,
  ComCtrls, SUIListView, Menus, SUIPopupMenu,ShellAPI;

const
  TRAY_CALLBACK = WM_USER + $7258;
  WM_BARICON=WM_USER+200; 
  RSP_SIMPLE_SERVICE=1;     //注册热键
         
type
  TForm1 = class(TForm)
    suiForm1: TsuiForm;
    suiPanel1: TsuiPanel;
    Timer1: TTimer;
    suiPanel2: TsuiPanel;
    Label3: TLabel;
    Label2: TLabel;
    suiRadioButton1: TsuiRadioButton;
    suiRadioButton2: TsuiRadioButton;
    suiEdit1: TsuiEdit;
    suiButton1: TsuiButton;
    suiButton2: TsuiButton;
    suiListView1: TsuiListView;
    suiPopupMenu1: TsuiPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    suiButton3: TsuiButton;
    suiButton5: TsuiButton;
    procedure Timer1Timer(Sender: TObject);
    procedure suiButton1Click(Sender: TObject);
    procedure suiButton2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure N1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure suiButton5Click(Sender: TObject);
    procedure suiButton3Click(Sender: TObject);

  private
    { Private declarations }
    procedure ListAll;
    procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY;    //热键
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  App_Path:string;
implementation
uses Unit2, Unit3;
{$R *.dfm}

function EnableDebugPrivilege(const CommandEx:string='SeDebugPrivilege'): Boolean;
    function EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable: Boolean): Boolean;
    var
      TP: TOKEN_PRIVILEGES;
      Dummy: Cardinal;
    begin
      TP.PrivilegeCount := 1;
      LookupPrivilegeValue(nil, pchar(PrivName), TP.Privileges[0].Luid);
      if bEnable then
         TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
      else
         TP.Privileges[0].Attributes := 0;
      AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy);
      Result := GetLastError = ERROR_SUCCESS;
    end;
var
  hToken: Cardinal;
begin
  OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken);   //or TOKEN_QUERY
  result:=EnablePrivilege(hToken, CommandEx, True); //SeShutdownPrivilege关机,SeDebugPrivilege调试
  CloseHandle(hToken);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  i,j:Integer;
begin
Label2.Caption:=TimeToStr(Now);
  if not suiButton1.Enabled then
  begin
     j:=suiListView1.Items.Count;
     if Label2.Caption=suiEdit1.Text then
     begin
        Timer1.Enabled:=False;
        EnableDebugPrivilege('SeShutdownPrivilege');
        
        if suiRadioButton1.Checked then
          ExitWindowsEx(EWX_POWEROFF Or EWX_FORCE, 0)
        else
          ExitWindowsEx(EWX_REBOOT Or EWX_FORCE, 0);
        Exit;  
     end;

     for i := 0 to j-1 do
     begin
         if AnsiCompareText(Label2.Caption,suiListView1.Items.Item[i].Caption)=0 then
            ShellExecute(GetDesktopWindow,'OPEN',PChar(suiListView1.Items.Item[i].SubItems.Strings[0]),
                        PChar(suiListView1.Items.Item[i].SubItems.Strings[1]),nil,5);
     end;
  end;

end;

procedure TForm1.suiButton1Click(Sender: TObject);
begin
suiButton1.Enabled:=False;
Timer1.Enabled:=True;

WritePrivateProfileString('Main','State','1',PChar(App_Path + 'Config.ini'));

WritePrivateProfileString('Main','StartTime',PChar(suiEdit1.Text),PChar(App_Path + 'Config.ini'));

if suiRadioButton2.Checked then
   WritePrivateProfileString('Main','State_SR','-1',PChar(App_Path + 'Config.ini'))
else
   WritePrivateProfileString('Main','State_SR','0',PChar(App_Path + 'Config.ini'));

end;

procedure TForm1.suiButton2Click(Sender: TObject);
begin
suiButton1.Enabled:=True;
Timer1.Enabled:=False;
WritePrivateProfileString('Main','State','0',PChar(App_Path + 'Config.ini'));
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Timer1.Tag=0 then
begin
   Action := caNone;
   Self.Visible:=False;
   Exit;
end;  
Timer1.Enabled:=False;
form1.Release;
form1:=nil;
end;

procedure TForm1.N1Click(Sender: TObject);
var
  Lvi: TListItem;
begin
  case TComponent(Sender).Tag of
    0:
      begin
        form2 := TForm2.Create(nil);
        Lvi := suiListView1.Selected;
        if Lvi<>nil then
        begin
           Form2.suiEdit1.Text:=Lvi.Caption;
           Form2.suiEdit2.Text:=Lvi.SubItems.Strings[0];
           Form2.suiEdit3.Text:=Lvi.SubItems.Strings[1];
        end;
        Form2.ShowModal;
      end;
    1:
      begin
        Lvi := suiListView1.Selected;
        if Lvi=nil then  Exit;
        WritePrivateProfileString('TimeRunP',PChar(Lvi.SubItems.Strings[0] + ',' + Lvi.SubItems.Strings[1]),nil,PChar(App_Path + 'Config.ini'));
        Lvi.Delete;
      end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  TempTime:PChar;
begin
  App_Path := ExtractFilePath(ParamStr(0));
  
//  show;
//  Application.ProcessMessages;

  ListAll;

  GetMem(TempTime,30);
  GetPrivateProfileString('Main','StartTime','00:80:00',TempTime,30,PChar(App_Path + 'Config.ini'));
  suiEdit1.Text:=TempTime;
  freeMem(TempTime,30);

  suiRadioButton2.Checked := Boolean(GetPrivateProfileInt('Main','State_SR',0,PChar(App_Path + 'Config.ini')));

  if GetPrivateProfileInt('Main','State',0,PChar(App_Path + 'Config.ini'))=1 then
     suiButton1Click(suiButton1);

  //注册热键
  RegisterHotKey(handle,8781,MOD_Alt,VK_F11);

end;

procedure TForm1.ListAll;
var
  tmpPath,P,SubTempPath:PChar;
  Lvi:TListItem;
  i:Integer;
begin
      suiListView1.Clear;
      
      GetMem(tmpPath,16384);          //读出子项下面的内容
      if GetPrivateProfileString('TimeRunP',nil,nil,tmpPath,16384,PChar(app_path+'Config.ini'))<>0 then
      begin
            P := tmpPath;
            while P^ <> #0 do
            begin

              GetMem(SubTempPath,MAX_PATH);
              GetPrivateProfileString('TimeRunP',p,'',SubTempPath,MAX_PATH,PChar(app_path+'Config.ini'));

              Lvi := suiListView1.Items.Add;
              Lvi.Caption:=SubTempPath;
              i:=Pos(',',p);
              if i>0 then
              begin
                Lvi.SubItems.Add(Copy(p,1,i-1));
                Lvi.SubItems.Add(Copy(p,i+1,Length(p)-i));
              end
              else
              begin
                Lvi.SubItems.Add(p);
                Lvi.SubItems.Add('');
              end;

              FreeMem(SubTempPath,MAX_PATH);

              Inc(P, StrLen(P) + 1);
            end;
      end;
      freeMem(tmpPath,16384);
end;

procedure TForm1.suiButton5Click(Sender: TObject);
begin
Service2.ServiceThread.Terminate;
end;

procedure TForm1.WMHotKey (var Msg : TWMHotKey);
begin
  if msg.HotKey <> 8781 then exit;
  Self.Visible := not Self.Visible;
  windows.SetForegroundWindow(Handle);
end;

procedure TForm1.suiButton3Click(Sender: TObject);
begin
ShellExecute(GetDesktopWindow,'open',PChar(ParamStr(0)),' /uninstall',nil,5);
suiButton3.Enabled:=False;
end;

end.

⌨️ 快捷键说明

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