Delphi 经典游戏程序设计40例 的学习 例25 数字钟

unit R25; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls; type TRei25 = class(TForm) Bevel1: TBevel; Timer1: TTimer; procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure Timer1Timer(Sender: TObject); private { Private declarations } procedure DpDgt(Dt:Byte;X,Y:Word); public { Public declarations } end; var Rei25: TRei25; DigitBmap : TBitmap; MakeBmap : TBitmap; RectS,RectD : TRect; Duse : array[0..9] of Byte = ( $77,$60,$3e,$7c,$69,$5d,$5f,$64,$7f,$7d); implementation {$R *.dfm} procedure TRei25.FormCreate(Sender: TObject); begin Rei25.Canvas.CopyMode := cmSrcCopy; DigitBmap := TBitmap.Create; DigitBmap.LoadFromFile(GetCurrentDir + '\Digit.bmp'); MakeBmap := TBitmap.Create; MakeBmap.Height := 70; MakeBmap.Width := 440; MakeBmap.Canvas.Brush.Color := clWhite; RectD := Rect(0,0,MakeBmap.Width,MakeBmap.Height); MakeBmap.Canvas.FillRect(RectD); MakeBmap.Canvas.CopyMode := cmSrcCopy; RectS := Rect(58,8,72,40); RectD := Rect(106,18,122,50); MakeBmap.Canvas.CopyRect(RectD,DigitBmap.Canvas,RectS); RectS := Rect(56,24,72,40); RectD := Rect(294,42,310,58); MakeBmap.Canvas.CopyRect(RectD,DigitBmap.Canvas,RectS); end; procedure TRei25.DpDgt(Dt:Byte;X,Y:Word); begin RectS := Rect(24,0,56,48); RectD := Rect(X,Y,X + 32,Y + 48); MakeBmap.Canvas.CopyRect(RectD,DigitBmap.Canvas,RectS); if (Duse[Dt] and 1) = 0 then //水漫填充函数 MakeBmap.Canvas.FloodFill(X + 4,Y + 12,clWhite,fsBorder); if (Duse[Dt] and 2) = 0 then MakeBmap.Canvas.FloodFill(X + 4,y + 36,clWhite,fsBorder); if (Duse[Dt] and 4) = 0 then MakeBmap.Canvas.FloodFill(X + 16,Y + 4,clWhite,fsBorder); if (Duse[Dt] and 8) = 0 then MakeBmap.Canvas.FloodFill(X + 16,Y + 24,clWhite,fsBorder); if (Duse[Dt] and 16) = 0 then MakeBmap.Canvas.FloodFill(X + 16,y + 44,clWhite,fsBorder); if (Duse[Dt] and 32) = 0 then MakeBmap.Canvas.FloodFill(X + 28,Y + 12,clWhite,fsBorder); if (Duse[Dt] and 64) = 0 then MakeBmap.Canvas.FloodFill(X + 28,Y + 36,clWhite,fsBorder); end; procedure TRei25.FormClose(Sender: TObject; var Action: TCloseAction); begin DigitBmap.Free; MakeBmap.Free; end; procedure TRei25.Timer1Timer(Sender: TObject); var H,M,S ,MS: Word; begin MakeBmap.Canvas.Brush.Color := clWhite; MakeBmap.Canvas.CopyMode := cmSrcCopy; RectD := Rect(10,18,34,50); MakeBmap.Canvas.FillRect(RectD); DecodeTime(Time,H,M,S,MS); //Time 返回现在的时间的函数, DecodeTimel类型转换函数 if H >= 12 then begin H := H - 12; RectS := Rect(0,24,24,40); RectD := Rect(10,34,34,50); end else begin RectS := Rect(0,8,24,24); RectD := Rect(10,18,34,34); end; MakeBmap.Canvas.CopyRect(RectD,DigitBmap.Canvas,RectS); if H >= 10 then //10需要写1 begin H := H - 10; DpDgt(1,34,10); end else begin RectD := Rect(50,10,66,58); //覆盖掉十位 MakeBmap.Canvas.FillRect(RectD); end; DpDgt(H,74,10); DpDgt(M div 10,122,10); DpDgt(M mod 10,162,10); DpDgt(S div 10,218,10); DpDgt(S mod 10,258,10); DpDgt(MS div 100,314,10); MS := MS mod 100; DpDgt(MS div 10,354,10); DpDgt(MS mod 10,394,10); RectS := Rect(0,0,440,70); RectD := Rect(Bevel1.Left + 3,Bevel1.Top + 3, Bevel1.Left + 440,Bevel1.Top + 70); Rei25.Canvas.CopyRect(RectD,MakeBmap.Canvas,RectS); end; end.
1,程序结构在 TIMER中调用时间,绘图。
2,FLOODFILL 函数
3,DecodeTime(Time,H,M,S,MS); 函数
4,理解该程序,需要看时钟界面设计图,Digit.bmp图形,这些坐标是固定的