begin process at 2010 02 10 02:36:43
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Graphique

 > BARRE DE PROGRESSION À LA CLEARLOOKS

BARRE DE PROGRESSION À LA CLEARLOOKS


 Information sur la source

Note :
10 / 10 - par 2 personnes
10,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Graphique Classé sous :barre, progression, clearlooks, graphics32, animation Niveau :Initié Date de création :15/11/2008 Date de mise à jour :18/11/2008 10:01:52 Vu / téléchargé :2 781 / 476

Auteur : yannbobu

Ecrire un message privé
Site perso
Commentaire sur cette source (10)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
Voici un composant basé sur la librairie Graphics32 qui permet d'être utilisé en tant que barre de progression animée. Une option "marquee" permet de rendre son état indéterminé.

Pour compiler la source, il vous faut les librairies:

Graphics32 : http://graphics32.org/wiki/
The GR32 Extension Components Pack : http://code.google.com/p/gr32ex/

Pour tester l'exemple, renommer le fichier "Exemple.exec" en "Exemple.exe" (merci Nicolas)

Source

  • unit GrProgressBar;
  • (* ***** BEGIN LICENSE BLOCK *****
  • * Version: MPL 1.1
  • *
  • * The contents of this file are subject to the Mozilla Public License Version
  • * 1.1 (the "License"); you may not use this file except in compliance with
  • * the License. You may obtain a copy of the License at
  • * http://www.mozilla.org/MPL/
  • *
  • * Software distributed under the License is distributed on an "AS IS" basis,
  • * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  • * for the specific language governing rights and limitations under the
  • * License.
  • *
  • * The Original Code is GrProgressBar
  • *
  • * The Initial Developer of the Original Code is
  • * Yann Papouin <yann.papouin at @ gmail.com>
  • *
  • * ***** END LICENSE BLOCK ***** *)
  • { $DEFINE DEBUG}
  • interface
  • uses
  • {$IfDef DEBUG}DbugIntf,{$ENDIF}
  • Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Math,
  • GR32, GR32_Image, GR32_Polygons, SimpleTimer;
  • type
  • TBarDirection = (
  • bdRight,
  • bdLeft
  • );
  • TGrProgressbar = class(TCustomImage32)
  • private
  • FModulo : integer;
  • FInvalidateColor : boolean;
  • FApplyingSystemColor: boolean;
  • FColor1 : TColor32;
  • FColor2 : TColor32;
  • FBarDirection : TBarDirection;
  • FLimitRight : integer;
  • FLimitLeft : integer;
  • FBar : TPolygon32;
  • FAniTimer: TSimpleTimer;
  • FInvalidatePattern : boolean;
  • FFiller: TBitmapPolygonFiller;
  • FUseSystemColors: boolean;
  • FAnimated: boolean;
  • FMax: integer;
  • FMin: integer;
  • FPosition: integer;
  • FMarquee: boolean;
  • FMarqueeWidth: single;
  • FMarqueeSpeed: integer;
  • FColor: TColor;
  • FAnimAtMax: boolean;
  • FFlip: boolean;
  • procedure SetUseSystemColors(const Value: boolean);
  • procedure SetAnimated(const Value: boolean);
  • { Déclarations privées }
  • procedure ComputePattern;
  • procedure InvalidatePattern;
  • procedure SetMax(const Value: integer);
  • procedure SetMin(const Value: integer);
  • procedure SetPosition(Value: integer);
  • procedure DoAniTimer(Sender: TObject);
  • procedure SetInterval(const Value: Cardinal);
  • function GetInterval: Cardinal;
  • procedure SetMarquee(const Value: boolean);
  • procedure SetMarqueeWidth(Value: single);
  • procedure SetMarqueeSpeed(const Value: integer);
  • procedure SetColor(const Value: TColor);
  • procedure SetAnimAtMax(const Value: boolean);
  • procedure SetFlip(const Value: boolean);
  • protected
  • procedure BuildBar;
  • procedure Resize; override;
  • procedure DoPaintBuffer; override;
  • function InternalPaintBuffer(aBitmap32: TBitmap32): Boolean;
  • procedure DrawBackground(aBitmap32: TBitmap32);
  • procedure CMStyleChanged( var msg: TMessage); message WM_THEMECHANGED;
  • procedure CMShowingChanged(var msg: TMessage); message CM_SHOWINGCHANGED;
  • public
  • { Déclarations publiques }
  • constructor Create(AOwner: TComponent); override;
  • destructor Destroy; override;
  • procedure Loaded; override;
  • published
  • property Align;
  • property AlignWithMargins;
  • property Anchors;
  • property Margins;
  • property AnimAtMax : boolean read FAnimAtMax write SetAnimAtMax;
  • property Animated : boolean read FAnimated write SetAnimated;
  • property Interval : Cardinal read GetInterval write SetInterval;
  • property UseSystemColors : boolean read FUseSystemColors write SetUseSystemColors;
  • property Max : integer read FMax write SetMax;
  • property Min : integer read FMin write SetMin;
  • property Position : integer read FPosition write SetPosition;
  • property Marquee : boolean read FMarquee write SetMarquee;
  • property MarqueeWidth : single read FMarqueeWidth write SetMarqueeWidth;
  • property MarqueeSpeed : integer read FMarqueeSpeed write SetMarqueeSpeed;
  • property Color : TColor read FColor write SetColor;
  • property Flip : boolean read FFlip write SetFlip;
  • end;
  • procedure Register;
  • implementation
  • uses
  • Themes, UxTheme;
  • var
  • SystemColor : TColor;
  • procedure Register;
  • begin
  • RegisterComponents('GLDali', [TGrProgressbar]);
  • end;
  • function Linearize(Ax, Ay, Bx, By, Value : Single): Single;
  • begin
  • if (Bx <> Ax) and (Ay <> By) then
  • result := Ay + (Value - Ax) / (Bx - Ax) * (By-Ay)
  • else
  • result := 0;
  • end;
  • { TGrProgressbar }
  • constructor TGrProgressbar.Create(AOwner: TComponent);
  • begin
  • inherited;
  • Height := 15;
  • Width := 150;
  • FFlip := true;
  • FModulo := Height;
  • FMin := 0;
  • FMax := 100;
  • FMarqueeWidth := 0.25;
  • FMarqueeSpeed := 5;
  • FBar := TPolygon32.Create;
  • FAniTimer := TSimpleTimer.CreateEx(50, DoAniTimer);
  • FFiller := TBitmapPolygonFiller.Create;
  • ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
  • csSetCaption, csParentBackground, csDoubleClicks, csReplicatable, csPannable];
  • //Color := clHighlight;
  • Animated := true;
  • end;
  • destructor TGrProgressbar.Destroy;
  • begin
  • FAniTimer.Free;
  • FFiller.Free;
  • FBar.Free;
  • inherited;
  • end;
  • procedure TGrProgressbar.Loaded;
  • begin
  • inherited Loaded;
  • Buffer.SetSize(Width,Height);
  • BuildBar;
  • InvalidatePattern;
  • {$IfDef DEBUG}SendDebugFmt('Progress bar named %s loaded',[Name]);{$ENDIF}
  • end;
  • procedure TGrProgressbar.InvalidatePattern;
  • begin
  • FInvalidatePattern := true;
  • Changed;
  • end;
  • // The pattern is computed only once
  • // It need to be recomputed after a resize of the control or a color (theme) change
  • procedure TGrProgressbar.ComputePattern;
  • var
  • Polygon : TPolygon32;
  • PatternWidth : integer;
  • PatternHeight : integer;
  • begin
  • PatternHeight := Height;
  • PatternWidth := PatternHeight*2;
  • FModulo := PatternWidth;
  • if Assigned(FFiller.Pattern) then
  • FFiller.Pattern.Free;
  • // First create a rectangle with the wanted color
  • FFiller.Pattern := TBitmap32.Create;
  • FFiller.Pattern.SetSize(PatternWidth,PatternHeight);
  • FFiller.Pattern.FillRectTS(0,0,PatternWidth,PatternHeight, FColor1);
  • PatternHeight := PatternHeight;
  • Polygon := TPolygon32.Create;
  • // Next draw a transparent parallelogram (a rectangle that is skewed)
  • with Polygon do
  • begin
  • Antialiased := true;
  • Add(FixedPoint(0,0));
  • Add(FixedPoint(PatternWidth div 2,0));
  • Add(FixedPoint(PatternWidth, PatternHeight));
  • Add(FixedPoint(PatternWidth div 2, PatternHeight));
  • DrawFill(FFiller.Pattern, SetAlpha(FColor2,127));
  • // Mirror operation on vertical axis if needed
  • if Flip then
  • FFiller.Pattern.FlipHorz(FFiller.Pattern);
  • end;
  • Polygon.Free;
  • // Finally draw a crystal effect
  • FFiller.Pattern.FillRectTS(0, 0, PatternWidth, PatternHeight div 2, SetAlpha(CLWhite32,60));
  • end;
  • procedure TGrProgressbar.BuildBar;
  • var
  • LPosition : integer;
  • Offset : integer;
  • Inflate : integer;
  • DrawRect : TRect;
  • begin
  • if not Assigned(Parent) then
  • Exit;
  • // Inflate value help to reduce the bar size from it's background
  • Inflate := -3;
  • // An offset is here to correct the 3D effect
  • Offset := -1;
  • // Get X coordinate from the Min, Max and Position Values
  • if not Marquee then
  • begin
  • LPosition := Round(Linearize(Min, -2* Inflate + Offset, Max, Width, Position));
  • {$IfDef DEBUG}SendInteger('LPosition',LPosition);{$ENDIF}
  • end;
  • DrawRect := ClientRect;
  • // Limits are used by the marquee animation
  • FLimitRight := DrawRect.Right + Inflate;
  • FLimitLeft := DrawRect.Left - Inflate + Offset;
  • if not Marquee then
  • DrawRect.Right := LPosition
  • else
  • DrawRect.Right := Round(DrawRect.Right*MarqueeWidth); // setting the marquee width by using it's percent value
  • // Apply the offset
  • DrawRect.Top := DrawRect.Top + Offset;
  • DrawRect.Left := DrawRect.Left + Offset;
  • InflateRect(DrawRect, Inflate, Inflate);
  • {$IfDef DEBUG}SendInteger('DrawRect.Left',DrawRect.Left);{$ENDIF}
  • {$IfDef DEBUG}SendInteger('DrawRect.Right',DrawRect.Right);{$ENDIF}
  • // Drawing the bar polygon
  • with FBar do
  • begin
  • Clear;
  • if DrawRect.Left <> DrawRect.Right then
  • begin
  • Add(FixedPoint(DrawRect.Left,DrawRect.Top));
  • Add(FixedPoint(DrawRect.Right,DrawRect.Top));
  • Add(FixedPoint(DrawRect.Right,DrawRect.Bottom));
  • Add(FixedPoint(DrawRect.Left,DrawRect.Bottom));
  • end;
  • end;
  • end;
  • procedure TGrProgressbar.DrawBackground(aBitmap32: TBitmap32);
  • var
  • R: TRect;
  • cl: TColor;
  • FProgressThemeData : HTHEME;
  • begin
  • if UseThemes then
  • begin
  • // Apply the WinXP style
  • FProgressThemeData := OpenThemeData(Handle, 'Progress');
  • R := ClientRect;
  • DrawThemeBackground(FProgressThemeData, aBitmap32.Handle, PP_BAR, 0, R, nil);
  • end
  • else
  • begin
  • // Mimic the Win2K style by drawing a lowered borders
  • aBitmap32.RaiseRectTS(0,0,width,height,-50);
  • end;
  • end;
  • function TGrProgressbar.InternalPaintBuffer(aBitmap32: TBitmap32): Boolean;
  • var
  • Polygon : TPolygon32;
  • LPosition : integer;
  • Inflate : integer;
  • DrawRect : TRect;
  • begin
  • // Do not try to draw anything is there is no parent for self
  • if not Assigned(Parent) then
  • begin
  • aBitmap32.Clear(clRed32);
  • Exit;
  • end;
  • // Recalc color after a theme change
  • if FInvalidateColor then
  • begin
  • UseSystemColors := UseSystemColors;
  • FInvalidateColor := false;
  • end;
  • // Recalc pattern on resize or color change
  • if FInvalidatePattern then
  • begin
  • FInvalidatePattern := false;
  • ComputePattern;
  • end;
  • // Draw the control background (themed if we can)
  • DrawBackground(aBitmap32);
  • // Draw the bar itself by using the pattern
  • if ((Position > 0) and not Marquee) or Marquee then
  • begin
  • with FBar do
  • begin
  • // The pattern is automatically repeated on x-axis and y-axis
  • DrawFill(aBitmap32, FFiller);
  • DrawEdge(aBitmap32, SetAlpha(clBlack32, 100));
  • end;
  • end;
  • result := true;
  • end;
  • procedure TGrProgressbar.DoPaintBuffer;
  • begin
  • inherited;
  • InternalPaintBuffer(self.Buffer);
  • end;
  • // Recompute bar size and pattern size
  • procedure TGrProgressbar.Resize;
  • begin
  • inherited;
  • BuildBar;
  • InvalidatePattern;
  • end;
  • procedure TGrProgressbar.DoAniTimer(Sender: TObject);
  • begin
  • if not FMarquee then
  • begin
  • if not ((Position = Max) and not FAnimAtMax) then
  • begin
  • // Animate pattern of the bar with a scroll
  • FFiller.OffsetX := (FFiller.OffsetX-1) mod FModulo;
  • end;
  • end
  • else
  • if FMarquee then
  • begin
  • // Changing direction from right to left
  • if (FBar.Points[0][1].X >= Fixed(FLimitRight)) and (FBarDirection = bdRight) then
  • begin
  • FBarDirection := bdLeft;
  • end;
  • // Changing direction from left to right
  • if (FBar.Points[0][0].X <= Fixed(FLimitLeft)) and (FBarDirection = bdLeft) then
  • begin
  • FBarDirection := bdRight;
  • end;
  • // Move the bar itself without moving the pattern
  • case FBarDirection of
  • bdRight :
  • begin
  • FBar.Offset(Fixed(FMarqueeSpeed), Fixed(0));
  • end;
  • bdLeft :
  • begin
  • FBar.Offset(Fixed(-FMarqueeSpeed), Fixed(0));
  • end;
  • end;
  • end;
  • // Repaint control
  • Changed;
  • end;
  • // Boolean value to enable/disable animation
  • procedure TGrProgressbar.SetAnimated(const Value: boolean);
  • begin
  • FAnimated := Value;
  • if Visible then
  • FAniTimer.Enabled := FAnimated;
  • // Reset pattern position
  • if not FAnimated then
  • FFiller.OffsetX := 0;
  • end;
  • // Continue scrolling when Position = Max ?
  • procedure TGrProgressbar.SetAnimAtMax(const Value: boolean);
  • begin
  • FAnimAtMax := Value;
  • end;
  • function TGrProgressbar.GetInterval: Cardinal;
  • begin
  • result := FAniTimer.Interval;
  • end;
  • procedure TGrProgressbar.SetInterval(const Value: Cardinal);
  • begin
  • FAniTimer.Interval := Value;
  • end;
  • // A marquee progress bar is a progress bar with unknow min, max and position values
  • // It's a feedback to state to warn the user that a thing is currently doing something
  • procedure TGrProgressbar.SetMarquee(const Value: boolean);
  • begin
  • if FMarquee <> Value then
  • begin
  • FMarquee := Value;
  • if not FMarquee then
  • begin
  • Position := Min;
  • end;
  • end;
  • BuildBar;
  • end;
  • // The marquee width is it's width in percentage of the bar width
  • procedure TGrProgressbar.SetMarqueeWidth(Value: single);
  • begin
  • Value := Math.Min(1.0,Value);
  • Value := Math.Max(0.0,Value);
  • if FMarqueeWidth <> Value then
  • begin
  • FMarqueeWidth := Value;
  • BuildBar;
  • end;
  • end;
  • procedure TGrProgressbar.SetMarqueeSpeed(const Value: integer);
  • begin
  • if FMarqueeSpeed <> Value then
  • begin
  • FMarqueeSpeed := Value;
  • FMarqueeSpeed := Math.Min(100,FMarqueeSpeed);
  • FMarqueeSpeed := Math.Max(1,FMarqueeSpeed);
  • end;
  • end;
  • procedure TGrProgressbar.SetMax(const Value: integer);
  • begin
  • if FMax <> Value then
  • begin
  • FMax := Value;
  • BuildBar;
  • end;
  • Position := Position;
  • end;
  • procedure TGrProgressbar.SetMin(const Value: integer);
  • begin
  • if FMin <> Value then
  • begin
  • FMin := Value;
  • BuildBar;
  • end;
  • Position := Position;
  • end;
  • procedure TGrProgressbar.SetPosition(Value: integer);
  • begin
  • // First, limit the setted value to min and max position
  • Value := Math.Min(Max, Value);
  • Value := Math.Max(Min, Value);
  • // Disable marquee if we write a new position
  • FMarquee := false;
  • if FPosition <> Value then
  • begin
  • FPosition := Value;
  • BuildBar;
  • Changed;
  • end;
  • end;
  • procedure TGrProgressbar.SetUseSystemColors(const Value: boolean);
  • var
  • ABmp : TBitmap;
  • PickColor: TColor;
  • FProgressThemeData : HTHEME;
  • begin
  • FUseSystemColors := Value;
  • if FUseSystemColors then
  • begin
  • FApplyingSystemColor := true;
  • // Check if Windows Themes are enabled
  • if UseThemes then
  • begin
  • // A small way (not really clean) to get the color of a themed progress bar
  • if (SystemColor = clNone) then
  • begin
  • {$IfDef DEBUG}SendDebug('SystemColor is None');{$ENDIF}
  • // We create a local bitmap where we painting on it a themed progress bar
  • ABmp := TBitmap.Create;
  • ABmp.SetSize(16,16);
  • FProgressThemeData := OpenThemeData(Handle, 'Progress');
  • DrawThemeBackground(FProgressThemeData, ABmp.Canvas.Handle, PP_CHUNK, 0, ABmp.Canvas.ClipRect, nil);
  • CloseThemeData(FProgressThemeData);
  • // After painting, we pick the color at a logical position
  • // Just hope that there is a valid color here
  • PickColor := ABmp.Canvas.Pixels[2,2];
  • if PickColor <> CLR_INVALID then
  • begin
  • SystemColor := PickColor;
  • {$IfDef DEBUG}SendDebug('System color found, extracted from BITMAP');{$ENDIF}
  • end
  • else
  • begin
  • SystemColor := clHighlight;
  • {$IfDef DEBUG}SendDebug('System color not found, using the Highlight one');{$ENDIF}
  • end;
  • ABmp.Free;
  • end;
  • Color := SystemColor;
  • {$IfDef DEBUG}SendDebug('Using previous computed system color');{$ENDIF}
  • end
  • else
  • begin
  • // If theming is not enabled then use the default highlight color
  • Color := clHighlight;
  • {$IfDef DEBUG}SendDebug('No theme is used, default highlight color');{$ENDIF}
  • end;
  • FApplyingSystemColor := false;
  • end;
  • end;
  • procedure TGrProgressbar.SetColor(const Value: TColor);
  • var
  • H,S,L : Byte;
  • begin
  • if FColor <> Value then
  • begin
  • // Disable system color only if SetColor is not called by the UseSystemColors method
  • if not FApplyingSystemColor then
  • UseSystemColors := false;
  • FColor := Value;
  • // Extracting the Wincolor to Hue, Saturation and Luminosity values
  • FColor1 := Color32(FColor);
  • RGBtoHSL(FColor1, H, S, L);
  • // Small way to set the color of the slashes
  • if Intensity(FColor1) > 160
  • then
  • FColor2 := clBlack32
  • else
  • FColor2 := clWhite32;
  • // Redraw the pattern with new colors
  • InvalidatePattern;
  • end;
  • end;
  • // Property that allow the flippig of the slashes
  • procedure TGrProgressbar.SetFlip(const Value: boolean);
  • begin
  • FFlip := Value;
  • InvalidatePattern;
  • end;
  • /// Catched Windows messages
  • // If the theme is changed by a new theme then invalidate the system color
  • // It will be recomputed by the first progress bar with UseSystemColor property enabled
  • procedure TGrProgressbar.CMStyleChanged(var msg: TMessage);
  • begin
  • {$IfDef DEBUG}SendDebug('TGrProgressbar.CMStyleChanged');{$ENDIF}
  • // Update colors
  • SystemColor := clNone;
  • FInvalidateColor := true;
  • end;
  • // Stop the timer when this control is not visible, else re-enable it
  • procedure TGrProgressbar.CMShowingChanged(var msg: TMessage);
  • begin
  • inherited;
  • {$IfDef DEBUG}SendDebug('CMShowingChanged');{$ENDIF}
  • if Showing then
  • begin
  • if Animated and not FAniTimer.Enabled then
  • begin
  • FAniTimer.Enabled := true;
  • {$IfDef DEBUG}SendDebugFmt('Timer of %s started',[Name]);{$ENDIF}
  • end;
  • end
  • else
  • begin
  • if Animated and FAniTimer.Enabled then
  • begin
  • FAniTimer.Enabled := false;
  • {$IfDef DEBUG}SendDebugFmt('Timer of %s stopped',[Name]);{$ENDIF}
  • end;
  • end;
  • end;
  • initialization
  • SystemColor := clNone;
  • finalization
  • end.
unit GrProgressBar;

(* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is GrProgressBar
 *
 * The Initial Developer of the Original Code is
 * Yann Papouin <yann.papouin at @ gmail.com>
 *
 * ***** END LICENSE BLOCK ***** *)
 
{ $DEFINE DEBUG}

interface

uses
  {$IfDef DEBUG}DbugIntf,{$ENDIF}
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Math,
  GR32, GR32_Image, GR32_Polygons, SimpleTimer;

type
  TBarDirection = (
    bdRight,
    bdLeft
  );

  TGrProgressbar = class(TCustomImage32)
  private
    FModulo : integer;
    FInvalidateColor : boolean;
    FApplyingSystemColor: boolean;
    FColor1 : TColor32;
    FColor2 : TColor32;
    FBarDirection : TBarDirection;
    FLimitRight : integer;
    FLimitLeft : integer;
    FBar : TPolygon32;
    FAniTimer: TSimpleTimer;
    FInvalidatePattern : boolean;
    FFiller: TBitmapPolygonFiller;
    FUseSystemColors: boolean;
    FAnimated: boolean;
    FMax: integer;
    FMin: integer;
    FPosition: integer;
    FMarquee: boolean;
    FMarqueeWidth: single;
    FMarqueeSpeed: integer;
    FColor: TColor;
    FAnimAtMax: boolean;
    FFlip: boolean;
    procedure SetUseSystemColors(const Value: boolean);
    procedure SetAnimated(const Value: boolean);
    { Déclarations privées }
    procedure ComputePattern;
    procedure InvalidatePattern;
    procedure SetMax(const Value: integer);
    procedure SetMin(const Value: integer);
    procedure SetPosition(Value: integer);
    procedure DoAniTimer(Sender: TObject);
    procedure SetInterval(const Value: Cardinal);
    function GetInterval: Cardinal;
    procedure SetMarquee(const Value: boolean);
    procedure SetMarqueeWidth(Value: single);
    procedure SetMarqueeSpeed(const Value: integer);
    procedure SetColor(const Value: TColor);
    procedure SetAnimAtMax(const Value: boolean);
    procedure SetFlip(const Value: boolean);
  protected
    procedure BuildBar;
    procedure Resize; override;
    procedure DoPaintBuffer; override;
    function InternalPaintBuffer(aBitmap32: TBitmap32): Boolean;
    procedure DrawBackground(aBitmap32: TBitmap32);
    procedure CMStyleChanged( var msg: TMessage); message WM_THEMECHANGED;
    procedure CMShowingChanged(var msg: TMessage); message CM_SHOWINGCHANGED;
  public
    { Déclarations publiques }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Loaded; override;
  published
    property Align;
    property AlignWithMargins;
    property Anchors;
    property Margins;
    property AnimAtMax : boolean read FAnimAtMax write SetAnimAtMax;
    property Animated : boolean read FAnimated write SetAnimated;
    property Interval : Cardinal read GetInterval write SetInterval;
    property UseSystemColors : boolean read FUseSystemColors write SetUseSystemColors;
    property Max : integer read FMax write SetMax;
    property Min : integer read FMin write SetMin;
    property Position : integer read FPosition write SetPosition;
    property Marquee : boolean read FMarquee write SetMarquee;
    property MarqueeWidth : single read FMarqueeWidth write SetMarqueeWidth;
    property MarqueeSpeed : integer read FMarqueeSpeed write SetMarqueeSpeed;
    property Color : TColor read FColor write SetColor;
    property Flip : boolean read FFlip write SetFlip;
  end;


procedure Register;

implementation

uses
  Themes, UxTheme;

var
  SystemColor : TColor;

procedure Register;
begin
  RegisterComponents('GLDali', [TGrProgressbar]);
end;


function Linearize(Ax, Ay, Bx, By, Value : Single): Single;
begin
  if (Bx <> Ax) and (Ay <> By) then
    result := Ay + (Value - Ax) / (Bx - Ax) * (By-Ay)
  else
    result := 0;
end;


{ TGrProgressbar }

constructor TGrProgressbar.Create(AOwner: TComponent);
begin
  inherited;
  
  Height := 15;
  Width := 150;

  FFlip := true;
  FModulo := Height;
  FMin := 0;
  FMax := 100;
  FMarqueeWidth := 0.25;
  FMarqueeSpeed := 5;
  FBar := TPolygon32.Create;

  FAniTimer := TSimpleTimer.CreateEx(50, DoAniTimer);
  FFiller := TBitmapPolygonFiller.Create;

  ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
    csSetCaption, csParentBackground, csDoubleClicks, csReplicatable, csPannable];

  //Color := clHighlight;
  Animated := true;
end;

destructor TGrProgressbar.Destroy;
begin
  FAniTimer.Free;
  FFiller.Free;
  FBar.Free;
  inherited;
end;

procedure TGrProgressbar.Loaded;
begin
  inherited Loaded;
  Buffer.SetSize(Width,Height);
  BuildBar;
  InvalidatePattern;
  {$IfDef DEBUG}SendDebugFmt('Progress bar named %s loaded',[Name]);{$ENDIF}
end;


procedure TGrProgressbar.InvalidatePattern;
begin
  FInvalidatePattern := true;
  Changed;
end;


// The pattern is computed only once
// It need to be recomputed after a resize of the control or a color (theme) change
procedure TGrProgressbar.ComputePattern;
var
  Polygon : TPolygon32;
  PatternWidth : integer;
  PatternHeight : integer;
begin
  PatternHeight := Height;
  PatternWidth := PatternHeight*2;
  FModulo := PatternWidth;

  if Assigned(FFiller.Pattern) then
    FFiller.Pattern.Free;

  // First create a rectangle with the wanted color
  FFiller.Pattern := TBitmap32.Create;
  FFiller.Pattern.SetSize(PatternWidth,PatternHeight);
  FFiller.Pattern.FillRectTS(0,0,PatternWidth,PatternHeight, FColor1);

  PatternHeight := PatternHeight;

  Polygon := TPolygon32.Create;

  // Next draw a transparent parallelogram (a rectangle that is skewed)
  with Polygon do
  begin
    Antialiased := true;
    Add(FixedPoint(0,0));
    Add(FixedPoint(PatternWidth div 2,0));
    Add(FixedPoint(PatternWidth, PatternHeight));
    Add(FixedPoint(PatternWidth div 2, PatternHeight));
    DrawFill(FFiller.Pattern, SetAlpha(FColor2,127));

    // Mirror operation on vertical axis if needed
    if Flip then
      FFiller.Pattern.FlipHorz(FFiller.Pattern);
  end;
  Polygon.Free;

  // Finally draw a crystal effect
  FFiller.Pattern.FillRectTS(0, 0, PatternWidth, PatternHeight div 2, SetAlpha(CLWhite32,60));
end;


procedure TGrProgressbar.BuildBar;
var
  LPosition : integer;
  Offset : integer;
  Inflate : integer;
  DrawRect : TRect;
begin

  if not Assigned(Parent) then
    Exit;

  // Inflate value help to reduce the bar size from it's background
  Inflate := -3;

  // An offset is here to correct the 3D effect
  Offset := -1;

  // Get X coordinate from the Min, Max and Position Values
  if not Marquee then
  begin
    LPosition := Round(Linearize(Min, -2* Inflate + Offset, Max, Width, Position));
    {$IfDef DEBUG}SendInteger('LPosition',LPosition);{$ENDIF}
  end;

  DrawRect := ClientRect;

  // Limits are used by the marquee animation
  FLimitRight := DrawRect.Right + Inflate;
  FLimitLeft := DrawRect.Left - Inflate + Offset;

  if not Marquee then
    DrawRect.Right := LPosition
  else
    DrawRect.Right := Round(DrawRect.Right*MarqueeWidth);  // setting the marquee width by using it's percent value

  // Apply the offset
  DrawRect.Top := DrawRect.Top + Offset;
  DrawRect.Left := DrawRect.Left + Offset;

  InflateRect(DrawRect, Inflate, Inflate);

  {$IfDef DEBUG}SendInteger('DrawRect.Left',DrawRect.Left);{$ENDIF}
  {$IfDef DEBUG}SendInteger('DrawRect.Right',DrawRect.Right);{$ENDIF}
  
  // Drawing the bar polygon
  with FBar do
  begin
    Clear;
    if DrawRect.Left <> DrawRect.Right then
    begin
      Add(FixedPoint(DrawRect.Left,DrawRect.Top));
      Add(FixedPoint(DrawRect.Right,DrawRect.Top));
      Add(FixedPoint(DrawRect.Right,DrawRect.Bottom));
      Add(FixedPoint(DrawRect.Left,DrawRect.Bottom));
    end;
  end;
end;

procedure TGrProgressbar.DrawBackground(aBitmap32: TBitmap32);
var
  R: TRect;
  cl: TColor;
  FProgressThemeData : HTHEME;
begin

  if UseThemes then
  begin
    // Apply the WinXP style
    FProgressThemeData := OpenThemeData(Handle, 'Progress');
    R := ClientRect;
    DrawThemeBackground(FProgressThemeData, aBitmap32.Handle, PP_BAR, 0, R, nil);
  end
    else
  begin
    // Mimic the Win2K style by drawing a lowered borders
    aBitmap32.RaiseRectTS(0,0,width,height,-50);
  end;

end;


function TGrProgressbar.InternalPaintBuffer(aBitmap32: TBitmap32): Boolean;
var
  Polygon : TPolygon32;
  LPosition : integer;
  Inflate : integer;
  DrawRect : TRect;
begin

  // Do not try to draw anything is there is no parent for self
  if not Assigned(Parent) then
  begin
    aBitmap32.Clear(clRed32);
    Exit;
  end;

  // Recalc color after a theme change
  if FInvalidateColor then
  begin
    UseSystemColors := UseSystemColors;
    FInvalidateColor := false;
  end;

  // Recalc pattern on resize or color change
  if FInvalidatePattern then
  begin
    FInvalidatePattern := false;
    ComputePattern;
  end;

  // Draw the control background (themed if we can)
  DrawBackground(aBitmap32);

  // Draw the bar itself by using the pattern
  if ((Position > 0) and not Marquee) or Marquee then
  begin
    with FBar do
    begin
      // The pattern is automatically repeated on x-axis and y-axis
      DrawFill(aBitmap32, FFiller);
      DrawEdge(aBitmap32, SetAlpha(clBlack32, 100));
    end;
  end;

  result := true;
end;

procedure TGrProgressbar.DoPaintBuffer;
begin
  inherited;
  InternalPaintBuffer(self.Buffer);
end;

// Recompute bar size and pattern size
procedure TGrProgressbar.Resize;
begin
  inherited;
  BuildBar;
  InvalidatePattern;
end;

procedure TGrProgressbar.DoAniTimer(Sender: TObject);
begin
  if not FMarquee then
  begin
    if not ((Position = Max) and not FAnimAtMax) then
    begin
      // Animate pattern of the bar with a scroll
      FFiller.OffsetX := (FFiller.OffsetX-1) mod FModulo;
    end;
  end
    else
  if FMarquee then
  begin
    // Changing direction from right to left
    if (FBar.Points[0][1].X >= Fixed(FLimitRight)) and (FBarDirection = bdRight) then
    begin
      FBarDirection := bdLeft;
    end;

    // Changing direction from left to right
    if (FBar.Points[0][0].X <= Fixed(FLimitLeft)) and (FBarDirection = bdLeft) then
    begin
      FBarDirection := bdRight;
    end;

    // Move the bar itself without moving the pattern
    case FBarDirection of
      bdRight :
        begin
          FBar.Offset(Fixed(FMarqueeSpeed), Fixed(0));
        end;
      bdLeft  :
        begin
          FBar.Offset(Fixed(-FMarqueeSpeed), Fixed(0));
        end;
    end;

  end;

  // Repaint control
  Changed;
end;

// Boolean value to enable/disable animation
procedure TGrProgressbar.SetAnimated(const Value: boolean);
begin
  FAnimated := Value;

  if Visible then
    FAniTimer.Enabled := FAnimated;

  // Reset pattern position
  if not FAnimated then
    FFiller.OffsetX := 0;
end;

// Continue scrolling when Position = Max ?
procedure TGrProgressbar.SetAnimAtMax(const Value: boolean);
begin
  FAnimAtMax := Value;
end;

function TGrProgressbar.GetInterval: Cardinal;
begin
  result := FAniTimer.Interval;
end;

procedure TGrProgressbar.SetInterval(const Value: Cardinal);
begin
  FAniTimer.Interval := Value;
end;

// A marquee progress bar is a progress bar with unknow min, max and position values
// It's a feedback to state to warn the user that a thing is currently doing something
procedure TGrProgressbar.SetMarquee(const Value: boolean);
begin
  if FMarquee <> Value then
  begin
    FMarquee := Value;
    if not FMarquee then
    begin
      Position := Min;
    end;
  end;

  BuildBar;
end;

// The marquee width is it's width in percentage of the bar width
procedure TGrProgressbar.SetMarqueeWidth(Value: single);
begin
  Value := Math.Min(1.0,Value);
  Value := Math.Max(0.0,Value);
  if FMarqueeWidth <> Value then
  begin
    FMarqueeWidth := Value;

    BuildBar;
  end;
end;

procedure TGrProgressbar.SetMarqueeSpeed(const Value: integer);
begin
  if FMarqueeSpeed <> Value then
  begin
    FMarqueeSpeed := Value;
    FMarqueeSpeed := Math.Min(100,FMarqueeSpeed);
    FMarqueeSpeed := Math.Max(1,FMarqueeSpeed);
  end;
end;

procedure TGrProgressbar.SetMax(const Value: integer);
begin
  if FMax <> Value then
  begin
    FMax := Value;
    BuildBar;
  end;
  Position := Position; 
end;

procedure TGrProgressbar.SetMin(const Value: integer);
begin
  if FMin <> Value then
  begin
    FMin := Value;
    BuildBar;
  end;
  Position := Position;
end;


procedure TGrProgressbar.SetPosition(Value: integer);
begin
  // First, limit the setted value to min and max position
  Value := Math.Min(Max, Value);
  Value := Math.Max(Min, Value);

  // Disable marquee if we write a new position
  FMarquee := false;

  if FPosition <> Value then
  begin
    FPosition := Value;
    BuildBar;
    Changed;
  end;
end;

procedure TGrProgressbar.SetUseSystemColors(const Value: boolean);
var
  ABmp : TBitmap;
  PickColor: TColor;
  FProgressThemeData : HTHEME;
begin
  FUseSystemColors := Value;

  if FUseSystemColors then
  begin
    FApplyingSystemColor := true;

    // Check if Windows Themes are enabled
    if UseThemes then
    begin

      // A small way (not really clean) to get the color of a themed progress bar
      if (SystemColor = clNone) then
      begin
        {$IfDef DEBUG}SendDebug('SystemColor is None');{$ENDIF}

        // We create a local bitmap where we painting on it a themed progress bar
        ABmp := TBitmap.Create;
        ABmp.SetSize(16,16);
        FProgressThemeData := OpenThemeData(Handle, 'Progress');
        DrawThemeBackground(FProgressThemeData, ABmp.Canvas.Handle, PP_CHUNK, 0, ABmp.Canvas.ClipRect, nil);
        CloseThemeData(FProgressThemeData);

        // After painting, we pick the color at a logical position
        // Just hope that there is a valid color here
        PickColor := ABmp.Canvas.Pixels[2,2];
        if PickColor <> CLR_INVALID then
        begin
          SystemColor := PickColor;
          {$IfDef DEBUG}SendDebug('System color found, extracted from BITMAP');{$ENDIF}
        end
          else
        begin
          SystemColor := clHighlight;
          {$IfDef DEBUG}SendDebug('System color not found, using the Highlight one');{$ENDIF}
        end;

        ABmp.Free;
      end;

      Color := SystemColor;
      {$IfDef DEBUG}SendDebug('Using previous computed system color');{$ENDIF}
    end
      else
    begin
      // If theming is not enabled then use the default highlight color
      Color := clHighlight;
      {$IfDef DEBUG}SendDebug('No theme is used, default highlight color');{$ENDIF}
    end;
    FApplyingSystemColor := false;
  end;

end;



procedure TGrProgressbar.SetColor(const Value: TColor);
var
  H,S,L : Byte;
begin
  if FColor <> Value then
  begin

    // Disable system color only if SetColor is not called by the UseSystemColors method
    if not FApplyingSystemColor then
      UseSystemColors := false;

    FColor := Value;

    // Extracting the Wincolor to Hue, Saturation and Luminosity values
    FColor1 := Color32(FColor);
    RGBtoHSL(FColor1, H, S, L);

    // Small way to set the color of the slashes
    if Intensity(FColor1) > 160
    then
      FColor2 := clBlack32
    else
      FColor2 := clWhite32;

    // Redraw the pattern with new colors
    InvalidatePattern;
  end;
  
end;

// Property that allow the flippig of the slashes
procedure TGrProgressbar.SetFlip(const Value: boolean);
begin
  FFlip := Value;
  InvalidatePattern;
end;

/// Catched Windows messages

// If the theme is changed by a new theme then invalidate the system color
// It will be recomputed by the first progress bar with UseSystemColor property enabled
procedure TGrProgressbar.CMStyleChanged(var msg: TMessage);
begin
  {$IfDef DEBUG}SendDebug('TGrProgressbar.CMStyleChanged');{$ENDIF}
  // Update colors
  SystemColor := clNone;
  FInvalidateColor := true;
end;


// Stop the timer when this control is not visible, else re-enable it
procedure TGrProgressbar.CMShowingChanged(var msg: TMessage);
begin
  inherited;
  {$IfDef DEBUG}SendDebug('CMShowingChanged');{$ENDIF}
  
  if Showing then
  begin
    if Animated and not FAniTimer.Enabled then
    begin
      FAniTimer.Enabled := true;
      {$IfDef DEBUG}SendDebugFmt('Timer of %s started',[Name]);{$ENDIF}
    end;
  end
    else
  begin
    if Animated and FAniTimer.Enabled then
    begin
      FAniTimer.Enabled := false;
      {$IfDef DEBUG}SendDebugFmt('Timer of %s stopped',[Name]);{$ENDIF}
    end;
  end;
end;

initialization
  SystemColor := clNone;

finalization


end.


 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
  •   GrProgressBar
    •   Package
      • GrComponents.dpkTélécharger ce fichier [Réservé aux membres club]596 octets
      • GrComponents.dprojTélécharger ce fichier [Réservé aux membres club]19 380 octets
      • GrComponents.resTélécharger ce fichier [Réservé aux membres club]5 940 octets
      • GrProgressBar.pasTélécharger ce fichier [Réservé aux membres club]Voir ce fichier17 449 octets
    • Exemple.dprTélécharger ce fichier [Réservé aux membres club]Voir ce fichier236 octets
    • Exemple.dprojTélécharger ce fichier [Réservé aux membres club]20 141 octets
    • Exemple.execTélécharger ce fichier [Réservé aux membres club]647 680 octets
    • Exemple.resTélécharger ce fichier [Réservé aux membres club]5 280 octets
    • Main.dfmTélécharger ce fichier [Réservé aux membres club]8 840 octets
    • Main.pasTélécharger ce fichier [Réservé aux membres club]Voir ce fichier2 298 octets

Télécharger le zip


 Historique

15 novembre 2008 01:02:45 :
Correction
17 novembre 2008 08:50:37 :
Ajout du code du composant ...
18 novembre 2008 10:01:52 :
Ajout d'un exécutable "Exemple.exec"

 Sources de la même categorie

Source avec Zip Source avec une capture EFFET VITRE ET THUMBNAILS SOUS VISTA par Bacterius
Source avec Zip Source avec une capture ANAGLYPHEUR OU COMMENT VOIR EN RELIEF LES STÉRÉOSCOPES ANCES... par jfs59
Source avec Zip Source avec une capture DÉFORMER UNE IMAGE AUX DIMENSIONS D'UN QUADRANGLE QUELCONQUE... par FFCAST
Source avec Zip Source avec une capture THREAD ET BITMAP (DESSIN AU CRAYON) par barbichette
Source avec Zip Source avec une capture ÉCRAN DE VEILLE FEU D'ARTIFICE par barbichette

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture ANIMATION AVEC PNG par Bacterius
Source avec Zip Source avec une capture AVATARS IN MADNESS par cantador
Source avec Zip Source avec une capture WINHIDER _ CACHER UN PROGRAMME DE L'ÉCRAN ET DE LA BARRE DE ... par soldier8514
Source avec Zip Source avec une capture PROGRESSBAR AVEC VSPEED par game50
Source avec Zip Source avec une capture UNE BARRE DE PROGRESSION EN RELIEF (STYLE PRODJ) par grandvizir

Commentaires et avis

Commentaire de Delphiprog le 16/11/2008 16:03:31 administrateur CS

Pas franchement de quoi publier un code-source pour illustrer l'usage de quatre propriétés d'un composant réalisé par d'autres.
Je ne commenterai pas davantage le niveau "initié"...

Commentaire de yannbobu le 17/11/2008 08:48:18

Heu! pas très sympa, sauf que l'exemple est une application du composant que j'ai réalisé qui est dans le dossier package : GrProgressBar.pas

Commentaire de Delphiprog le 17/11/2008 09:42:41 administrateur CS

Ok et désolé pour le commentaire un peu désagréable et je retire ce que j'ai écrit.
Quand je l'ai déposé, il n'était pas évident de deviner que tu en étais l'auteur.
Mais là, maintenant, je comprends mieux et, à la lecture du code du composant,  je t'adresse mes plus vives félicitations.

"Les sous-entendus sont souvent à l'origine des malentendus."

Commentaire de MAURICIO le 17/11/2008 11:17:36

Salut,

malheureusement devoir installer des compos pour pouvoir tester le tiens risque d' en rebuter plus d' un!

Par contre l' effet est très sympa et le code est très pro: j' aime beaucoup!

A+

Commentaire de yannbobu le 17/11/2008 11:37:19

En effet MAURICIO, devoir installer deux autres librairies est une contrainte, j'avais mis un .exe de l'exemple dans l'archive pour éviter cela, mais apparemment c'est automatiquement supprimé pour des raisons de sécurité.

Pour ceux qui ne veulent pas trop se casser la tête, les packages des autres librairies ne sont pas obligatoires, il suffit d'indiquer le chemin vers les sources des bibliothèques. Et pour la "GR32 Extension Components Pack", je n'utilise que l'unité SimpleTimer.

Merci pour vos commentaires :)

Commentaire de JulioDelphi le 17/11/2008 16:49:29 administrateur CS

Super beau en effet, étant fan des barres de progressions, je vais la prendre :]
merci bon boulot !

Commentaire de Nicolas___ le 18/11/2008 00:06:56

si tu veux mettre 1 exe renome le en .exec (ou n'importe quelle extension <>'exe')

En tout cas c'est très beau , très épuré , vraiment sympa

Ciao

Commentaire de Jean-Pierre le 20/11/2008 18:49:49 10/10

Salut,

Bravo et merci pour cette idée et création ; en effet c'est super sympa et très original en rendu visuel.  

Pour un peu, on en ajouterai dans nos appli, rien que pour le plaisir des mirettes ; même s'il n'y a aucun besoin en barres de progression lol !

Commentaire de offlake le 24/11/2008 12:36:04 10/10

C bien Comme idée des Barres de progressions a l'effet dévastateur
Je te donne 10/10
By Offlake

Commentaire de LALLEM2005 le 26/01/2009 13:07:54

SALUT J4AI PROBLEME POUR INSTALLATION  PAQUET GR32_BDS2006  MANQUE

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Insérer une barre de progression lors d'un chargement d'un base .... [ par burnx22 ] Je suis au Cégep en Informatique et je dois savoir absolument comment faire pour faire fonctionner une barre de progression (progress bar) lorsque j'i Barre de progression réelle [ par mighty_warrior ] Bonjour,Ca fait pas mal de temps maintenant que je bidouille un p'tit programme disposant d'une interface en 3D. Le truc c'est que le programme met un SplashScreen + barre de progression [ par cantador ] Bonjour à tous, Dans une appli, je crée un SplashScreen au démarrage(5 x 10 cm)avec une barre de progression qui s'incrémente au fur et à mesure des afficher icone dans une fiche (Tform) comme ceux de la barre d'outils [ par mighty_warrior ] Bonjours,Je sollicite votre aide car j'ai besoin de savoir comment faire pour afficher les icones des programmes dans une fiche delphi comme on les vo comment detecter qu'une entree se fait pas lecteur code barre [ par kiou9 ] salut a tousj'ai encore une question du tonnerre de Zeusj'ai un programme de gestion client-produit-activitéje voudrai pouvoir detecter le fait que j' program invisible dans la barre de tache [ par Ali_usto ] salut tout le monde voila je voudrais savoir comment faire pour rendre un programme invisible dans la barre de tache , j'ai trouvé un exemple sur le s animation copie fichier [ par sisi231 ] Bonjour, je souhaiterais reprendre l'animation de copie de fichier et de destruction de fichier qui s'affiche dans les boîtes de dialogue de SHFileOpe Dimension de l'écran avec barre à outils [ par yvessimon ] Bonjour,La taille total de l'écran est accéssible avec " screen ".Hors la barre d'outils windows peut être horizontale ou verticale et de largeur vari composant Tanimate [ par mighty_warrior ] Bonjour,Est-ce que quelqu'un pourrait me renseigné sur le composant Tanimate parce que l'aide de delphi ne m'apporte pas grand chose.D'aprés ce que j' Minimiser des forms non principales. [ par Fat ] Je ne sais pas si c'est normal, mais dans une appli en VB par exemple, quand vous creez plusieurs forms, quand vous les minimiser, elles se retrouvent


Nos sponsors


Sondage...

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,108 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales