VrAnalog.pas
上传用户:hbszzs
上传日期:2008-08-20
资源大小:628k
文件大小:10k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrAnalog;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Forms, Graphics, Controls,
  14.   VrTypes, VrClasses, VrControls, VrSysUtils, VrThreads;
  15. type
  16.   TVrAnalogClock = class(TVrGraphicImageControl)
  17.   private
  18.     FActive: Boolean;
  19.     FSecondsIndicator: Boolean;
  20.     FHourMarks: Boolean;
  21.     FHours, FMinutes, FSeconds: Word;
  22.     FGlyph: TBitmap;
  23.     FHandsColor: TColor;
  24.     FSecHandColor: TColor;
  25.     FTickColor: TColor;
  26.     FTickWidth: Integer;
  27.     FTickOutline: TColor;
  28.     FThreaded: Boolean;
  29.     FAlarmTime: TDateTime;
  30.     FEnableAlarm: Boolean;
  31.     FOnAlarm: TNotifyEvent;
  32.     FOnHoursChanged: TVrHoursChangeEvent;
  33.     FOnMinutesChanged: TVrMinutesChangeEvent;
  34.     FOnSecondsChanged: TVrSecondsChangeEvent;
  35.     FTimer: TVrTimer;
  36.     procedure SetActive(Value: Boolean);
  37.     procedure SetSecondsIndicator(Value: Boolean);
  38.     procedure SetHourMarks(Value: Boolean);
  39.     procedure SetGlyph(Value: TBitmap);
  40.     procedure SetHandsColor(Value: TColor);
  41.     procedure SetSecHandColor(Value: TColor);
  42.     procedure SetTickColor(Value: TColor);
  43.     procedure SetTickWidth(Value: Integer);
  44.     procedure SetTickOutline(Value: TColor);
  45.     procedure SetThreaded(Value: Boolean);
  46.     procedure DrawHand(XCenter, YCenter, Radius, BackRadius: Integer; Angle: Double);
  47.     procedure OnTimerEvent(Sender: TObject);
  48.   protected
  49.     procedure Paint; override;
  50.     procedure HoursChanged; virtual;
  51.     procedure MinutesChanged; virtual;
  52.     procedure SecondsChanged; virtual;
  53.     procedure DoAlarm;
  54.   public
  55.     constructor Create (AOwner: TComponent); override;
  56.     destructor Destroy; override;
  57.     property Hours: Word read FHours;
  58.     property Minutes: Word read FMinutes;
  59.     property Seconds: Word read FSeconds;
  60.     property AlarmTime: TDateTime read FAlarmTime write FAlarmTime;
  61.   published
  62.     property Threaded: Boolean read FThreaded write SetThreaded default True;
  63.     property Active: Boolean read FActive write SetActive default false;
  64.     property SecondsIndicator: Boolean read FSecondsIndicator write SetSecondsIndicator default True;
  65.     property HourMarks: Boolean read FHourMarks write SetHourMarks default True;
  66.     property Glyph: TBitmap read FGlyph write SetGlyph;
  67.     property HandsColor: TColor read FHandsColor write SetHandsColor default clLime;
  68.     property SecHandColor: TColor read FSecHandColor write SetSecHandColor default clLime;
  69.     property TickColor: TColor read FTickColor write SetTickColor default clLime;
  70.     property TickWidth: Integer read FTickWidth write SetTickWidth default 2;
  71.     property TickOutline: TColor read FTickOutline write SetTickOutline default clGreen;
  72.     property EnableAlarm: Boolean read FEnableAlarm write FEnableAlarm default false;
  73.     property OnAlarm: TNotifyEvent read FOnAlarm write FOnAlarm;
  74.     property OnHoursChanged: TVrHoursChangeEvent read FOnHoursChanged write FOnHoursChanged;
  75.     property OnMinutesChanged: TVrMinutesChangeEvent read FOnMinutesChanged write FOnMinutesChanged;
  76.     property OnSecondsChanged: TVrSecondsChangeEvent read FOnSecondsChanged write FOnSecondsChanged;
  77.     property Transparent default false;
  78.     property Align;
  79. {$IFDEF VER110}
  80.     property Anchors;
  81.     property Constraints;
  82. {$ENDIF}
  83.     property Color default clBlack;
  84.     property DragCursor;
  85. {$IFDEF VER110}
  86.     property DragKind;
  87. {$ENDIF}
  88.     property DragMode;
  89.     property Hint;
  90.     property ParentColor default false;
  91.     property ParentShowHint;
  92.     property PopupMenu;
  93.     property ShowHint;
  94.     property Visible;
  95.     property OnClick;
  96. {$IFDEF VER130}
  97.     property OnContextPopup;
  98. {$ENDIF}    
  99.     property OnDblClick;
  100.     property OnDragDrop;
  101.     property OnDragOver;
  102. {$IFDEF VER110}
  103.     property OnEndDock;
  104. {$ENDIF}
  105.     property OnEndDrag;
  106.     property OnMouseDown;
  107.     property OnMouseMove;
  108.     property OnMouseUp;
  109. {$IFDEF VER110}
  110.      property OnStartDock;
  111. {$ENDIF}
  112.     property OnStartDrag;
  113.   end;
  114. implementation
  115. {TVrAnalogClock}
  116. constructor TVrAnalogClock.Create(AOwner: TComponent);
  117. begin
  118.   inherited Create(AOwner);
  119.   ControlStyle := ControlStyle + [csOpaque] - [csSetCaption];
  120.   Width := 90;
  121.   Height := 90;
  122.   ParentColor := false;
  123.   Color := clBlack;
  124.   Transparent := false;
  125.   FSecondsIndicator := True;
  126.   FHourMarks := True;
  127.   FActive := false;
  128.   FHandsColor := clLime;
  129.   FSecHandColor := clLime;
  130.   FTickColor := clLime;
  131.   FTickWidth := 2;
  132.   FTickOutline := clGreen;
  133.   FAlarmTime := 0;
  134.   FEnableAlarm := false;
  135.   FGlyph := TBitmap.Create;
  136.   FThreaded := True;
  137.   FTimer := TVrTimer.Create(Self);
  138.   FTimer.Enabled := false;
  139.   FTimer.OnTimer := OnTimerEvent;
  140.   OnTimerEvent(self);
  141. end;
  142. destructor TVrAnalogClock.Destroy;
  143. begin
  144.   FTimer.Free;
  145.   FGlyph.Free;
  146.   inherited Destroy;
  147. end;
  148. procedure TVrAnalogClock.SetActive(Value: Boolean);
  149. begin
  150.   if FActive <> Value then
  151.   begin
  152.     FActive := Value;
  153.     if not Designing then
  154.       FTimer.Enabled := Value;
  155.   end;
  156. end;
  157. procedure TVrAnalogClock.SetSecondsIndicator(Value: Boolean);
  158. begin
  159.   if FSecondsIndicator <> Value then
  160.   begin
  161.     FSecondsIndicator := Value;
  162.     UpdateControlCanvas;
  163.   end;
  164. end;
  165. procedure TVrAnalogClock.SetHourMarks(Value: Boolean);
  166. begin
  167.   if FHourMarks <> Value then
  168.   begin
  169.     FHourMarks := Value;
  170.     UpdateControlCanvas;
  171.   end;
  172. end;
  173. procedure TVrAnalogClock.SetHandsColor(Value: TColor);
  174. begin
  175.   if FHandsColor <> Value then
  176.   begin
  177.     FHandsColor := Value;
  178.     UpdateControlCanvas;
  179.   end;
  180. end;
  181. procedure TVrAnalogClock.SetSecHandColor(Value: TColor);
  182. begin
  183.   if FSecHandColor <> Value then
  184.   begin
  185.     FSecHandColor := Value;
  186.     UpdateControlCanvas;
  187.   end;
  188. end;
  189. procedure TVrAnalogClock.SetTickColor(Value: TColor);
  190. begin
  191.   if FTickColor <> Value then
  192.   begin
  193.     FTickColor := Value;
  194.     UpdateControlCanvas;
  195.   end;
  196. end;
  197. procedure TVrAnalogClock.SetTickWidth(Value: Integer);
  198. begin
  199.   if (FTickWidth <> Value) and (Value > 0) then
  200.   begin
  201.     FTickWidth := Value;
  202.     UpdateControlCanvas;
  203.   end;
  204. end;
  205. procedure TVrAnalogClock.SetTickOutline(Value: TColor);
  206. begin
  207.   if FTickOutline <> Value then
  208.   begin
  209.     FTickOutline := Value;
  210.     UpdateControlCanvas;
  211.   end;
  212. end;
  213. procedure TVrAnalogClock.SetGlyph(Value: TBitmap);
  214. begin
  215.   FGlyph.Assign(Value);
  216.   UpdateControlCanvas;
  217. end;
  218. procedure TVrAnalogClock.SetThreaded(Value: Boolean);
  219. begin
  220.   if FThreaded <> Value then
  221.   begin
  222.     FThreaded := Value;
  223.     if Value then FTimer.TimerType := ttThread
  224.     else FTimer.TimerType := ttSystem;
  225.   end;
  226. end;
  227. procedure TVrAnalogClock.HoursChanged;
  228. begin
  229.   if Assigned(FOnHoursChanged) then
  230.     FOnHoursChanged(Self, FHours);
  231. end;
  232. procedure TVrAnalogClock.MinutesChanged;
  233. begin
  234.   if Assigned(FOnMinutesChanged) then
  235.     FOnMinutesChanged(Self, FMinutes);
  236. end;
  237. procedure TVrAnalogClock.SecondsChanged;
  238. begin
  239.   if Assigned(FOnSecondsChanged) then
  240.     FOnSecondsChanged(Self, FSeconds);
  241. end;
  242. procedure TVrAnalogClock.DoAlarm;
  243. begin
  244.   if Assigned(FOnAlarm) then
  245.     FOnAlarm(Self);
  246. end;
  247. procedure TVrAnalogClock.OnTimerEvent(Sender: TObject);
  248. var
  249.   S100: Word;
  250.   Ho, Mo, So, Ha, Ma, Sa: Word;
  251.   Alarm, SecsChanged: Boolean;
  252. begin
  253.   //store old values
  254.   Ho := FHours;
  255.   Mo := FMinutes;
  256.   So := FSeconds;
  257.   DecodeTime(Time, FHours, FMinutes, FSeconds, S100);
  258.   DecodeTime(AlarmTime, Ha, Ma, Sa, S100);
  259.   Alarm := (EnableAlarm) and (Trunc(AlarmTime) = Date) and
  260.     (FHours = Ha) and (FMinutes = Ma) and (FSeconds = Sa);
  261.   SecsChanged := (FSecondsIndicator) and (So <> FSeconds);
  262.   if (SecsChanged) or (FMinutes <> Mo) then
  263.     UpdateControlCanvas;
  264.   if (FHours <> Ho) then HoursChanged;
  265.   if (FMinutes <> Mo) then MinutesChanged;
  266.   if (FSeconds <> So) then SecondsChanged;
  267.   if Alarm then DoAlarm;
  268. end;
  269. procedure TVrAnalogClock.Paint;
  270. var
  271.   Angle: Double;
  272.   I, X, Y, Radius: Integer;
  273.   XCenter, YCenter: Integer;
  274.   R: TRect;
  275. begin
  276.   ClearBitmapCanvas;
  277.   if not FGlyph.Empty then
  278.   begin
  279.     if Transparent then BitmapCanvas.Brush.Style := bsClear
  280.     else BitmapCanvas.Brush.Style := bsSolid;
  281.     BitmapCanvas.BrushCopy(ClientRect, FGlyph,
  282.       BitmapRect(FGlyph), Self.Color);
  283.   end;
  284.   XCenter := Width div 2;
  285.   YCenter := Height div 2;
  286.   if XCenter > YCenter then
  287.     Radius := YCenter - 10
  288.   else Radius := XCenter - 10;
  289.   with BitmapCanvas do
  290.   begin
  291.     Pen.Color := TickOutline;
  292.     Pen.Style := psSolid;
  293.     Brush.Color := TickColor;
  294.     Brush.Style := bsSolid;
  295.   end;
  296.   if FHourMarks then
  297.     for I := 0 to 11 do
  298.     begin
  299.       Angle := 2 * Pi * (I + 9) / 12;
  300.       X := XCenter - Round (Radius * Cos(Angle));
  301.       Y := YCenter - Round (Radius * Sin(Angle));
  302.       with BitmapCanvas do
  303.       begin
  304.         R := Rect(X - TickWidth, Y - TickWidth, X + TickWidth, Y + TickWidth);
  305.         Ellipse(R.Left, R.Top, R.Right, R.Bottom);
  306.       end;
  307.     end;
  308.   BitmapCanvas.Pen.Color := FHandsColor;
  309.   Angle := 2 * Pi * (FMinutes + 45) / 60;
  310.   DrawHand (XCenter, YCenter,
  311.     Radius * 90 div 100, 0, Angle);
  312.   Angle := 2 * Pi * (FHours + 9 + FMinutes / 60) / 12;
  313.   DrawHand (XCenter, YCenter,
  314.     Radius * 70 div 100, 0, Angle);
  315.   if FSecondsIndicator then
  316.   begin
  317.     BitmapCanvas.Pen.Color := FSecHandColor;
  318.     Angle := 2 * Pi * (FSeconds + 45) / 60;
  319.     DrawHand (XCenter, YCenter, Radius,
  320.       Radius * 30 div 100, Angle);
  321.   end;
  322.   inherited Paint;
  323. end;
  324. procedure TVrAnalogClock.DrawHand (XCenter, YCenter,
  325.   Radius, BackRadius: Integer; Angle: Double);
  326. begin
  327.   BitmapCanvas.MoveTo(
  328.     XCenter - Round (BackRadius * Cos (Angle)),
  329.     YCenter - Round (BackRadius * Sin (Angle)));
  330.   BitmapCanvas.LineTo(
  331.     XCenter + Round (Radius * Cos (Angle)),
  332.     YCenter + Round (Radius * Sin (Angle)));
  333. end;
  334. end.