home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 December / Chip_2002-12_cd1.bin / zkuste / delphi / kompon / d5 / DDLEDBAR.ZIP / DDLedBar / DDLedBar.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2002-09-22  |  7.1 KB  |  244 lines

  1. //------------------------------------------------------------------------------
  2. //
  3. //    TDDLedBar v1.0  ( FREEWARE Component)
  4. //    Copyright 2002 Dobrin Dobrev, e-mail:dobrito@email.com
  5. //    http://elektronika.vega.bg
  6. //
  7. //------------------------------------------------------------------------------
  8. //
  9. // This is a simple 3 color LED bar component for Delphi
  10. //
  11. // ColorLow, ColorMid, ColorHi - Used colors to render bars
  12. // Orientation - Orientation of the component
  13. // BarSize - Size of each bar in LED
  14. // Position - Current position
  15. // LowToMidPos, MidToHiPos - Break between low, middle and high color
  16. //
  17. //------------------------------------------------------------------------------
  18.  
  19. unit DDLedBar;
  20.  
  21. interface
  22.  
  23. uses
  24.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  25.   ComCtrls, ExtCtrls;
  26.  
  27. type
  28.   TDDLedBar = class(TGraphicControl)
  29.   private
  30.     FColorLow : TColor;
  31.     FColorMid : TColor;
  32.     FColorHi : TColor;
  33.     FOrientation : TTrackBarOrientation;
  34.     FMaxValue : Longint;
  35.     FBackColor : TColor;
  36.     FBarSize : Word;
  37. //    FBarOrientation : TTrackBarOrientation;
  38.     FPosition : Longint;
  39.     FLowToMidPos : Byte;
  40.     FMidToHiPos : Byte;
  41.     procedure SetColorLow(const Value: TColor);
  42.     procedure SetColorMid(const Value: TColor);
  43.     procedure SetColorHi(const Value: TColor);
  44.     procedure SetOrientation(const Value: TTrackBarOrientation);
  45.     procedure SetMaxValue(const Value: Longint);
  46.     procedure SetBackColor(const Value: TColor);
  47.     procedure SetBarSize(const Value: Word);
  48. //    procedure SetBarOrientation(const Value: TTrackBarOrientation);
  49.     procedure SetPosition(const Value: Longint);
  50.     procedure SetLowToMidPos(const Value: Byte);
  51.     procedure SetMidToHiPos(const Value: Byte);
  52.     { Private declarations }
  53.   protected
  54.     { Protected declarations }
  55.     BarCount: Longint;
  56.     procedure Paint; override;
  57.     procedure CalculateBarCount;
  58.   public
  59.     { Public declarations }
  60.     constructor Create(Owner: TComponent); override;
  61.     destructor Destroy; override;
  62.   published
  63.     { Published declarations }
  64.     property ColorLow: TColor read FColorLow write SetColorLow;
  65.     property ColorMid: TColor read FColorMid write SetColorMid;
  66.     property ColorHi: TColor read FColorHi write SetColorHi;
  67.     property Orientation: TTrackBarOrientation read FOrientation write SetOrientation default trVertical;
  68.     property MaxValue: Longint read FMaxValue write SetMaxValue;
  69.     property BackColor: TColor read FBackColor write SetBackColor;
  70.     property BarSize: Word read FBarSize write SetBarSize;
  71. //    property BarOrientation: TTrackBarOrientation read FBarOrientation write SetBarOrientation default trVertical;
  72.     property Position: Longint read FPosition write SetPosition;
  73.     property LowToMidPos: Byte read FLowToMidPos write SetLowToMidPos;
  74.     property MidToHiPos: Byte read FMidToHiPos write SetMidToHiPos;
  75.     property Constraints;
  76.     property ParentShowHint;
  77.     property PopupMenu;
  78.     property ShowHint;
  79.     property Visible;
  80.     property Align;
  81.     property Anchors;
  82.     property OnClick;
  83.     property OnMouseDown;
  84.     property OnMouseMove;
  85.     property OnMouseUp;
  86.   end;
  87.  
  88. procedure Register;
  89.  
  90. implementation
  91.  
  92. procedure Register;
  93. begin
  94.   RegisterComponents('Dobrin Dobrev', [TDDLedBar]);
  95. end;
  96.  
  97. { DDLedBar }
  98.  
  99. constructor TDDLedBar.Create(Owner: TComponent);
  100. begin
  101.   inherited Create(Owner);
  102.   Width := 25;
  103.   Height := 147;
  104.   FBackColor := clBlack;
  105.   FColorLow := clLime;
  106.   FColorMid := clYellow;
  107.   FColorHi := clRed;
  108.   FOrientation := trVertical;
  109.   FMaxValue := 100 ;
  110.   FBarSize := 5;
  111.   RGB(255,0,0);
  112. //  FBarOrientation := trVertical;
  113.   FPosition := 0;
  114.   FLowToMidPos := 32;
  115.   FMidToHiPos := 16;
  116.   CalculateBarCount;  
  117. end;
  118.  
  119. destructor TDDLedBar.Destroy;
  120. begin
  121.   inherited Destroy;
  122. end;
  123.  
  124. // Draw DDLedBar
  125. procedure TDDLedBar.Paint;
  126. var Bar: TBitmap;
  127.     I: Integer;
  128. begin
  129.   Bar := TBitmap.Create;
  130.   Bar.Width := Width;
  131.   Bar.Height := Height;
  132.   Bar.Canvas.Brush.Color := FBackColor;
  133.   Bar.Canvas.FillRect(Rect(0, 0, Width, Height));
  134.   CalculateBarCount;
  135.   for I := 1 to BarCount do
  136.     begin
  137.       if (((I*100) div BarCount) < (100-FLowToMidPos)) then Bar.Canvas.Brush.Color := FColorLow;
  138.       if (((I*100) div BarCount) >= (100-FLowToMidPos)) and ((((I*100) div BarCount)) < (100-FMidToHiPos)) then Bar.Canvas.Brush.Color := FColorMid;
  139.       if (((I*100) div BarCount) >= (100-FMidToHiPos)) then Bar.Canvas.Brush.Color := FColorHi;
  140.       if (((FMaxValue > FPosition) and (((I*100) div BarCount) > ((FPosition*100) div MaxValue)))
  141.       or ((((I*100) div BarCount) > FPosition))) or (FPosition = 0) then
  142.         Bar.Canvas.Brush.Color := RGB(GetRValue(Bar.Canvas.Brush.Color) div 2,GetGValue(Bar.Canvas.Brush.Color)div 2,GetGValue(Bar.Canvas.Brush.Color)div 2);
  143.       if FOrientation = trVertical then
  144.         Bar.Canvas.FillRect(Rect(2, ((Height-2)-FBarSize*I+2), Width-2, ((Height-2)-FBarSize*I+FBarSize)))
  145.       else
  146.         Bar.canvas.FillRect(Rect(2+FBarSize*(I-1), 2, (2+FBarSize*(I-1))+FBarSize-2, Height-2));
  147.     end;
  148.    Canvas.CopyRect(Rect(0,0,Width,Height),Bar.Canvas,Rect(0,0,Width,Height));
  149.    Bar.Free;
  150. end;
  151.  
  152. procedure TDDLedBar.CalculateBarCount;
  153. begin
  154.   if (FOrientation = trVertical) then
  155.     BarCount := (Height - 2) div (FBarSize)
  156.   else
  157.     BarCount := (Width - 2) div (FBarSize);
  158. end;
  159.  
  160. procedure TDDLedBar.SetColorLow(const Value: TColor);
  161. begin
  162.   FColorLow := Value;
  163.   Paint
  164. end;
  165.  
  166. procedure TDDLedBar.SetColorMid(const Value: TColor);
  167. begin
  168.   FColorMid := Value;
  169.   Paint
  170. end;
  171.  
  172. procedure TDDLedBar.SetColorHi(const Value: TColor);
  173. begin
  174.   FColorHi := Value;
  175.   Paint
  176. end;
  177.  
  178. procedure TDDLedBar.SetBackColor(const Value: TColor);
  179. begin
  180.   FBackColor := Value;
  181.   Paint;
  182. end;
  183.  
  184. procedure TDDLedBar.SetOrientation(const Value: TTrackBarOrientation);
  185. begin
  186.   if FOrientation <> Value then
  187.   begin
  188.   FOrientation := Value;
  189.    //Change Orientation
  190.     if ComponentState * [csLoading, csUpdating] = [] then
  191.       SetBounds(Left, Top, Height, Width);
  192.   CalculateBarCount;    
  193.   Paint;
  194.   end;
  195. end;
  196.  
  197. procedure TDDLedBar.SetMaxValue(const Value: Longint);
  198. begin
  199.   if FMaxValue <> Value then
  200.     begin
  201.       FMaxValue := Value;
  202.       Paint;
  203.     end;
  204. end;
  205.  
  206. procedure TDDLedBar.SetBarSize(const Value: Word);
  207. begin
  208.   if ((FOrientation = trVertical) and (Value<(Height-4)) and (Value >2))
  209.     or ((FOrientation = trHorizontal) and (Value<(Width-4)) and (Value>2)) then
  210.       begin
  211.         FBarSize := Value;
  212.         CalculateBarCount;
  213.         Paint;
  214.       end;
  215. end;
  216.  
  217. //procedure SetBarOrientation(const Value: TTrackBarOrientation);
  218.  
  219. procedure TDDLedBar.SetPosition(const Value: Longint);
  220. begin
  221.   FPosition := Value;
  222.   Paint;
  223. end;
  224.  
  225. procedure TDDLedBar.SetLowToMidPos(const Value: Byte);
  226. begin
  227.   if (Value < 100) and (Value > FMidToHiPos) then
  228.     begin
  229.       FLowToMidPos := Value;
  230.       Paint;
  231.     end;
  232. end;
  233.  
  234. procedure TDDLedBar.SetMidToHiPos(const Value: Byte);
  235. begin
  236.   if (Value <= 100) and (Value < FLowToMidPos) then
  237.     begin
  238.       FMidToHiPos := Value;
  239.       Paint;
  240.     end;  
  241. end;
  242.         
  243. end.
  244.