如何在delphi7中制作数字时钟? [英] How to make a Digital clock in delphi7?

查看:422
本文介绍了如何在delphi7中制作数字时钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很熟悉delphi,而且想要开始一些容易的事情。
有人可以告诉我一个例子,如何使数字时钟将时间(小时,分钟,秒)转移到标签?或者类似的东西

解决方案

练习1



在您的表单上删除 TLabel TButton



双击该按钮,并写入

  procedure TForm1.Button1Click(Sender:TObject); 
begin
Label1.Caption:= TimeToStr(Time);
结束

练习2



为了让时间自动更新,请在您的表单中添加一个 TTimer ,然后双击它(如果您愿意,可以删除该按钮)。然后写入

  procedure TForm1.Timer1Timer(Sender:TObject); 
begin
Label1.Caption:= TimeToStr(Time);
结束

此代码将运行一次( TTimer的默认间隔时间,这对我们来说是完美的,所以我们不需要改变它)。



练习3



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

  TForm1 = class(TForm)
Button1:TButton;
Label1:TLabel;
Timer1:TTimer;
procedure Button1Click(Sender:TObject);
程序Timer1Timer(Sender:TObject);
private
{私有声明}
FHighlight:boolean;
public
{公开声明}
end;

现在你可以做

  procedure TForm1.Timer1Timer(Sender:TObject); 
begin
Label1.Caption:= TimeToStr(Time);
如果FHighlight然后
begin
Label1.Color:= clWhite;
Label1.Font.Color:= clBlack;
end
else
begin
Label1.Color:= clBlack;
Label1.Font.Color:= clWhite;
结束
FHighlight:=不是FHighlight;
结束

为了使此效果正常工作,您需要更改 TLabel control(design-time)。将透明更改为 false ,使用对象检查器(如果尚未)。



更新(练习4)



由于Warren P认为这是无聊的简单 TLabel ,这是如何实现一个真正的七段数字时钟:

  procedure TForm1.FormPaint(Sender:TObject); 
type
TDigitData = array [0..6] of boolean;
TPhysDigit = TRect的数组[0..7]
const
DIGIT:TDigitData =
的数组[0..9](
(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,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:TPhysDigit的数组[0..5];
i:整数;

函数TranslatePhysDigit(const PhysDigit:TPhysDigit; const DX:integer; const DY:integer = 0):TPhysDigit;
var
i:整数;
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;
结束
结束

程序DrawDigit(const Position,Value:integer);
var
i:integer;
begin
for i:= 0 to 6 do
如果DIGIT [Value,i] then
Canvas.FillRect(SEGMENTS [Position,i]);
结束

程序DrawColon(const位置:整数);
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);
结束

var
t:string;

begin
PaddingW:=宽度div 20;
PaddingH:=高度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到5 do
DrawDigit(i,StrToInt(Copy(t,i + 1,1)));

如果奇数(StrToInt(Copy(t,6,1)))然后
begin
DrawColon(8);
DrawColon(18);
结束
结束

procedure TForm1.FormResize(Sender:TObject);
begin
无效;
结束

程序TForm1.Timer1Timer(Sender:TObject);
begin
无效;
结束

七段数字时钟http://privat.rejbrand.se/seven-segment-clock.png



玩GDI画笔:



七段数字时钟http:// privat.rejbrand.se/seven-segment-clock-brush.png


I am pretty new to delphi , and would like to start with something easy . Could someone please show me an example how to make a Digital clock that will transfer the "time" ( hour , min , sec ) to a label ? Or something like that

解决方案

Exercise 1

Drop a TLabel and a TButton on your form.

Double-click the button, and write

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := TimeToStr(Time);
end;

Exercise 2

To get the time to update automatically, add a TTimer to your form, and double-click it (you can remove the button if you like). Then write

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Label1.Caption := TimeToStr(Time);
end;

This code will run once a second (the default interval for a TTimer, which is perfect for us, so we do not need to change it).

Exercise 3

To make the clock more annoying, you can try this: in the interface of your form, add a private field called FHighlight, like this:

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;

Now you can do

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;

In order for this effect to work, you need to change one of the properties of the TLabel control (design-time). Change Transparent to false, using the Object Inspector, if it isn't already.

Update (Exercise 4)

Since Warren P thinks it is too boring with a plain TLabel, this is how you can achieve a 'true' seven-segment digital clock:

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;

Seven-segment digital clock http://privat.rejbrand.se/seven-segment-clock.png

Playing with the GDI brushes:

Seven-segment digital clock http://privat.rejbrand.se/seven-segment-clock-brush.png

这篇关于如何在delphi7中制作数字时钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆