如何在delphi7中制作数字时钟?

Art*_*tof 9 delphi pascal delphi-7

我是delphi的新手,想从容易开始.有人可以告诉我一个如何制作一个将"时间"(小时,分钟,秒)转移到标签的数字时钟的例子吗?或类似的东西

And*_*and 42

练习1

删除表单上的a TLabel和a TButton.

双击按钮,然后写入

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := TimeToStr(Time);
end;
Run Code Online (Sandbox Code Playgroud)

练习2

要获得自动更新的时间,TTimer请在表单中添加一个,然后双击它(如果愿意,可以删除该按钮).然后写

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Label1.Caption := TimeToStr(Time);
end;
Run Code Online (Sandbox Code Playgroud)

此代码将每秒运行一次(a的默认间隔TTimer,这对我们来说是完美的,因此我们不需要更改它).

练习3

为了让时钟更烦人,您可以尝试这样:在表单的界面中,添加一个名为的私有字段FHighlight,如下所示:

TForm1 = class(TForm)
  Button1: TButton;
  Label1: TLabel;
  Timer1: TTimer;
  procedure Button1Click(Sender: TObject);
  procedure Timer1Timer(Sender: TObject);
private
  { Private declarations }
  FHighlight: boolean;
public
  { Public declarations }
end;
Run Code Online (Sandbox Code Playgroud)

现在你可以做到

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Label1.Caption := TimeToStr(Time);
  if FHighlight then
  begin
    Label1.Color := clWhite;
    Label1.Font.Color := clBlack;
  end
  else
  begin
    Label1.Color := clBlack;
    Label1.Font.Color := clWhite;
  end;
  FHighlight := not FHighlight;
end;
Run Code Online (Sandbox Code Playgroud)

为了使此效果起作用,您需要更改TLabel控件的一个属性(设计时).使用Object Inspector 更改Transparentfalse(如果尚未使用).

更新(练习4)

由于Warren P认为它太平淡无味TLabel,这就是你如何才能实现"真正的"七段数字时钟:

procedure TForm1.FormPaint(Sender: TObject);
type
  TDigitData = array[0..6] of boolean;
  TPhysDigit = array[0..7] of TRect;
const
  DIGIT: array[0..9] of TDigitData =
    (
      (true, true, true, true, true, true, false),
      (false, true, true, false, false, false, false),
      (true, true, false, true, true, false, true),
      (true, true, true, true, false, false, true),
      (false, true, true, false, false, true, true),
      (true, false, true, true, false, true, true),
      (true, false, true, true, true, true, true),
      (true, true, true, false, false, false, false),
      (true, true, true, true, true, true, true),
      (true, true, true, true, false, true, true)
    );
var
  PaddingW,
  PaddingH,
  UnitX,
  UnitY,
  DigitWidth,
  DigitHeight,
  BarLengthX,
  BarLengthY,
  DigitSeparation,
  FieldSeparation: integer;
  SEGMENTS: array[0..5] of TPhysDigit;
  i: Integer;

  function TranslatePhysDigit(const PhysDigit: TPhysDigit; const DX: integer; const DY: integer = 0): TPhysDigit;
  var
    i: Integer;
  begin
    for i := 0 to 7 do
    begin
      result[i].Left := PhysDigit[i].Left + DX;
      result[i].Right := PhysDigit[i].Right + DX;
      result[i].Top := PhysDigit[i].Top + DY;
      result[i].Bottom := PhysDigit[i].Bottom + DY;
    end;
  end;

  procedure DrawDigit(const Position, Value: integer);
  var
    i: integer;
  begin
    for i := 0 to 6 do
      if DIGIT[Value, i] then
        Canvas.FillRect(SEGMENTS[Position, i]);
  end;

  procedure DrawColon(const Position: integer);
  var
    ColonRect1: TRect;
    ColonRect2: TRect;
  begin
    ColonRect1 := Rect(PaddingW + Position*UnitX, PaddingH + UnitY,
      PaddingW + (Position+1)*UnitX, PaddingH + 2*UnitY);
    ColonRect2 := Rect(PaddingW + Position*UnitX, PaddingH + 3*UnitY,
      PaddingW + (Position+1)*UnitX, PaddingH + 4*UnitY);
    Canvas.FillRect(ColonRect1);
    Canvas.FillRect(ColonRect2);
  end;

var
  t: string;

begin
  PaddingW := Width div 20;
  PaddingH := Height div 20;
  UnitX := (ClientWidth - 2*PaddingW) div 27;
  UnitY := (ClientHeight - 2*PaddingH) div 5;
  DigitWidth := 3*UnitX;
  DigitHeight := 5*UnitY;
  BarLengthX := 3*UnitX;
  BarLengthY := 3*UnitY;
  DigitSeparation := 4*UnitX;
  FieldSeparation := 6*UnitX;
  SEGMENTS[0, 0] := Rect(0, 0, DigitWidth, UnitY);
  SEGMENTS[0, 1] := Rect(DigitWidth - UnitX, 0, DigitWidth, BarLengthY);
  SEGMENTS[0, 2] := Rect(DigitWidth - UnitX, 2*UnitY, DigitWidth, DigitHeight);
  SEGMENTS[0, 3] := Rect(0, DigitHeight - UnitY, DigitWidth, DigitHeight);
  SEGMENTS[0, 4] := Rect(0, 2*UnitY, UnitX, DigitHeight);
  SEGMENTS[0, 5] := Rect(0, 0, UnitX, BarLengthY);
  SEGMENTS[0, 6] := Rect(0, 2*UnitY, DigitWidth, 3*UnitY);
  SEGMENTS[0] := TranslatePhysDigit(SEGMENTS[0], PaddingW, PaddingH);
  SEGMENTS[1] := TranslatePhysDigit(SEGMENTS[0], DigitSeparation);
  SEGMENTS[2] := TranslatePhysDigit(SEGMENTS[1], FieldSeparation);
  SEGMENTS[3] := TranslatePhysDigit(SEGMENTS[2], DigitSeparation);
  SEGMENTS[4] := TranslatePhysDigit(SEGMENTS[3], FieldSeparation);
  SEGMENTS[5] := TranslatePhysDigit(SEGMENTS[4], DigitSeparation);
  Canvas.Brush.Color := clBlack;
  Canvas.FillRect(ClientRect);
  Canvas.Brush.Color := clBlack;
  Canvas.FillRect(Rect(PaddingW, PaddingH, ClientWidth - PaddingW,
    ClientHeight - PaddingH));
  Canvas.Brush.Color := clRed;
  t := FormatDateTime('hhnnss', Time);

  for i := 0 to 5 do
    DrawDigit(i, StrToInt(Copy(t, i+1, 1)));

  if odd(StrToInt(Copy(t, 6, 1))) then
  begin
    DrawColon(8);
    DrawColon(18);
  end;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  Invalidate;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Invalidate;
end;
Run Code Online (Sandbox Code Playgroud)

七段数字时钟

使用GDI画笔:

七段数字时钟

  • +1如果你运气不好,它可以在一次更新中提前2秒.较小的间隔可以解决这个问题. (11认同)
  • 也可以[作为一张玻璃](http://privat.rejbrand.se/seven-segment-glass.png). (4认同)
  • 为了好玩,您至少应该从使用无聊的旧TLabel变为7段"LED".:-) (3认同)
  • @Warren P:好的,你去吧. (3认同)
  • 嗯,还很无聊,GDI动画布谷鸟的模拟时钟怎么样:-)(+1,干得好) (3认同)
  • @TLama:如果不是因为我需要明天起床的(悲伤)事实,我会马上就开始了. (2认同)
  • 哦,多么甜蜜的教程..我们很幸运,我们有一些优秀的老师愿意分享..谢谢..我喜欢这个教程.. (2认同)

NGL*_*GLN 15

这里下载我的开源NLDDigiLabel组件,将其中三个放在表单上,​​在两者之间放置两个常用标签作为时间分隔符,并设置表单的背景颜色.在这个例子中,为方便起见,我在框架上做了所有这些:

unit Unit2;

interface

uses
  Windows, SysUtils, Classes, Graphics, Controls, Forms, StdCtrls, ExtCtrls,
  NLDDigiLabel;

type
  TDigitalClock = class(TFrame)
    HoursLabel: TNLDDigiLabel;
    MinsLabel: TNLDDigiLabel;
    SecsLabel: TNLDDigiLabel;
    TimeSeparator1: TLabel;
    TimeSeparator2: TLabel;
    Timer: TTimer;
    procedure TimerTimer(Sender: TObject);
  private
    FTime: TTime;
    procedure SetTime(Value: TTime);
  public
    property Time: TTime read FTime write SetTime;
  end;

implementation

{$R *.dfm}

{ TDigitalClock }

procedure TDigitalClock.SetTime(Value: TTime);
var
  Hours: Word;
  Mins: Word;
  Secs: Word;
  MSecs: Word;
begin
  if FTime <> Value then
  begin
    FTime := Value;
    DecodeTime(FTime, Hours, Mins, Secs, MSecs);
    HoursLabel.Value := Hours;
    MinsLabel.Value := Mins;
    SecsLabel.Value := Secs;
  end;
end;

procedure TDigitalClock.TimerTimer(Sender: TObject);
begin
  SetTime(FTime + 1/SecsPerDay);
end;

end.
Run Code Online (Sandbox Code Playgroud)

现在,在你的表单上删除这样一个框架,etvoilá:

procedure TForm1.FormCreate(Sender: TObject);
begin
  DigitalClock1.Time := Time;
end;
Run Code Online (Sandbox Code Playgroud)

DigitalClock.png