Mega Code Archive

 
Categories / Delphi / Graphic
 

Gradient Label

Title: Gradient Label Question: 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 LayouttlTop 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.