Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
497 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


Gradient LabelFormat this article printer-friendly!Bookmark function is only available for registered users!
How to create a cute gradient label
Product:
Delphi all versions
Category:
GUI
Skill Level:
Scoring:
Last Update:
11/03/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet Gradient Label
Times Scored:
3
Visits:
3173
Uploader: Alain Gosselin
Company:
Reference: N/A
 
Question/Problem/Abstract:
How to create a gradient label as a component?
Answer:



unit AGGradientLabel;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

const
  DefaultStart = clBlue;
  DefaultEnd = clBlack;

type
  TAGGradientLabel = class(TLabel)
  private
    FclStart: TColor;
    FclEnd: TColor;
    procedure SetEndColor(const Value: TColor);
    procedure SetStartColor(const Value: TColor);
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property GradientEnd: TColor read FclEnd write SetEndColor default DefaultEnd;
    property GradientStart: TColor read FclStart write SetStartColor default DefaultStart;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Alain', [TAGGradientLabel]);
end;

{ TAGGradientLabel }

constructor TAGGradientLabel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FclStart:=DefaultStart;
  FclEnd:=DefaultEnd;
  Font.Color:=clWhite;
end;

destructor TAGGradientLabel.Destroy;
begin
  inherited Destroy;
end;

procedure TAGGradientLabel.Paint;
const
  Alignments: array[TAlignment] of Word = (DT_LEFT,DT_RIGHT,DT_CENTER);
var
  ColorStart: LongInt;
  ColorEnd  : LongInt;
  RC1       : Byte;
  GC1       : Byte;
  BC1       : Byte;
  RC2       : Byte;
  GC2       : Byte;
  BC2       : Byte;
  Rct       : TRect;
  CRct      : TRect;
  I         : Integer;
  LblSize   : Integer;
  DrawStyle : LongInt;
begin
  ColorStart:=ColorToRGB(FclStart);
  ColorEnd  :=ColorToRGB(FclEnd);
  RC1       :=GetRValue(ColorStart);
  GC1       :=GetGValue(ColorStart);
  BC1       :=GetBValue(ColorStart);
  RC2       :=GetRValue(ColorEnd);
  GC2       :=GetGValue(ColorEnd);
  BC2       :=GetBValue(ColorEnd);
  Rct       :=Canvas.ClipRect;
  LblSize   :=Rct.Right-Rct.Left;

  Canvas.Brush.Style:=bsSolid;
  for I:=0 to LblSize do
    begin
    Canvas.Brush.Color:=RGB(
                           (RC1+(((RC2-RC1)*(Rct.Left+I)) div LblSize)),
                           (GC1+(((GC2-GC1)*(Rct.Left+I)) div LblSize)),
                           (BC1+(((BC2-BC1)*(Rct.Left+I)) div LblSize))
                           );
    Canvas.FillRect(Rect(Rct.Left+I,0,Rct.Left+I+1,Height));
    end;

  Canvas.Brush.Style:=bsClear;
  DrawStyle:=(DT_EXPANDTABS or DT_WORDBREAK) or Alignments[Alignment];
  if Layout<>tlTop then
    begin
      CRct:=Rct;
      DoDrawText(CRct,DrawStyle or DT_CALCRECT);
      if Layout=tlBottom then OffsetRect(Rct,0,Height-CRct.Bottom)
      else OffsetRect(Rct,0,(Height-CRct.Bottom) div 2);
    end;
  DoDrawText(Rct,DrawStyle);
end;

procedure TAGGradientLabel.SetEndColor(const Value: TColor);
begin
  FclEnd:=Value;
  Paint;
end;

procedure TAGGradientLabel.SetStartColor(const Value: TColor);
begin
  FclStart:=Value;
  Paint;
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
slight enhancements
    Jesse Slicer (Dec 1 2001 4:06AM)

Here is an enhanced version that does a couple of things:

1) Allows for the gradient to go Left to Right, Right to Left, Top to Bottom or Bottom to Top.  The original component only gradient filled Left to Right

2) Only calls Paint on the Gradient properties if you've changed them from they already were.

unit AGGradientLabel;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TAGGradientDirection = (aggrLeftToRight, aggrRightToLeft, aggrTopToBottom,
                          aggrBottomToTop);

const
  DefaultStart = clBlue;
  DefaultEnd = clBlack;
  DefaultDirection = aggrLeftToRight;

type
  TAGGradientLabel = class(TLabel)
  private
    FclStart: TColor;
    FclEnd: TColor;
    FaggrDirection: TAGGradientDirection;
    procedure SetEndColor(const Value: TColor);
    procedure SetStartColor(const Value: TColor);
    procedure SetGradientDirection(const Value: TAGGradientDirection);
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property GradientEnd: TColor read FclEnd write SetEndColor default DefaultEnd;
    property GradientStart: TColor read FclStart write SetStartColor default DefaultStart;
    property GradientDirection: TAGGradientDirection read FaggrDirection write SetGradientDirection default DefaultDirection;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Alain', [TAGGradientLabel]);
end;

{ TAGGradientLabel }

constructor TAGGradientLabel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FclStart:=DefaultStart;
  FclEnd:=DefaultEnd;
  FaggrDirection := DefaultDirection;
  Font.Color:=clWhite;
end;

destructor TAGGradientLabel.Destroy;
begin
  inherited Destroy;
end;

procedure TAGGradientLabel.Paint;
const
  Alignments: array[TAlignment] of
Respond

RE: slight enhancements
Jesse Slicer (Dec 1 2001 4:07AM)

Shoot, got limited by 2000 characters.
Respond

RE: RE: slight enhancements
Jesse C. Slicer (Dec 10 2001 12:14AM)

Since it couldn't fit in the comment area, I've posted the file at http://www.intervocative.com/jslicer/Delphi/AGGradientLabel.pas for those of you interested.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
L. Rosenstein
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)