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

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrProgressBar;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   VrTypes, VrClasses, VrControls, VrSysUtils;
  15. type
  16.   TVrProgressBar = class(TVrGraphicImageControl)
  17.   private
  18.     FBevel: TVrBevel;
  19.     FMin: Integer;
  20.     FMax: Integer;
  21.     FPosition: Integer;
  22.     FPlainColors: Boolean;
  23.     FStartColor: TColor;
  24.     FEndColor: TColor;
  25.     FSmooth: Boolean;
  26.     FOrientation: TVrOrientation;
  27.     FStep: Integer;
  28.     ViewPort: TRect;
  29.     Bitmap: TBitmap;
  30.     ColorUpdate: Boolean;
  31.     function GetPercentDone: Longint;
  32.     procedure SetMin(Value: Integer);
  33.     procedure SetMax(Value: Integer);
  34.     procedure SetPosition(Value: Integer);
  35.     procedure SetStartColor(Value: TColor);
  36.     procedure SetEndColor(Value: TColor);
  37.     procedure SetPlainColors(Value: Boolean);
  38.     procedure SetSmooth(Value: Boolean);
  39.     procedure SetOrientation(Value: TVrOrientation);
  40.     procedure SetBevel(Value: TVrBevel);
  41.     procedure BevelChanged(Sender: TObject);
  42.   protected
  43.     procedure CreateBitmap(const Rect: TRect);
  44.     procedure Paint; override;
  45.     procedure DrawHori;
  46.     procedure DrawVert;
  47.   public
  48.     constructor Create(AOwner: TComponent); override;
  49.     destructor Destroy; override;
  50.     procedure StepIt;
  51.     procedure StepBy(Delta: Integer);
  52.     property PercentDone: Longint read GetPercentDone;
  53.   published
  54.     property Bevel: TVrBevel read FBevel write SetBevel;
  55.     property Max: Integer read FMax write SetMax default 100;
  56.     property Min: Integer read FMin write SetMin default 0;
  57.     property Position: Integer read FPosition write SetPosition default 100;
  58.     property StartColor: TColor read FStartColor write SetStartColor default clNavy;
  59.     property EndColor: TColor read FEndColor write SetEndColor default clAqua;
  60.     property PlainColors: Boolean read FPlainColors write SetPlainColors default false;
  61.     property Smooth: Boolean read FSmooth write SetSmooth default false;
  62.     property Orientation: TVrOrientation read FOrientation write SetOrientation default voHorizontal;
  63.     property Step: Integer read FStep write FStep default 10;
  64.     property Align;
  65. {$IFDEF VER110}
  66.     property Anchors;
  67.     property Constraints;
  68. {$ENDIF}
  69.     property Color default clBlack;
  70.     property DragCursor;
  71. {$IFDEF VER110}
  72.     property DragKind;
  73. {$ENDIF}
  74.     property DragMode;
  75.     property Hint;
  76.     property ParentColor default false;
  77.     property ParentShowHint;
  78.     property PopupMenu;
  79.     property ShowHint;
  80.     property Visible;
  81.     property OnClick;
  82. {$IFDEF VER130}
  83.     property OnContextPopup;
  84. {$ENDIF}    
  85.     property OnDblClick;
  86.     property OnDragDrop;
  87.     property OnDragOver;
  88. {$IFDEF VER110}
  89.     property OnEndDock;
  90. {$ENDIF}
  91.     property OnEndDrag;
  92.     property OnMouseDown;
  93.     property OnMouseMove;
  94.     property OnMouseUp;
  95. {$IFDEF VER110}
  96.      property OnStartDock;
  97. {$ENDIF}
  98.     property OnStartDrag;
  99.   end;
  100. implementation
  101. { TVrProgressBar }
  102. constructor TVrProgressBar.Create(AOwner: TComponent);
  103. begin
  104.   inherited Create(AOwner);
  105.   ControlStyle := ControlStyle + [csOpaque] - [csSetCaption];
  106.   Width := 200;
  107.   Height := 16;
  108.   Color := clBlack;
  109.   ParentColor := false;
  110.   FMin := 0;
  111.   FMax := 100;
  112.   FPosition := 100;
  113.   FStartColor := clNavy;
  114.   FEndColor := clAqua;
  115.   FPlainColors := false;
  116.   FSmooth := false;
  117.   FOrientation := voHorizontal;
  118.   FStep := 10;
  119.   FBevel := TVrBevel.Create;
  120.   with FBevel do
  121.   begin
  122.     InnerStyle := bsNone;
  123.     InnerSpace := 0;
  124.     OuterStyle := bsLowered;
  125.     OuterOutline := osNone;
  126.     OnChange := BevelChanged;
  127.   end;
  128.   Bitmap := TBitmap.Create;
  129. end;
  130. destructor TVrProgressBar.Destroy;
  131. begin
  132.   Bitmap.Free;
  133.   FBevel.Free;
  134.   inherited Destroy;
  135. end;
  136. procedure TVrProgressBar.BevelChanged(Sender: TObject);
  137. begin
  138.   UpdateControlCanvas;
  139. end;
  140. procedure TVrProgressBar.CreateBitmap(const Rect: TRect);
  141. var
  142.   R: TRect;
  143.   NewWidth, NewHeight: Integer;
  144. begin
  145.   NewWidth := WidthOf(Rect);
  146.   NewHeight := HeightOf(Rect);
  147.   if (ColorUpdate) or
  148.      (Bitmap.Width <> NewWidth) or
  149.      (Bitmap.Height <> NewHeight) then
  150.   begin
  151.     Bitmap.Width := NewWidth;
  152.     Bitmap.Height := NewHeight;
  153.     R := Bounds(0, 0, NewWidth, NewHeight);
  154.     if FPlainColors then
  155.     begin
  156.       Bitmap.Canvas.Brush.Color := clHighlight;
  157.       Bitmap.Canvas.FillRect(R);
  158.     end
  159.     else
  160.       case FOrientation of
  161.         voHorizontal:
  162.           DrawGradient(Bitmap.Canvas, R, FStartColor,
  163.             FEndColor, FOrientation, 1);
  164.         voVertical:
  165.           DrawGradient(Bitmap.Canvas, R, FEndColor,
  166.             FStartColor, FOrientation, 1);
  167.       end;
  168.     ColorUpdate := false;
  169.   end;
  170. end;
  171. function TVrProgressBar.GetPercentDone: Longint;
  172. begin
  173.   Result := SolveForY(FPosition - FMin, FMax - FMin);
  174. end;
  175. procedure TVrProgressBar.StepIt;
  176. begin
  177.   Position := Position + FStep;
  178. end;
  179. procedure TVrProgressBar.StepBy(Delta: Integer);
  180. begin
  181.   Position := Position + Delta;
  182. end;
  183. procedure TVrProgressBar.Paint;
  184. begin
  185.   ClearBitmapCanvas;
  186.   ViewPort := ClientRect;
  187.   FBevel.Paint(BitmapCanvas, ViewPort);
  188.   CreateBitmap(ViewPort);
  189.   case Orientation of
  190.     voVertical: DrawVert;
  191.     voHorizontal: DrawHori;
  192.   end;
  193.   inherited Paint;
  194. end;
  195. procedure TVrProgressBar.DrawHori;
  196. var
  197.   R: TRect;
  198.   BarWidth: Integer;
  199.   XLimit: Integer;
  200. begin
  201.   BarWidth := (HeightOf(ViewPort) div 3) * 2;
  202.   R := Bounds(ViewPort.Left + ord(FSmooth), ViewPort.Top,
  203.     BarWidth, ViewPort.Bottom - ViewPort.Top);
  204.   InflateRect(R, -ord(not FSmooth), -1);
  205.   XLimit := SolveForX(PercentDone, WidthOf(ViewPort));
  206.   if FSmooth then
  207.     R.Right := ViewPort.Left + XLimit;
  208.   while R.Left < ViewPort.Left + XLimit do
  209.   begin
  210.     if R.Right >= ViewPort.Right then R.Right := ViewPort.Right - 1;
  211.     BitmapCanvas.CopyRect(R, Bitmap.Canvas,
  212.        Bounds(R.Left - ViewPort.Left, 0, R.Right - R.Left, R.Bottom - R.Top));
  213.     OffsetRect(R, R.Right - R.Left + 2, 0);
  214.   end;
  215. end;
  216. procedure TVrProgressBar.DrawVert;
  217. var
  218.   R: TRect;
  219.   BarHeight: Integer;
  220.   XLimit: Integer;
  221. begin
  222.   BarHeight := (WidthOf(ViewPort) div 3) * 2;
  223.   R := Bounds(ViewPort.Left, ViewPort.Bottom - BarHeight,
  224.     ViewPort.Right - ViewPort.Left, BarHeight);
  225.   InflateRect(R, -1, -ord(not FSmooth));
  226.   XLimit := SolveForX(PercentDone, HeightOf(ViewPort));
  227.   if FSmooth then
  228.     R.Top := ViewPort.Bottom - XLimit;
  229.   while R.Bottom > ViewPort.Bottom - XLimit do
  230.   begin
  231.     if R.Top <= ViewPort.Top then R.Top := ViewPort.Top + 1;
  232.     BitmapCanvas.CopyRect(R, Bitmap.Canvas,
  233.        Bounds(0, R.Top - ViewPort.Top - 1, Bitmap.Width, R.Bottom - R.Top));
  234.     OffsetRect(R, 0, -(R.Bottom - R.Top + 2));
  235.   end;
  236. end;
  237. procedure TVrProgressBar.SetMin(Value: Integer);
  238. begin
  239.   if (FMin <> Value) and (Value < FMax) then
  240.   begin
  241.     FMin := Value;
  242.     if FPosition < FMin then Position := FMin
  243.     else UpdateControlCanvas;
  244.   end;
  245. end;
  246. procedure TVrProgressBar.SetMax(Value: Integer);
  247. begin
  248.   if (FMax <> Value) and (Value > FMin) then
  249.   begin
  250.     FMax := Value;
  251.     if FPosition > FMax then Position := FMax
  252.     else UpdateControlCanvas;
  253.   end;
  254. end;
  255. procedure TVrProgressBar.SetPosition(Value: Integer);
  256. begin
  257.   if Value < FMin then Value := FMin;
  258.   if Value > FMax then Value := FMax;
  259.   if FPosition <> Value then
  260.   begin
  261.     FPosition := Value;
  262.     UpdateControlCanvas;
  263.   end;
  264. end;
  265. procedure TVrProgressBar.SetStartColor(Value: TColor);
  266. begin
  267.   if FStartColor <> Value then
  268.   begin
  269.     FStartColor := Value;
  270.     ColorUpdate := True;
  271.     UpdateControlCanvas;
  272.   end;
  273. end;
  274. procedure TVrProgressBar.SetEndColor(Value: TColor);
  275. begin
  276.   if FEndColor <> Value then
  277.   begin
  278.     FEndColor := Value;
  279.     ColorUpdate := True;
  280.     UpdateControlCanvas;
  281.   end;
  282. end;
  283. procedure TVrProgressBar.SetPlainColors(Value: Boolean);
  284. begin
  285.   if FPlainColors <> Value then
  286.   begin
  287.     FPlainColors := Value;
  288.     ColorUpdate := True;
  289.     UpdateControlCanvas;
  290.   end;
  291. end;
  292. procedure TVrProgressBar.SetSmooth(Value: Boolean);
  293. begin
  294.   if FSmooth <> Value then
  295.   begin
  296.     FSmooth := Value;
  297.     UpdateControlCanvas;
  298.   end;
  299. end;
  300. procedure TVrProgressBar.SetBevel(Value: TVrBevel);
  301. begin
  302.   FBevel.Assign(Value);
  303. end;
  304. procedure TVrProgressBar.SetOrientation(Value: TVrOrientation);
  305. begin
  306.   if FOrientation <> Value then
  307.   begin
  308.     FOrientation := Value;
  309.     ColorUpdate := True;
  310.     if not Loading then
  311.       BoundsRect := Bounds(Left, Top, Height, Width);
  312.     UpdateControlCanvas;
  313.   end;
  314. end;
  315. end.