我的一个时钟控件源代码

来源:互联网 发布:DEA算法 编辑:程序博客网 时间:2024/06/01 07:12

unit ddgclock;

interface

 uses windows,classes,sysutils,extctrls,messages,controls,forms;

 type

    TTimerEvent = Procedure (Sender:TObject;DDGTime:TdateTime) of object;

    TddgdigitalClock=class(TPanel)
      private
        FHour,
        FMinute,
        FSecond:word;
        FDateTime : TDateTime;

        FOldMinute,
        FOldsecond:word;

        FTimer : TTimer;

        { event }
        FOnHour:TTimerEvent;
        FOnHalfPast:TTimerEvent;
        FOnMinute:TTimerEvent;
        FOnHalfMinute:TTimerEvent;
        FOnSecond:TTimerEvent;

        procedure TimerProc(sender:tobject);

      protected
        procedure Paint;override;
        procedure DoHour(TM:TDateTime);dynamic;
        procedure DoHalfPast(TM:TDateTime);dynamic;
        procedure DoMinute(TM:TDateTime);dynamic;
        procedure DoHalfMinute(TM:TDateTime);dynamic;
        procedure DoSecond(TM:TDateTime);dynamic;
      public
        constructor create(aOwner:Tcomponent);override;
        destructor destroy;override;
      published
        property OnHour:TTimerEvent read FOnHour write FOnHour;
        property OnHalfPast:TTimerEvent read FOnHalfPast write FOnHalfPast;
        property OnMinute:TTimerEvent read FOnMinute write FOnMinute;
        property OnHalfMinute:TTimerEvent read FOnHalfMinute write FOnHalfMinute;
        property OnSecond:TTimerEvent read FOnSecond write FOnSecond;


    end;

    procedure Register;

implementation

  constructor TddgDigitalClock.Create(AOwner:Tcomponent);
  begin
    inherited create(aOwner);
    self.Height := 25;
    self.Width := 120;
    BevelInner := bvLowered;
    BevelOuter := bvLowered;

    self.caption:='';  // inherited caption := '';

    FTimer := TTimer.Create(self);
    FTimer.Interval := 200;
    FTimer.OnTimer := self.TimerProc;
   

  end;

  Destructor TddgdigitalClock.Destroy;
  begin
    Ftimer.Free;
    inherited;
  end;

  procedure TddgDigitalClock.Paint;
  begin
    inherited paint;
    caption := TimeToStr(self.FDateTime);
   
  end;

  procedure TddgDigitalClock.TimerProc(sender:Tobject);
  var
    Hsec:word;
  begin
    FOldMinute := FMinute;
    FOldSecond := FSecond;

    FDateTime := now;
    sysutils.DecodeTime(FDatetime,FHour,FMinute,FSecond,hsec);

    refresh;   // 试着不用看有什么效果

    if FMinute = 0 then
      DoHour(FDateTime);
    if FMinute = 29 then
      DoHalfPast(FDateTime);
    {if FSecond = 0 then
      DoMinute(FDateTime);
    if FSecond = 29 then
      DoHalfMinute(FDateTime);}
    if FMinute <> FOldMinute then
    begin
      DoMinute(FDateTime);
    end;
    if FSecond <> Foldsecond then
    begin
      if ((FSecond=0) or (FSecond=29)) then
        DoHalfMinute(FDateTime)
      else
        DoSecond(FDateTime);
    end;

  end;

procedure TddgDigitalClock.DoHour(TM:TDateTime);
begin
  if assigned(FOnHour) then
    FOnHour(self.FTimer,tm);
end;

procedure TddgDigitalClock.DoMinute(TM:TDateTime);
begin
  if Assigned(FOnMinute) then
    FOnMinute(self,TM);
end;

procedure TddgDigitalClock.DoHalfPast(TM:tDateTime);
begin
  if Assigned(FOnHalfPast) then
    FOnHalfPast(self,TM);
end;

procedure TddgDigitalClock.DoSecond(TM:TDateTime);
begin
  if Assigned(FOnSecond) then
     FOnSecond(self,Tm);
end;
procedure TddgDigitalClock.DoHalfMinute(TM:TDateTime);
begin
  if Assigned(FOnHalfMinute) then
    FOnHalfMinute(self,TM);
end;

  procedure Register;
  begin
    RegisterComponents('Test',[TddgDigitalClock]);
  end;
end.