1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| 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,
System.Diagnostics;
type
TForm1 = class(TForm)
lblTime: TLabel;
btnStart: TButton;
btnPause: TButton;
btnResume: TButton;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure btnStartClick(Sender: TObject);
procedure btnPauseClick(Sender: TObject);
procedure btnResumeClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Déclarations privées }
FStopwatch: TStopwatch;
FRunning: Boolean;
FAccumulated: Int64;
function FormatTime(Millis: Int64): string;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.FormatTime(Millis: Int64): string;
var
MS, S, M, H: Int64;
begin
MS := Millis mod 1000;
S := (Millis div 1000) mod 60;
M := (Millis div 60000) mod 60;
H := Millis div 3600000;
Result := Format('%.2d:%.2d:%.2d.%.3d', [H, M, S, MS]);
end;
procedure TForm1.btnPauseClick(Sender: TObject);
begin
if FRunning then
begin
FStopwatch.Stop;
FAccumulated := FAccumulated + FStopwatch.ElapsedMilliseconds;
FRunning := False;
end;
end;
procedure TForm1.btnResumeClick(Sender: TObject);
begin
FStopwatch.Reset;
FAccumulated := 0;
FRunning := False;
lblTime.Caption := '00:00.000';
end;
procedure TForm1.btnStartClick(Sender: TObject);
begin
if not FRunning then
begin
FStopwatch := TStopwatch.StartNew;
FRunning := True;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled := True;
Timer1.Interval := 100;
lblTime.Caption := '00:00.000';
FRunning := False;
FAccumulated := 0;
FStopwatch := TStopwatch.Create;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
TotalMs: Int64;
begin
if FRunning then
TotalMs := FAccumulated + FStopwatch.ElapsedMilliseconds
else
TotalMs := FAccumulated;
lblTime.Caption := FormatTime(TotalMs);
end;
end. |
Partager