unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.ComCtrls; type TForm1 = class(TForm) Timer1: TTimer; RichEdit1: TRichEdit; procedure FormCreate(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } FLastInputTime: DWORD; function GetLastInputTime: DWORD; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin Timer1.Interval := 1000; // 一分钟 Timer1.Enabled := True; end; procedure TForm1.Timer1Timer(Sender: TObject); begin FLastInputTime := GetLastInputTime; if (GetTickCount - FLastInputTime) > 5000 then begin // RichEdit1.Lines.Add('power off'); SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2); end; if (GetTickCount - FLastInputTime) > 10000 then begin // RichEdit1.Lines.Add('lock station'); LockWorkStation; end; end; function TForm1.GetLastInputTime: DWORD; var lii: TLASTINPUTINFO; begin lii.cbSize := SizeOf(TLASTINPUTINFO); if GetLastInputInfo(lii) then Result := lii.dwTime; end; end.