home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 April A / Pcwk4a98.iso / PROGRAM / DELPHI16 / Calmira / Src / VCL / BARGAUGE.PAS < prev    next >
Pascal/Delphi Source File  |  1997-02-15  |  6KB  |  242 lines

  1. {*********************************************************}
  2. {                                                         }
  3. {    Calmira Visual Component Library 1.0                 }
  4. {    by Li-Hsin Huang,                                    }
  5. {    released into the public domain January 1997         }
  6. {                                                         }
  7. {*********************************************************}
  8.  
  9. unit BarGauge;
  10.  
  11. { TBarGauge }
  12.  
  13. { TBarGauge is a simplified version of Borland's sample TGauge, but is around
  14.   10 times faster at drawing, because it doesn't bother to draw the
  15.   clever "inverse" text effect.  Use it for speed critical applications.
  16. }
  17.  
  18. interface
  19.  
  20. uses
  21.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  22.   Forms, Dialogs, StdCtrls;
  23.  
  24. type
  25.   TBarKind = (bkHorizontal, bkVertical);
  26.  
  27.   TBarGauge = class(TGraphicControl)
  28.   private
  29.     { Private declarations }
  30.     FMinValue: Longint;
  31.     FMaxValue: Longint;
  32.     FCurValue: Longint;
  33.     FShowText: Boolean;
  34.     FBorderStyle: TBorderStyle;
  35.     FForeColor: TColor;
  36.     FBackColor: TColor;
  37.     FCtl3D : Boolean;
  38.     FKind : TBarKind;
  39.     procedure SetShowText(Value: Boolean);
  40.     procedure SetBorderStyle(Value: TBorderStyle);
  41.     procedure SetForeColor(Value: TColor);
  42.     procedure SetBackColor(Value: TColor);
  43.     procedure SetMinValue(Value: Longint);
  44.     procedure SetMaxValue(Value: Longint);
  45.     procedure SetProgress(Value: Longint);
  46.     procedure SetCtl3D(Value: Boolean);
  47.     procedure SetKind(Value: TBarKind);
  48.     function GetPercentDone: Integer;
  49.   protected
  50.     { Protected declarations }
  51.     procedure Paint; override;
  52.   public
  53.     { Public declarations }
  54.     constructor Create(AOwner: TComponent); override;
  55.     procedure AddProgress(Value: Longint);
  56.     property PercentDone: Integer read GetPercentDone;
  57.   published
  58.     { Published declarations }
  59.     property Align;
  60.     property Ctl3D : Boolean read FCtl3D write SetCtl3D default True;
  61.     property Enabled;
  62.     property ShowText: Boolean read FShowText write SetShowText default True;
  63.     property Font;
  64.     property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle default bsSingle;
  65.     property ForeColor: TColor read FForeColor write SetForeColor default clBlack;
  66.     property BackColor: TColor read FBackColor write SetBackColor default clWhite;
  67.     property Kind : TBarKind read FKind write SetKind default bkHorizontal;
  68.     property MinValue: Longint read FMinValue write SetMinValue default 0;
  69.     property MaxValue: Longint read FMaxValue write SetMaxValue default 100;
  70.     property ParentFont;
  71.     property ParentShowHint;
  72.     property Progress: Longint read FCurValue write SetProgress;
  73.     property ShowHint;
  74.     property Visible;
  75.   end;
  76.  
  77. procedure Register;
  78.  
  79. implementation
  80.  
  81. uses ExtCtrls;
  82.  
  83. constructor TBarGauge.Create(AOwner: TComponent);
  84. begin
  85.   inherited Create(AOwner);
  86.   ControlStyle := ControlStyle + [csFramed, csOpaque];
  87.   { default values }
  88.   FMinValue := 0;
  89.   FMaxValue := 100;
  90.   FCurValue := 0;
  91.   FShowText := True;
  92.   FBorderStyle := bsSingle;
  93.   FForeColor := clBlack;
  94.   FBackColor := clWhite;
  95.   FCtl3D := True;
  96.   Width := 100;
  97.   Height := 100;
  98. end;
  99.  
  100.  
  101. function TBarGauge.GetPercentDone: Integer;
  102. begin
  103.   if (FMaxValue = FMinValue) or (FCurValue = FMinValue) then Result := 0
  104.   else Result := Trunc((FCurValue - FMinValue) / (FMaxValue - FMinValue) * 100);
  105. end;
  106.  
  107.  
  108. procedure TBarGauge.SetShowText(Value: Boolean);
  109. begin
  110.   if Value <> FShowText then begin
  111.     FShowText := Value;
  112.     Repaint;
  113.   end;
  114. end;
  115.  
  116. procedure TBarGauge.SetBorderStyle(Value: TBorderStyle);
  117. begin
  118.   if Value <> FBorderStyle then begin
  119.     FBorderStyle := Value;
  120.     Repaint;
  121.   end;
  122. end;
  123.  
  124. procedure TBarGauge.SetForeColor(Value: TColor);
  125. begin
  126.   if Value <> FForeColor then begin
  127.     FForeColor := Value;
  128.     Repaint;
  129.   end;
  130. end;
  131.  
  132. procedure TBarGauge.SetBackColor(Value: TColor);
  133. begin
  134.   if Value <> FBackColor then begin
  135.     FBackColor := Value;
  136.     Repaint;
  137.   end;
  138. end;
  139.  
  140. procedure TBarGauge.SetMinValue(Value: Longint);
  141. begin
  142.   if Value <> FMinValue then begin
  143.     FMinValue := Value;
  144.     Repaint;
  145.   end;
  146. end;
  147.  
  148. procedure TBarGauge.SetMaxValue(Value: Longint);
  149. begin
  150.   if Value <> FMaxValue then begin
  151.     FMaxValue := Value;
  152.     Repaint;
  153.   end;
  154. end;
  155.  
  156.  
  157. procedure TBarGauge.SetProgress(Value: Longint);
  158. var PrevPercent : Integer;
  159. begin
  160.   if (FCurValue <> Value) and (Value >= FMinValue) and (Value <= FMaxValue) then begin
  161.     PrevPercent := GetPercentDone;
  162.     FCurValue := Value;
  163.     if GetPercentDone <> PrevPercent then Repaint;
  164.   end;
  165. end;
  166.  
  167.  
  168. procedure TBarGauge.SetCtl3D(Value: Boolean);
  169. begin
  170.   if Value <> FCtl3D then begin
  171.     FCtl3D := Value;
  172.     Repaint;
  173.   end;
  174. end;
  175.  
  176.  
  177. procedure TBarGauge.SetKind(Value: TBarKind);
  178. begin
  179.   if Value <> FKind then begin
  180.     FKind := Value;
  181.     Repaint;
  182.   end;
  183. end;
  184.  
  185.  
  186. procedure TBarGauge.AddProgress(Value: Longint);
  187. begin
  188.   Progress := FCurValue + Value;
  189. end;
  190.  
  191.  
  192.  
  193. procedure TBarGauge.Paint;
  194. var
  195.   r: TRect;
  196.   x, y: Integer;
  197.   s: string[4];
  198. begin
  199.   r := Rect(0, 0, Width, Height);
  200.   with Canvas do begin
  201.  
  202.     if BorderStyle = bsSingle then begin
  203.       if Ctl3D then Frame3D(Canvas, r, clBtnShadow, clBtnHighlight, 1);
  204.       Frame3D(Canvas, r, clBlack, clBlack, 1);
  205.     end;
  206.  
  207.     if Kind = bkHorizontal then begin
  208.       x := MulDiv(r.Right - r.Left, PercentDone, 100);
  209.       Brush.Color := ForeColor;
  210.       FillRect(Rect(r.Left, r.Top, r.Left + x, r.Bottom));
  211.       Brush.Color := BackColor;
  212.       FillRect(Rect(r.Left + x, r.Top, r.Right, r.Bottom));
  213.     end
  214.     else begin
  215.       y := MulDiv(r.Bottom - r.Top, PercentDone, 100);
  216.       Brush.Color := ForeColor;
  217.       FillRect(Rect(r.Left, r.Bottom - y, r.Right, r.Bottom));
  218.       Brush.Color := BackColor;
  219.       FillRect(Rect(r.Left, r.Top, r.Right, r.Bottom - y));
  220.     end;
  221.  
  222.     if ShowText then begin
  223.       s := Format('%d%%', [PercentDone]);
  224.       Brush.Style := bsClear;
  225.       Font.Assign(Self.Font);
  226.       with r do begin
  227.         x := (Width + 1 - TextWidth(s)) div 2;
  228.         y := (Height + 1 - TextHeight(s)) div 2;
  229.       end;
  230.       TextRect(r, x, y, S);
  231.     end;
  232.   end;
  233. end;
  234.  
  235.  
  236. procedure Register;
  237. begin
  238.   RegisterComponents('Samples', [TBarGauge]);
  239. end;
  240.  
  241. end.
  242.