type TClockWindow =class(TCustomControl)
//By SMP3 .
private
ClockWinRect :TRect;
protected
procedure WMEraseBkgnd(var Message: TWmEraseBkgnd); message WM_ERASEBKGND;
procedure CreateParams(var Params: TCreateParams); override;
procedure Paint;override;
public
procedure ShowClock();
procedure HideClock();
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Color;
property Font;
end;
{ TClockWindow }
constructor TClockWindow.Create(AOwner: TComponent);
var TrayClockWClassHandle :HWND;
begin
inherited;
Color :=clWhite;
TrayClockWClassHandle:=FindWindow('Shell_TrayWnd',0);//TaskBar Handle
TrayClockWClassHandle:=FindWindowEx(TrayClockWClassHandle,0,'TrayNotifyWnd',0);//Parent Clock Handle
TrayClockWClassHandle:=FindWindowEx(TrayClockWClassHandle,0,'TrayClockWClass',0);// Final Clock Handle
GetWindowRect(TrayClockWClassHandle,ClockWinRect );
with ClockWinRect do
begin
Width :=Right - Left;
Height :=Bottom - Top ;
end;
Left := ClockWinRect.Left;
Top:=ClockWinRect.Top;
end;
procedure TClockWindow.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style:=WS_POPUP;// Since it is not a part of your Form
// it does not requires Parent propretie
end;
destructor TClockWindow.Destroy;
begin
inherited;
end;
procedure TClockWindow.HideClock;
begin
HandleNeeded;
SetWindowPos(Handle, HWND_TOPMOST, Left , Top , Width, Height,
SWP_HIDEWINDOW or SWP_NOACTIVATE);
end;
procedure TClockWindow.Paint;
var R:TRect ;
Text:String;
begin
// Get Curent Time
Text :=FormatDateTime('hh:mm', Time);
Canvas.Brush.Color := Color;
Canvas.FillRect(ClientRect);
Canvas.Brush.Color :=ClBlack;
R:=ClientRect;
// Set Canvas background color to Transparent
SetBkMode(Canvas.Handle,TransParent);
Canvas.Font:=Font;
// Draw and Center Text
DrawText(Canvas.Handle,{$IFDEF UNICODE}Text {$ELSE} PAnsiChar (Text){$ENDIF},Length(Text),R, DT_SINGLELINE or DT_VCENTER or DT_CENTER);
end;
procedure TClockWindow.ShowClock;
begin
HandleNeeded ;
SetWindowPos(Handle, HWND_TOPMOST, Left , Top , Width, Height,
SWP_SHOWWINDOW or SWP_NOACTIVATE);
end;
procedure TClockWindow.WMEraseBkgnd(var Message: TWmEraseBkgnd);
begin
Message.Result:=1;
end;