home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 November / CDVD1105.ISO / Software / Freeware / programare / graphics32 / Examples / Vcl / ProgressBar_Ex / G32_ProgressBar.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2004-07-05  |  6.2 KB  |  256 lines

  1. unit G32_ProgressBar;
  2.  
  3. (* ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Graphics32
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * Alex A. Denisov
  20.  *
  21.  * Portions created by the Initial Developer are Copyright (C) 2000-2004
  22.  * the Initial Developer. All Rights Reserved.
  23.  *
  24.  * Contributor(s):
  25.  *
  26.  * ***** END LICENSE BLOCK ***** *)
  27.  
  28. interface
  29.  
  30. uses
  31.   Windows, Classes, Graphics, Forms, GR32, GR32_Image, GR32_Blend;
  32.  
  33. type
  34.   TG32_ProgressBar = class(TCustomPaintBox32)
  35.   private
  36.     FBackColor: TColor;
  37.     FBorderStyle: TBorderStyle;
  38.     FContrast: Integer;
  39.     FFramed: Boolean;
  40.     FMax: Integer;
  41.     FMin: Integer;
  42.     FPosition: Integer;
  43.     procedure SetBackColor(Value: TColor);
  44.     procedure SetBorderStyle(Value: TBorderStyle);
  45.     procedure SetContrast(Value: Integer);
  46.     procedure SetFramed(Value: Boolean);
  47.     procedure SetMax(Value: Integer);
  48.     procedure SetMin(Value: Integer);
  49.     procedure SetPosition(Value: Integer);
  50.   protected
  51.     procedure DoPaintBuffer; override;
  52.     function  GetBarRect: TRect;
  53.     procedure PaintBar(Buffer: TBitmap32; ARect: TRect);
  54.     procedure PaintFrame(Buffer: TBitmap32; ARect: TRect);
  55.   public
  56.     constructor Create(AOwner: TComponent); override;
  57.   published
  58.     property Align;
  59.     property Anchors;
  60.     property BackColor: TColor read FBackColor write SetBackColor default clBtnShadow;
  61.     property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle;
  62.     property Caption;
  63.     property Color;
  64.     property Constraints;
  65.     property Contrast: Integer read FContrast write SetContrast default 64;
  66.     property Cursor;
  67.     property DragCursor;
  68.     property DragKind;
  69.     property DragMode;
  70.     property Font;
  71.     property Framed: Boolean read FFramed write SetFramed default True;
  72.     property Height default 16;
  73.     property HelpContext;
  74.     property Max: Integer read FMax write SetMax default 100;
  75.     property Min: Integer read FMin write SetMin default 0;
  76.     property ParentColor;
  77.     property ParentFont;
  78.     property ParentShowHint;
  79.     property PopupMenu;
  80.     property Position: Integer read FPosition write SetPosition;
  81.     property ShowHint;
  82.     property Visible;
  83.     property Width default 150;
  84.     property OnDragDrop;
  85.     property OnDragOver;
  86.     property OnEndDrag;
  87.     property OnMouseDown;
  88.     property OnMouseMove;
  89.     property OnMouseUp;
  90.     property OnStartDrag;
  91.   end;
  92.  
  93. procedure Register;
  94.  
  95. implementation
  96.  
  97. procedure Register;
  98. begin
  99.   RegisterComponents('Graphics32', [TG32_ProgressBar]);
  100. end;
  101.  
  102. { TG32_ProgressBar }
  103.  
  104. constructor TG32_ProgressBar.Create(AOwner: TComponent);
  105. begin
  106.   inherited;
  107.   Width := 150;
  108.   Height := 16;
  109.   FBackColor := clBtnShadow;
  110.   FContrast := 64;
  111.   FMax := 100;
  112.   FFramed := True;
  113. end;
  114.  
  115. procedure TG32_ProgressBar.DoPaintBuffer;
  116. var
  117.   Rect: TRect;
  118. begin
  119.   inherited;
  120.   Rect := ClientRect;
  121.   PaintFrame(Buffer, Rect);
  122.   PaintBar(Buffer, GetBarRect);
  123. end;
  124.  
  125. function TG32_ProgressBar.GetBarRect: TRect;
  126. var
  127.   X: Integer;
  128. begin
  129.   Result := ClientRect;
  130.   if Framed then InflateRect(Result, -1, -1);
  131.   with Result do
  132.     if (Position > Min) and (Max > Min) then
  133.     begin
  134.       X := Left + (Position - Min) * (Right - Left) div (Max - Min);
  135.       if X < Right then Right := X;
  136.     end
  137.     else
  138.       Right := Left; // return an empty rectagle
  139. end;
  140.  
  141. procedure TG32_ProgressBar.PaintBar(Buffer: TBitmap32; ARect: TRect);
  142. var
  143.   Clr, LineColor: TColor32;
  144.   I, CY, H: Integer;
  145. begin
  146.   if IsRectEmpty(ARect) then Exit;
  147.  
  148.   Clr := Color32(Color);
  149.  
  150.   if Contrast <> 0 then
  151.   begin
  152.     with ARect do
  153.     try
  154.       H := Bottom - Top;
  155.       CY := (Top + Bottom) div 2;
  156.       for I := Top to Bottom - 1 do
  157.       begin
  158.         LineColor := Lighten(Clr, (CY - I) * Contrast div H);
  159.         Buffer.HorzLineS(Left, I, Right - 1, LineColor);
  160.       end;
  161.     finally
  162.       EMMS; // the low-level blending function was used EMMS is required
  163.     end;
  164.   end
  165.   else Buffer.FillRectS(ARect, Clr);
  166.  
  167.   Buffer.RaiseRectTS(ARect, 32);
  168.  
  169.   if Framed then
  170.     with ARect do Buffer.VertLineS(Right, Top, Bottom, Color32(clWindowFrame));
  171. end;
  172.  
  173. procedure TG32_ProgressBar.PaintFrame(Buffer: TBitmap32; ARect: TRect);
  174. var
  175.   Clr: TColor32;
  176. begin
  177.   if BorderStyle = bsSingle then
  178.   begin
  179.   end;
  180.  
  181.   { Paint frame border }
  182.   if Framed then
  183.   begin
  184.     Clr := TColor32(clWindowFrame);
  185.     Buffer.FrameRectS(ARect, Clr);
  186.     InflateRect(ARect, -1, -1);
  187.   end;
  188.  
  189.   { Fill the background }
  190.   Clr := Color32(BackColor);
  191.   Buffer.FillRectS(ARect, Clr);
  192.  
  193.   { Paint shadow on top of background}
  194.   with ARect do
  195.   begin
  196.     Dec(Right);
  197.     Dec(Bottom);
  198.     Clr := Lighten(Clr, -32);
  199.     Buffer.HorzLineS(Left, Top, Right, Clr);
  200.     Buffer.VertLineS(Left, Top + 1, Bottom, Clr);
  201.     if not Framed then
  202.     begin
  203.       Clr := Lighten(Clr, 64);
  204.       Buffer.HorzLineS(Left, Bottom, Right, Clr);
  205.       Buffer.VertLineS(Right, Top + 1, Bottom, Clr);
  206.     end;
  207.   end;
  208. end;
  209.  
  210. procedure TG32_ProgressBar.SetBackColor(Value: TColor);
  211. begin
  212.   FBackColor := Value;
  213.   Invalidate;
  214. end;
  215.  
  216. procedure TG32_ProgressBar.SetBorderStyle(Value: TBorderStyle);
  217. begin
  218.   FBorderStyle := Value;
  219.   Invalidate;
  220. end;
  221.  
  222. procedure TG32_ProgressBar.SetContrast(Value: Integer);
  223. begin
  224.   FContrast := Value;
  225.   Invalidate;
  226. end;
  227.  
  228. procedure TG32_ProgressBar.SetFramed(Value: Boolean);
  229. begin
  230.   FFramed := Value;
  231.   Invalidate;
  232. end;
  233.  
  234. procedure TG32_ProgressBar.SetMax(Value: Integer);
  235. begin
  236.   FMax := Value;
  237.   Invalidate;
  238. end;
  239.  
  240. procedure TG32_ProgressBar.SetMin(Value: Integer);
  241. begin
  242.   FMin := Value;
  243.   Invalidate;
  244. end;
  245.  
  246. procedure TG32_ProgressBar.SetPosition(Value: Integer);
  247. begin
  248.   if Value <> Position then
  249.   begin
  250.     FPosition := Value;
  251.     Repaint;
  252.   end;
  253. end;
  254.  
  255. end.
  256.