djjd47130

Progress Bar Sample

Aug 12th, 2015
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.92 KB | None | 0 0
  1. unit ProgressBar;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, System.Classes, System.SysUtils,
  7.   Vcl.Controls, Vcl.Graphics;
  8.  
  9. type
  10.   TCustomProgresBar = class;
  11.  
  12.   TProgressBarOrientation = (poLeftToRight, poRightToLeft, poBottomToTop, poTopToBottom);
  13.  
  14.   TProgressBarDisplay = (pdNone, pdPercent, pdCurrent, pdCurrentTotal, pdCustom);
  15.  
  16.   TCustomProgresBar = class(TCustomControl)
  17.   private
  18.     FMax: Integer;
  19.     FMin: Integer;
  20.     FPosition: Integer;
  21.     FOrientation: TProgressBarOrientation;
  22.     FForeColor: TColor;
  23.     FBackColor: TColor;
  24.     FDisplay: TProgressBarDisplay;
  25.     procedure SetMax(const Value: Integer);
  26.     procedure SetMin(const Value: Integer);
  27.     procedure SetPosition(const Value: Integer);
  28.     procedure SetOrientation(const Value: TProgressBarOrientation);
  29.     procedure SetBackColor(const Value: TColor);
  30.     procedure SetForeColor(const Value: TColor);
  31.     procedure SetDisplay(const Value: TProgressBarDisplay);
  32.   protected
  33.     procedure Paint; override;
  34.   public
  35.     constructor Create(AOwner: TComponent); override;
  36.     destructor Destroy; override;
  37.   published
  38.     property Min: Integer read FMin write SetMin;
  39.     property Max: Integer read FMax write SetMax;
  40.     property Position: Integer read FPosition write SetPosition;
  41.     property Orientation: TProgressBarOrientation read FOrientation write SetOrientation;
  42.     property Display: TProgressBarDisplay read FDisplay write SetDisplay;
  43.     property BackColor: TColor read FBackColor write SetBackColor;
  44.     property ForeColor: TColor read FForeColor write SetForeColor;
  45.  
  46.     property Align;
  47.     property Anchors;
  48.     property Caption;
  49.     property Font;
  50.   end;
  51.  
  52.  
  53. implementation
  54.  
  55. { TCustomProgresBar }
  56.  
  57. constructor TCustomProgresBar.Create(AOwner: TComponent);
  58. begin
  59.   inherited;
  60.   Width:= 200;
  61.   Height:= 25;
  62.   FMin:= 0;
  63.   FMax:= 100;
  64.   FPosition:= 0;
  65.   FBackColor:= clGray;
  66.   FForeColor:= clNavy;
  67.   Font.Color:= clWhite;
  68.   Font.Style:= [fsBold];
  69. end;
  70.  
  71. destructor TCustomProgresBar.Destroy;
  72. begin
  73.  
  74.   inherited;
  75. end;
  76.  
  77. procedure TCustomProgresBar.SetBackColor(const Value: TColor);
  78. begin
  79.   FBackColor := Value;
  80.   Invalidate;
  81. end;
  82.  
  83. procedure TCustomProgresBar.SetDisplay(const Value: TProgressBarDisplay);
  84. begin
  85.   FDisplay := Value;
  86.   Invalidate;
  87. end;
  88.  
  89. procedure TCustomProgresBar.SetForeColor(const Value: TColor);
  90. begin
  91.   FForeColor := Value;
  92.   Invalidate;
  93. end;
  94.  
  95. procedure TCustomProgresBar.SetMax(const Value: Integer);
  96. begin
  97.   FMax := Value;
  98.   Invalidate;
  99. end;
  100.  
  101. procedure TCustomProgresBar.SetMin(const Value: Integer);
  102. begin
  103.   FMin := Value;
  104.   Invalidate;
  105. end;
  106.  
  107. procedure TCustomProgresBar.SetOrientation(const Value: TProgressBarOrientation);
  108. begin
  109.   FOrientation := Value;
  110.   Invalidate;
  111. end;
  112.  
  113. procedure TCustomProgresBar.SetPosition(const Value: Integer);
  114. begin
  115.   FPosition := Value;
  116.   if FPosition > FMax then FPosition:= FMax;
  117.   if FPosition < FMin then FPosition:= FMin;
  118.   Invalidate;
  119. end;
  120.  
  121. procedure TCustomProgresBar.Paint;
  122. var
  123.   R: TRect;
  124.   D: Single;
  125.   TW, TH: Integer;
  126.   S: String;
  127. begin
  128.   //Calculation
  129.   R:= Canvas.ClipRect;
  130.   D:= FPosition / FMax;
  131.  
  132.   //Background
  133.   Canvas.Brush.Style:= bsSolid;
  134.   Canvas.Pen.Style:= psClear;
  135.   Canvas.Brush.Color:= FBackColor;
  136.   Canvas.FillRect(Canvas.ClipRect);
  137.  
  138.   //Foreground
  139.   case FOrientation of
  140.     poLeftToRight: R.Right:= Trunc(ClientWidth * D);
  141.     poRightToLeft: R.Left:= R.Width - Trunc(ClientWidth * D);
  142.     poBottomToTop: R.Top:= R.Height - Trunc(ClientHeight * D);
  143.     poTopToBottom: R.Bottom:= Trunc(ClientHeight * D);
  144.   end;
  145.   Canvas.Brush.Color:= FForeColor;
  146.   Canvas.FillRect(R);
  147.  
  148.   //Display Text
  149.   case FDisplay of
  150.     pdPercent: begin
  151.       S:= FormatFloat('0%', (D * 100));
  152.     end;
  153.     pdCurrent: begin
  154.       S:= IntToStr(FPosition);
  155.     end;
  156.     pdCurrentTotal: begin
  157.       S:= IntToStr(FPosition) + ' of ' + IntToStr(FMax);
  158.     end;
  159.     pdCustom: begin
  160.       S:= Caption;
  161.     end;
  162.   end;
  163.   Canvas.Font.Assign(Font);
  164.   TW:= Canvas.TextWidth(S);
  165.   TH:= Canvas.TextHeight(S);
  166.   R.Left:= (ClientWidth div 2) - (TW div 2);
  167.   R.Width:= TW;
  168.   R.Top:= (ClientHeight div 2) - (TH div 2);
  169.   R.Height:= TH;
  170.   Canvas.Brush.Style:= bsClear;
  171.  
  172.   DrawText(Canvas.Handle, S, Length(S), R, 0);
  173.  
  174. end;
  175.  
  176. end.
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190. Test Form:
  191.  
  192.  
  193.  
  194. unit uProgBarTest;
  195.  
  196. interface
  197.  
  198. uses
  199.   Winapi.Windows, Winapi.Messages,
  200.   System.SysUtils, System.Variants, System.Classes,
  201.   Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  202.   ProgressBar;
  203.  
  204. type
  205.   TForm1 = class(TForm)
  206.     procedure FormCreate(Sender: TObject);
  207.   private
  208.     FProg: TCustomProgresBar;
  209.   public
  210.     { Public declarations }
  211.   end;
  212.  
  213. var
  214.   Form1: TForm1;
  215.  
  216. implementation
  217.  
  218. {$R *.dfm}
  219.  
  220. procedure TForm1.FormCreate(Sender: TObject);
  221. begin
  222.   ReportMemoryLeaksOnShutdown:= True;
  223.   FProg:= TCustomProgresBar.Create(Self);
  224.   FProg.Parent:= Self;
  225.   FProg.Position:= 25;
  226.   FProg.Display:= TProgressBarDisplay.pdCurrentTotal;
  227. end;
  228.  
  229. end.
Advertisement
Add Comment
Please, Sign In to add comment