home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1995 December / PCPRO_1295.ISO / code / prog / listing.txt
Encoding:
Text File  |  1995-10-09  |  6.4 KB  |  255 lines

  1. unit Progress;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, ExtCtrls;
  8.  
  9. type
  10.   TBevelWidth = 1..MaxInt;
  11.   TBorderWidth = 0..MaxInt;
  12.   TPanelBevel = (bvNone, bvLowered, bvRaised);
  13.  
  14.   TProgress = class(TCustomControl)
  15.   private
  16.     { Private declarations }
  17.     FMin, FMax, FPosition: Integer;
  18.     FProgressColor: TColor;
  19.     FBevelInner: TPanelBevel;
  20.     FBevelOuter: TPanelBevel;
  21.     FBevelWidth: TBevelWidth;
  22.     FBorderStyle: TBorderStyle;
  23.     FBorderWidth: TBorderWidth;
  24.     procedure SetBevelInner(Value: TPanelBevel);
  25.     procedure SetBevelOuter(Value: TPanelBevel);
  26.     procedure SetBevelWidth(Value: TBevelWidth);
  27.     procedure SetBorderStyle(Value: TBorderStyle);
  28.     procedure SetBorderWidth(Value: TBorderWidth);
  29.     procedure SetProgressColor (Value: TColor);
  30.     procedure SetMin (Value: Integer);
  31.     procedure SetMax (Value: Integer);
  32.     procedure SetPosition (Value: Integer);
  33.  
  34.   protected
  35.     { Protected declarations }
  36.     procedure Paint; override;
  37.     procedure CreateParams(var Params: TCreateParams); override;
  38.  
  39.   published
  40.     { Published declarations }
  41.     property BevelInner: TPanelBevel read FBevelInner 
  42.     write SetBevelInner default bvNone;
  43.     property BevelOuter: TPanelBevel read FBevelOuter 
  44.     write SetBevelOuter default bvLowered;
  45.     property BevelWidth: TBevelWidth read FBevelWidth 
  46.     write SetBevelWidth default 2;
  47.     property BorderStyle: TBorderStyle read FBorderStyle 
  48.     write SetBorderStyle default bsNone;
  49.     property BorderWidth: TBorderWidth read FBorderWidth 
  50.     write SetBorderWidth default 0;
  51.     property Min: Integer read FMin 
  52.     write SetMin default 0;
  53.     property Max: Integer read FMax 
  54.     write SetMax default 100;
  55.     property Position: Integer read FPosition 
  56.     write SetPosition default 50;
  57.     property ProgressColor: TColor read FProgressColor 
  58.     write SetProgressColor default clNavy;
  59.  
  60.     property Color default clBtnFace;
  61.     property ParentColor default True;
  62.     property ParentShowHint;
  63.     property ShowHint;
  64.     property TabOrder;   
  65.     property TabStop default True;
  66.     property Visible;
  67.  
  68.   public
  69.     { Public declarations }
  70.     constructor Create (AOwner: TComponent); override;
  71.     procedure SetParams (APosition, AMin, AMax: Integer);
  72.  
  73.   end;
  74.  
  75. procedure Register;
  76.  
  77. implementation
  78.  
  79. constructor TProgress.Create (AOwner: TComponent);
  80. begin
  81.     inherited Create (AOwner);
  82.  
  83.     Height := (GetSystemMetrics (sm_CYHScroll) * 4) div 3;
  84.     if Height < 4 then Height := 4;
  85.     Width := Height * 6;
  86.  
  87.     BevelOuter := bvLowered;
  88.     BevelWidth := 2;
  89.     FBorderStyle := bsNone;
  90.     FProgressColor := clNavy;
  91.     FBorderWidth := 0;
  92.     ParentColor := True;
  93.     Color := clBtnFace;
  94.     TabStop := True;
  95.     FMin := 0;
  96.     FMax := 100;
  97.     FPosition := 50;
  98.  end;
  99.  
  100. procedure TProgress.SetBevelInner(Value: TPanelBevel);
  101. begin
  102.   FBevelInner := Value;
  103.   Invalidate;
  104. end;
  105.  
  106. procedure TProgress.SetBevelOuter(Value: TPanelBevel);
  107. begin
  108.   FBevelOuter := Value;
  109.   Invalidate;
  110. end;
  111.  
  112. procedure TProgress.SetBevelWidth(Value: TBevelWidth);
  113. begin
  114.   FBevelWidth := Value;
  115.   Invalidate;
  116. end;
  117.  
  118. procedure TProgress.SetProgressColor (Value: TColor);
  119. begin
  120.   if FProgressColor <> Value then
  121.   begin
  122.       FProgressColor := Value;
  123.       Invalidate;
  124.   end;
  125. end;
  126.  
  127. procedure TProgress.SetBorderStyle(Value: TBorderStyle);
  128. begin
  129.   if FBorderStyle <> Value then
  130.   begin
  131.     FBorderStyle := Value;
  132.     RecreateWnd;
  133.   end;
  134. end;
  135.  
  136. procedure TProgress.SetBorderWidth(Value: TBorderWidth);
  137. begin
  138.   FBorderWidth := Value;
  139.   Invalidate;
  140. end;
  141.  
  142. procedure TProgress.CreateParams(var Params: TCreateParams);
  143. const
  144.     BorderStyles: array[TBorderStyle] of Longint = (0, WS_BORDER);
  145.  
  146. begin
  147.     inherited CreateParams(Params);
  148.  
  149.     with Params do
  150.     begin
  151.         WindowClass.style := WindowClass.style or CS_HREDRAW or CS_VREDRAW;
  152.         Style := Style or BorderStyles [FBorderStyle];
  153.     end;
  154. end;
  155.  
  156. procedure TProgress.Paint;
  157. var
  158.   BoxCount, BoxWidth: Integer;
  159.   BoxRect, Rect: TRect;
  160.   TopColor, BottomColor: TColor;
  161.  
  162.   procedure AdjustColors (Bevel: TPanelBevel);
  163.   begin
  164.      TopColor := clBtnHighlight;
  165.      if Bevel = bvLowered then TopColor := clBtnShadow;
  166.      BottomColor := clBtnShadow;
  167.      if Bevel = bvLowered then BottomColor := clBtnHighlight;
  168.   end;
  169.  
  170. begin
  171.     Rect := GetClientRect;
  172.  
  173.     if BevelOuter <> bvNone then
  174.     begin
  175.         AdjustColors (BevelOuter);
  176.         Frame3D (Canvas, Rect, TopColor, BottomColor, BevelWidth);
  177.     end;
  178.  
  179.     Frame3D (Canvas, Rect, Color, Color, BorderWidth);
  180.  
  181.     if BevelInner <> bvNone then
  182.     begin
  183.         AdjustColors (BevelInner);
  184.         Frame3D (Canvas, Rect, TopColor, BottomColor, BevelWidth);
  185.     end;
  186.  
  187.     with Canvas do
  188.     begin
  189.         Brush.Color := Color;
  190.         FillRect (Rect);
  191.         InflateRect (Rect, -2, -2);
  192.         BoxRect := Rect;
  193.         boxWidth := ((Rect.bottom - Rect.top) * 2) div 3;
  194.         if boxWidth <= 0 then boxWidth := 1;
  195.         BoxCount := (MulDiv (Rect.right - Rect.left, 
  196.         Position - Min, Max - Min) + boxWidth + 1) div (boxWidth + 2);
  197.         SetBkColor (Canvas.Handle, FProgressColor);
  198.  
  199.         while BoxCount > 0 do
  200.         begin
  201.             BoxRect.right := BoxRect.left + boxWidth;
  202.             if BoxRect.right < Rect.right then
  203.             begin
  204.                 ExtTextOut (Canvas.Handle, 0, 0, eto_Opaque, @BoxRect, Nil, 0, Nil);
  205.             end;
  206.  
  207.             BoxRect.left := BoxRect.right + 2;
  208.             Dec (BoxCount);
  209.         end;
  210.     end;
  211. end;
  212.  
  213. procedure TProgress.SetPosition (Value: Integer);
  214. begin
  215.     SetParams (Value, FMin, FMax);
  216. end;
  217.  
  218. procedure TProgress.SetMin (Value: Integer);
  219. begin
  220.     SetParams (FPosition, Value, FMax);
  221. end;
  222.  
  223. procedure TProgress.SetMax (Value: Integer);
  224. begin
  225.     SetParams (FPosition, FMin, Value);
  226. end;
  227.  
  228. procedure TProgress.SetParams (APosition, AMin, AMax: Integer);
  229. begin
  230.     if AMax < AMin then raise EInvalidOperation.Create('Max less than Min.');
  231.  
  232.     if APosition < AMin then APosition := AMin;
  233.     if APosition > AMax then APosition := AMax;
  234.  
  235.     if (FMin <> AMin) or (FMax <> AMax) then
  236.     begin
  237.         FMin := AMin;
  238.         FMax := AMax;
  239.         Invalidate;
  240.     end;
  241.  
  242.     if FPosition <> APosition then
  243.     begin
  244.         FPosition := APosition;
  245.         Invalidate;
  246.     end;
  247. end;
  248.  
  249. procedure Register;
  250. begin
  251.   RegisterComponents('Pilgrim''s Progress', [TProgress]);
  252. end;
  253.  
  254. end.
  255.