Wednesday, January 03, 2007
Clock
Aquest component hereta les propietats de TCustomLabel per mostrar en el Caption l'hora i la data.
unit Clock;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, DsgnIntf, ShellAPI;
type
TClock = class(TCustomLabel)
private
{ Private declarations }
Timer: TTimer;
FClockEnabled: boolean;
FHour: Word;
FMinute: Word;
FSecond: Word;
FMSecond: Word;
FYear: Word;
FMonth: Word;
FDay: Word;
FShowSeconds: boolean;
FShowDate: boolean;
FShowTime: boolean;
FTime: string;
FDate: string;
procedure RefreshDate;
protected
{ Protected declarations }
procedure SetClockEnabled(AClockEnabled: boolean);
procedure SetShowSeconds(AShowSeconds: boolean);
procedure SetShowTime(AShowTime: boolean);
procedure SetShowDate(AShowDate: boolean);
procedure OnTimer(Sender: TObject); virtual;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property ClockEnabled: boolean read FClockEnabled write SetClockEnabled default True;
property ShowSeconds: boolean read FShowSeconds write SetShowSeconds default True;
property ShowTime: boolean read FShowTime write SetShowTime default True;
property ShowDate: boolean read FShowDate write SetShowDate default True;
property Align;
property Alignment;
property Anchors;
property AutoSize;
property BiDiMode;
property Caption;
property Color;
property Constraints;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property FocusControl;
property Font;
property ParentBiDiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowAccelChar;
property ShowHint;
property Transparent;
property Layout;
property WordWrap;
property Visible;
property OnClick;
property OnDblClick;
property OnDragOver;
property OnDragDrop;
property OnEndDock;
property OnEndDrag;
property OnMouseDown;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;
procedure Register;
implementation
constructor TClock.Create(AOwner: TComponent);
begin
FClockEnabled := True;
FShowSeconds := True;
FShowTime := True;
FShowDate := True;
Timer := TTimer.Create(Self);
Timer.Enabled := FClockEnabled;
Timer.Interval := 1000;
Timer.OnTimer := OnTimer;
inherited;
RefreshDate;
end;
destructor TClock.Destroy;
begin
Timer.Free;
inherited;
end;
procedure TClock.SetClockEnabled(AClockEnabled: boolean);
begin
FClockEnabled := AClockEnabled;
Timer.Enabled := AClockEnabled;
end;
procedure TClock.SetShowSeconds(AShowSeconds: boolean);
begin
FShowSeconds := AShowSeconds;
if FShowSeconds then
begin
FTime := Format('%.2d',[FHour])+':'+Format('%.2d',[FMinute])+':'+Format('%.2d',[FSecond]);
Timer.Interval := 1000;
end
else
begin
FTime := Format('%.2d',[FHour])+':'+Format('%.2d',[FMinute]);
Timer.Interval := 60000;
end;
Caption := FTime + ' ' + FDate;
end;
procedure TClock.SetShowTime(AShowTime: boolean);
begin
FShowTime := AShowTime;
if FShowTime then
begin
if FShowSeconds then
begin
FTime := Format('%.2d',[FHour])+':'+Format('%.2d',[FMinute])+':'+Format('%.2d',[FSecond]);
end
else
begin
FTime := Format('%.2d',[FHour])+':'+Format('%.2d',[FMinute]);
end;
end
else
begin
FTime := '';
end;
Caption := FTime + ' ' + FDate;
end;
procedure TClock.SetShowDate(AShowDate: boolean);
begin
FShowDate := AShowDate;
if FShowDate then
begin
FDate := Format('%.2d',[FDay])+'/'+Format('%.2d',[FMonth])+'/'+Format('%.4d',[FYear]);
end
else
begin
FDate := '';
end;
Caption := FTime + ' ' + FDate;
end;
procedure TClock.OnTimer(Sender: TObject);
begin
RefreshDate;
end;
procedure TClock.RefreshDate;
begin
DecodeTime(Time,FHour,FMinute,FSecond,FMSecond);
DecodeDate(Date,FYear,FMonth,FDay);
//seconds
if FShowSeconds then
begin
FTime := Format('%.2d',[FHour])+':'+Format('%.2d',[FMinute])+':'+Format('%.2d',[FSecond]);
Timer.Interval := 1000;
end
else
begin
FTime := Format('%.2d',[FHour])+':'+Format('%.2d',[FMinute]);
Timer.Interval := 60000;
end;
//time
if FShowTime then
begin
if FShowSeconds then
begin
FTime := Format('%.2d',[FHour])+':'+Format('%.2d',[FMinute])+':'+Format('%.2d',[FSecond]);
end
else
begin
FTime := Format('%.2d',[FHour])+':'+Format('%.2d',[FMinute]);
end;
end
else
begin
FTime := '';
end;
//date
if FShowDate then
begin
FDate := Format('%.2d',[FDay])+'/'+Format('%.2d',[FMonth])+'/'+Format('%.4d',[FYear]);
end
else
begin
FDate := '';
end;
Caption := FTime + ' ' + FDate;
end;
procedure Register;
begin
RegisterComponents('Components Delphi', [TClock]);
end;
end.
La propietat ClockEnabled activa i desactiva l'actualització de data i hora. La propietat ShowSeconds ens permet mostrar o ocultar els segons. Les propietats ShowTime i ShowDate, fan, respectivamente, el mateix amb l'hora i la data.