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

前端之家收集整理的这篇文章主要介绍了如何在delphi7中制作数字时钟?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我对delphi很新,并且想要从容易的东西开始.
有人可以给我一个例子,说明如何使数字时钟将“时间”(小时,分钟,秒)转移到标签上?或类似的东西

解决方法

练习1

在您的表单上放置一个TLabel和一个TButton.

双击该按钮并写入

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

练习2

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

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

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

练习3

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

@H_403_11@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;

现在你可以做

@H_403_11@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;

为了实现这一效果,您需要更改TLabel控件的一个属性(设计时间).将透明度更改为false,使用对象检查器(如果尚未).

更新(练习4)

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

@H_403_11@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,false),(false,false,(true,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,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,UnitY); SEGMENTS[0,1] := Rect(DigitWidth - UnitX,BarLengthY); SEGMENTS[0,2] := Rect(DigitWidth - UnitX,2*UnitY,DigitHeight); SEGMENTS[0,3] := Rect(0,DigitHeight - UnitY,4] := Rect(0,5] := Rect(0,6] := Rect(0,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,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

玩GDI画笔:

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

原文链接:https://www.f2er.com/delphi/101582.html

猜你在找的Delphi相关文章