大佬教程收集整理的这篇文章主要介绍了如何在delphi7中制作数字时钟?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在您的表单上放置一个TLabel和一个TButton.
双击该按钮并写入
procedure TForm1.button1Click(Sender: TObject); begin Label1.Caption := TimeToStr(TimE); end;
练习2
要获得自动更新的时间,请将TTimer添加到表单中,然后双击它(如果您愿意,可以删除该按钮).然后写
procedure TForm1.Timer1Timer(Sender: TObject); begin Label1.Caption := TimeToStr(TimE); end;
该代码将运行一秒钟(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;
现在你可以做
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太无聊了,这就是您如何实现“真正的”七段数字时钟:
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 := FormatdatetiR_842_11845@e('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
以上是大佬教程为你收集整理的如何在delphi7中制作数字时钟?全部内容,希望文章能够帮你解决如何在delphi7中制作数字时钟?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。