Advertisement
Guest User

Untitled

a guest
Jan 15th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 14.54 KB | None | 0 0
  1. unit DateTimeLabelUnit;  { TDateTimeLabel component. }
  2. {-----------------------------------------------------------------------------}
  3. {  Name   : TDateTimeLabel                                                    }
  4. {  Author : G. Bradley MacDonald                                              }
  5. {  Date   : March 1997                                                        }
  6. {  Cost   : $5.00 US                                                          }
  7. {  Purpose: This component provides the programmer with a Date and Time Label }
  8. {-----------------------------------------------------------------------------}
  9. {  Version:  1.01                                                             }
  10. {            Added property 'RunTimerinDesign' to allow the programmer to see }
  11. {            the timmer event (ie time changing) in Design Mode               }
  12. {  Version:  1.00                                                             }
  13. {            All New                                                          }
  14. {                                                                             }
  15. {-----------------------------------------------------------------------------}
  16.  
  17. interface
  18.  
  19. uses
  20.   Windows,
  21.   SysUtils,
  22.   Messages,
  23.   Classes,
  24.   Graphics,
  25.   Controls,
  26.   Forms,
  27.   Dialogs,
  28.   Menus,
  29.   StdCtrls,
  30.   WinTypes,
  31.   WinProcs,
  32.   ExtCtrls;
  33.  
  34. type
  35.   TShadowStyle = (Raised, Lowered, HasShadow, NormalWithDisable, NoShadow);
  36.   TLabelType = (dtDate, dtTime, dtDateTime);
  37.  
  38. type
  39.   TDateTimeLabel = class(TCustomLabel)
  40.   private
  41.     FRefreshTimer: TTimer;
  42.     FLabelType: TLabelType;
  43.     FDateTimeFormat: String;
  44.     FAutoFormat: Boolean;
  45.     FRunTimerInDesign: Boolean;
  46.  
  47.     { Private declarations }
  48.     FRotationAngle: Integer;
  49.     FShadowOffset: Integer;
  50.     FShadowStyle: TShadowStyle;
  51.     FHighlightColor: TColor;
  52.     FShadowColor: TColor;
  53.     {$IFDEF Win32}
  54.     FUnicodeCaption: Boolean;
  55.     {$ENDIF}
  56.     procedure SetRotationAngle(newAngle: Integer);
  57.     procedure SetShadowOffset(newShadowOffset: Integer);
  58.     procedure SetHighlightColor(newColor: TColor);
  59.     procedure SetShadowColor(newColor: TColor);
  60.     procedure SetShadowStyle(newStyle: TShadowStyle);
  61.     procedure RedrawLabel;
  62.     procedure SetDateTimeFormat(newValue: String);
  63.     procedure SetLabelType(newValue: TLabelType);
  64.     procedure SetInterval_FRefreshTimer(newValue: Cardinal);
  65.     function GetInterval_FRefreshTimer: Cardinal;
  66.     procedure Timer_FRefreshTimerHandler(Sender: TObject);  { TNotifyEvent }
  67.   protected
  68.     { Protected declarations }
  69.     procedure Paint; override;
  70.     property Transparent default False;
  71.   public
  72.     { Public declarations }
  73.     constructor Create(AOwner: TComponent); override;
  74.     destructor Destroy; override;
  75.   published
  76.     { Published properties and events }
  77.     { Inherited properties: }
  78.     property Align;
  79.     property Alignment;
  80.     property AutoSize;
  81.     property Caption;
  82.     property Color;
  83.     property Font;
  84.     property PopupMenu;
  85.     property ShowHint;
  86.     property WordWrap;
  87.     property Rotation: Integer read FRotationAngle write SetRotationAngle default 0;
  88.     property ShadowStyle: TShadowStyle read FShadowStyle write SetShadowStyle;
  89.     property ShadowColor: TColor read FShadowColor write SetShadowColor default clBtnShadow;
  90.     property HighlightColor: TColor read FHighlightColor write SetHighlightColor default clBtnHighlight;
  91.     property ShadowOffset: integer read FShadowOffset write SetShadowOffset default 1;
  92.     {$IFDEF Win32}
  93.     property UnicodeCaption: boolean read FUnicodeCaption write FUnicodeCaption default False;
  94.     {$ENDIF}
  95.     property AutoFormat: Boolean read FAutoFormat write FAutoFormat default True;
  96.     property RunTimerinDesign: Boolean read FRunTimerInDesign write FRunTimerInDesign default False;
  97.     property DateTimeFormat: String read FDateTimeFormat write SetDateTimeFormat;
  98.     property LabelType: TLabelType read FLabelType write SetLabelType;
  99.     property Interval_FRefreshTimer: Cardinal read GetInterval_FRefreshTimer write SetInterval_FRefreshTimer default 0;
  100.   end;  { TDateTimeLabel }
  101.  
  102. procedure Register;
  103.  
  104. implementation
  105.  
  106. procedure TDateTimeLabel.SetRotationAngle(newAngle: Integer);
  107. { sets the angle for text rotation (limited to -180° and +180°, inclusive). }
  108. var
  109.   localTextMetrics: TTextMetric;
  110. begin
  111.     if newAngle > 0 then
  112.     newAngle := newAngle mod 360
  113.     else
  114.     newAngle := -(abs(newAngle) mod 360);
  115.  
  116.     if newAngle > 180 then
  117.     newAngle := newAngle - 360
  118.     else if newAngle < -180 then
  119.     newAngle := newAngle + 360;
  120.  
  121.     if newAngle <> FRotationAngle then
  122.   begin
  123.     FRotationAngle := newAngle;
  124.     if (not (csLoading in ComponentState)) and (FRotationAngle <> 0) then    { We're rotating. }
  125.     begin    { Make sure we've got a TrueType font... }
  126.       Canvas.Font := Self.Font;
  127.       GetTextMetrics(Canvas.Handle, localTextMetrics);
  128.       if (localTextMetrics.tmPitchAndFamily and TMPF_TrueType) = 0 then    { Not TrueType! }
  129.       begin
  130.         { You may want to inform users that they can only rotate TrueType fonts. }
  131.         Font.Name := 'Arial';    { Force it. }
  132.       end;    { if }
  133.     end;    { if }
  134.     RedrawLabel;
  135.   end;
  136. end;        { SetRotationAngle }
  137.  
  138. Procedure TDateTimeLabel.SetShadowOffset(newShadowOffset: Integer);
  139. { Sets data member FShadowOffset to newShadowOffset. }
  140. begin
  141.   if FShadowOffset <> newShadowOffset then
  142.   begin
  143.     FShadowOffset := newShadowOffset;
  144.     RedrawLabel;
  145.   end;
  146. end;        { SetShadowOffset }
  147.  
  148. procedure TDateTimeLabel.SetHighlightColor(newColor: TColor);
  149. { Sets data member FHighlightColor to newColor. }
  150. begin
  151.     if FHighlightColor <> newColor then
  152.     begin
  153.     FHighlightColor := newColor;
  154.     RedrawLabel;
  155.     end;
  156. end;        { SetHighlightColor }
  157.  
  158. procedure TDateTimeLabel.SetShadowColor(newColor: TColor);
  159. { Sets data member FShadowColor to newColor. }
  160. begin
  161.   if FShadowColor <> newColor then
  162.   begin
  163.     FShadowColor := newColor;
  164.     RedrawLabel;
  165.   end;
  166. end;        { SetShadowColor }
  167.  
  168. procedure TDateTimeLabel.SetShadowStyle(newStyle: TShadowStyle);
  169. { Sets data member SetShadowStyle to newStyle. }
  170. begin
  171.   if FShadowStyle <> newStyle then
  172.   begin
  173.     FShadowStyle := newStyle;
  174.     RedrawLabel;
  175.   end;
  176. end;        { SetShadowStyle }
  177.  
  178. procedure TDateTimeLabel.RedrawLabel;
  179. { Forces transparent to false temporarily. }
  180. var
  181.   saveTransparent: boolean;
  182. begin
  183.  
  184.   If FLabelType = dtDate then Begin
  185.     If FDateTimeFormat = '' then FDateTimeFormat := 'ddddd';
  186.     Caption := FormatDateTime(FDateTimeFormat,Date());
  187.   End;
  188.   If FLabelType = dtTime then Begin
  189.     If FDateTimeFormat = '' then FDateTimeFormat := 't';
  190.     Caption := FormatDateTime(FDateTimeFormat,Time());
  191.   End;
  192.   If FLabelType = dtDateTime then Begin
  193.     If FDateTimeFormat = '' then FDateTimeFormat := 'c';
  194.     Caption := FormatDateTime(FDateTimeFormat,Now());
  195.   End;
  196.  
  197.   saveTransparent := Transparent;
  198.   Transparent := False;
  199.   Invalidate;
  200.   Transparent := saveTransparent;
  201. end;  { RedrawLabel }
  202.  
  203. procedure TDateTimeLabel.Paint;
  204. var
  205.   localCaption,
  206.   localFaceName: string;
  207.   localOffset,
  208.   X,
  209.   Y,
  210.   newHeight,
  211.   newWidth: integer;
  212.   newFontHandle: HFont;
  213.   oldFontHandle: HFont;
  214.   stringWidth,
  215.   stringHeight,
  216.   R: real;
  217.   localTextExtent: Longint;
  218.   {$IFDEF Win32}
  219.   Size: TSize;
  220.   {$ENDIF}
  221.  
  222.   procedure LocalDrawText(deltaX, deltaY: Integer; textColor: TColor);
  223.   var
  224.     localRect: TRect;
  225.   begin
  226.     SetTextColor(Canvas.Handle, ColorToRGB(textColor));
  227.     ExtTextOut(Canvas.Handle, X + deltaX, Y + deltaY, 0, @localRect, @localCaption[1], Length(localCaption), nil);
  228.   end;    { LocalDrawText }
  229.  
  230. begin       { Paint }
  231.   if not Transparent then  { Erase background. }
  232.     with Canvas do
  233.     begin
  234.       Brush.Color := Self.Color;
  235.       Brush.Style := bsSolid;
  236.         FillRect(ClientRect);
  237.     end;  { with }
  238.  
  239.   localCaption := Caption;
  240.  
  241.   { Make localFaceName null-terminated: }
  242.   localFaceName := Font.Name + #0;
  243.  
  244.   { Create the rotated font: }
  245.   newFontHandle:= CreateFont(Font.Height,
  246.                         0,    { Width }
  247.                         FRotationAngle * 10,    { Escapement - Rotation is in tenths of a degree. }
  248.                         0,    { Orientation }
  249.                         ord(fsBold in Font.Style) * FW_BOLD,    { Weight }
  250.                         ord(fsItalic in Font.Style),    { Italic }
  251.                         ord(fsUnderLine in Font.Style),    { Underline }
  252.                         ord(fsStrikeOut in Font.Style),    { StrikeOut }
  253.                         DEFAULT_CHARSET,    { Charset }
  254.                         OUT_DEFAULT_PRECIS,    { Output Precision }
  255.                         CLIP_DEFAULT_PRECIS,    { Clipping Precision }
  256.                         DEFAULT_QUALITY,    { Quality }
  257.                         DEFAULT_PITCH + FF_DONTCARE,    { Pitch }
  258.                         @localFaceName[1]);    { "[1]" points to first character in string }
  259.  
  260.   { Select the new font into the HDC }
  261.   oldFontHandle:= SelectObject(Canvas.Handle, newFontHandle);
  262.  
  263.   { Calculate the height and width of the text. }
  264.   {$IFDEF Win32}
  265.   If FUnicodeCaption Then
  266.     GetTextExtentPointW(Canvas.Handle, @localCaption[1], Length(localCaption), Size)
  267.   Else
  268.     GetTextExtentPointA(Canvas.Handle, @localCaption[1], Length(localCaption), Size);
  269.   stringWidth := Size.cx;
  270.   stringHeight := Size.cy;
  271.   {$ELSE}
  272.   localTextExtent := GetTextExtent(Canvas.Handle, @localCaption[1], Length(localCaption));
  273.   stringWidth := LoWord(localTextExtent);
  274.   stringHeight := HiWord(localTextExtent);
  275.   {$ENDIF}
  276.   R := pi * (FRotationAngle / 180);
  277.  
  278.   { Calculate the length of the text box: }
  279.   newWidth := Round(Abs( stringHeight * sin(R)) + Abs( stringWidth * cos(R))) + FShadowOffset;
  280.   newHeight := Round(Abs( stringWidth * sin(R)) + Abs( stringHeight * cos(R))) + FShadowOffset;
  281.  
  282.   if (Align=alTop) or (Align=alNone) or (Align=alBottom) then
  283.     Height := newHeight;
  284.   if (Align=alLeft) or (Align=alRight) or (align=alNone) then
  285.     Width := newWidth;
  286.  
  287.   { Calculate starting point for text, depending on angle of rotation: }
  288.   if (FRotationAngle >= 0) and (FRotationAngle <= 90) then    { 0..90 degrees }
  289.   begin
  290.     X := 0;
  291.     Y := Abs(Round(stringWidth * sin(R)));
  292.   end
  293.   else if (FRotationAngle >= -90) and (FRotationAngle < 0) then    { -90..-1 degrees }
  294.   begin
  295.     X := Abs(Round(stringHeight * sin(R)));
  296.     Y := 0;
  297.   end
  298.   else if (FRotationAngle > 90) and (FRotationAngle <= 180) then    { 91..180 degrees }
  299.   begin
  300.     X := Abs(Round(stringWidth * cos(R)));
  301.     Y := newHeight;
  302.   end
  303.   else if (FRotationAngle > -180) and (FRotationAngle < -90) then  { -179..-91 degree }
  304.   begin
  305.     X := newWidth;
  306.     Y := Abs(Round(stringHeight * cos(R)));
  307.   end;
  308.  
  309.   { Use a transparent brush to write the text. }
  310.   Canvas.Brush.Style := bsClear;
  311.  
  312.   if FShadowStyle <> NoShadow then
  313.   begin
  314.     localOffset := FShadowOffset;
  315.     if ((FShadowStyle <> HasShadow) and (FShadowStyle <> NormalWithDisable)) or
  316.        ((FShadowStyle = NormalWithDisable) and (not Enabled)) then    { Draw highlighting. }
  317.     begin
  318.       if (FShadowStyle = Lowered) or (FShadowStyle = NormalWithDisable) then
  319.         localOffset := -FShadowOffset;
  320.       LocalDrawText(-localOffset, -localOffset, FHighlightColor);
  321.     end;
  322.  
  323.     if FShadowStyle <> NormalWithDisable then    { Now draw shadow: }
  324.       LocalDrawText(localOffset, localOffset, ShadowColor)
  325.     else if (FShadowStyle = NormalWithDisable) and (not Enabled) then
  326.     begin
  327.       LocalDrawText(0, 0, FShadowColor);
  328.       SelectObject(Canvas.Handle, oldFontHandle);
  329.       DeleteObject(newFontHandle);
  330.       Exit;
  331.     end;
  332.   end;
  333.  
  334.   { Now draw the normal text on top of shadow and highlighting: }
  335.   LocalDrawText(0, 0, Font.Color);
  336.  
  337.   { Restore the previous font used in this HDC. }
  338.   SelectObject(Canvas.Handle, oldFontHandle);
  339.  
  340.   { Delete our rotated font. }
  341.   DeleteObject(newFontHandle);
  342. end;        { Paint }
  343.  
  344. procedure TDateTimeLabel.SetDateTimeFormat(newValue: String);
  345. { Sets data member FDateTimeFormat to newValue. }
  346. begin
  347.   if FDateTimeFormat <> newValue then
  348.   begin
  349.     FDateTimeFormat := newValue;
  350.     {  Add display update code here if needed. }
  351.     ReDrawLabel;
  352.   end;
  353. end;  { SetDateTimeFormat }
  354.  
  355. procedure TDateTimeLabel.SetLabelType(newValue: TLabelType);
  356. { Sets data member FLabelType to newValue. }
  357. begin
  358.   If FLabelType <> newValue then begin
  359.     FLabelType := newValue;
  360.     { Add display update code here if needed. }
  361.     If (FDateTimeFormat = '') or (FAutoFormat) Then Begin
  362.       If FLabelType = dtDateTime then  FDateTimeFormat := 'c';
  363.       If FLabelType = dtDate     then  FDateTimeFormat := 'ddddd';
  364.       If FLabelType = dtTime     then  FDateTimeFormat := 't';
  365.     End;
  366.     ReDrawLabel;
  367.   End;
  368. end;  { SetLabelType }
  369.  
  370. { Exposed properties' Read/Write methods: }
  371. procedure TDateTimeLabel.SetInterval_FRefreshTimer(newValue: Cardinal);
  372. { Sets the FRefreshTimer subcomponent's Interval property to newValue. }
  373. begin
  374.   FRefreshTimer.Interval := newValue;
  375. end;  { SetInterval_FRefreshTimer }
  376.  
  377. function TDateTimeLabel.GetInterval_FRefreshTimer: Cardinal;
  378. { Returns the Interval property from the FRefreshTimer subcomponent. }
  379. begin
  380.   GetInterval_FRefreshTimer := FRefreshTimer.Interval;
  381. end;  { GetInterval_FRefreshTimer }
  382.  
  383. procedure TDateTimeLabel.Timer_FRefreshTimerHandler(Sender: TObject);
  384. { Handles the FRefreshTimer OnTimer event. }
  385. begin
  386.   { Place your event handler code here. }
  387.   If (NOT (csDesigning in ComponentState)) or (FRunTimerinDesign) then Begin
  388.     ReDrawLabel;
  389.   End;
  390. end;  { Timer_FRefreshTimerHandler }
  391.  
  392. destructor TDateTimeLabel.Destroy;
  393. begin
  394.   { Free member variables: }
  395.   { Free allocated memory and created objects here. }
  396.   FRefreshTimer.Free;
  397.   inherited Destroy;
  398. end;  { Destroy }
  399.  
  400. constructor TDateTimeLabel.Create(AOwner: TComponent);
  401. { Creates an object of type TDateTimeLabel, and initializes properties. }
  402. begin
  403.   inherited Create(AOwner);
  404.   FShadowColor      := clBtnShadow;
  405.   FHighlightColor   := clBtnHighlight;
  406.   FShadowOffset     := 1;
  407.   Transparent       := False;
  408.   { Initialize properties with default values: }
  409.   FAutoFormat       := True;
  410.   FRunTimerinDesign := False;
  411.   FLabelType        := dtDateTime;
  412.   FDateTimeFormat   := 'c';
  413.  
  414.   { Create member variables (that are objects): }
  415.   FRefreshTimer          := TTimer.Create(Self);
  416.   FRefreshTimer.Interval := 90000;   { Every 15 Minutes }
  417.   FRefreshTimer.OnTimer  := Timer_FRefreshTimerHandler;
  418.   { Make sure FRefreshTimer has been created (make sure it's valid). }
  419.   FRefreshTimer.OnTimer  := Timer_FRefreshTimerHandler;
  420.  
  421.  
  422.   ReDrawLabel;
  423.  
  424. end;  { Create }
  425.  
  426. procedure Register;
  427. begin
  428.   RegisterComponents('LDB', [TDateTimeLabel]);
  429. end;  { Register }
  430.  
  431. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement