Taazz: right again - there is an ugly black area at the right/bottom if EraseBackground is overridden by an empty method and a client-aligned gradient panel is resized. There almost no flicker on my system (Win7, i5), though, if the EraseBackground is not used. No difference between the OP's gradient painting and the canvas' GradientFill. But I don't see any flicker if the gradient panel is double-buffered.
This is my code (just an empty form) which does not cause flicker on my system (other variants commented):
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, LCLType;
type
TGradientPanel = class(TPanel)
protected
//procedure EraseBackground(DC: HDC); override;
public
FromColor, ToColor: TColor;
procedure Paint; override;
end;
{ TForm1 }
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ private declarations }
gradientPanel: TGradientPanel;
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
uses
LclIntf, Math;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
gradientPanel := TGradientPanel.create(self);
gradientPanel.Parent := self;
gradientPanel.Align := alClient;
gradientPanel.FromColor := clWhite;
gradientPanel.ToColor := clMoneyGreen;
gradientPanel.DoubleBuffered := true;
end;
procedure TGradientPanel.Paint;
var
R: TRect;
begin
R := Rect(0, 0, Width-1, Height-1);
Canvas.GradientFill(R, FromColor, ToColor, gdVertical);
end;
(* // OP's painting method
procedure TGradientPanel.Paint;
var
Y:integer;
dr,dg,db:Extended;
C1,C2:TColor;
r1,r2,g1,g2,b1,b2:Byte;
R,G,B:Byte;
cnt:integer;
rct: TRect;
begin
rct := Rect(0, 0, Width-1, Height-1);
C1 := FromColor;
R1 := GetRValue(C1);
G1 := GetGValue(C1);
B1 := GetBValue(C1);
C2 := ToColor;
R2 := GetRValue(C2);
G2 := GetGValue(C2);
B2 := GetBValue(C2);
dr := (R2-R1) / rct.Bottom-rct.Top;
dg := (G2-G1) / rct.Bottom-rct.Top;
db := (B2-B1) / rct.Bottom-rct.Top;
cnt := 0;
for Y := rct.Top to rct.Bottom-1 do
begin
R := R1+Ceil(dr*cnt);
G := G1+Ceil(dg*cnt);
B := B1+Ceil(db*cnt);
Canvas.Pen.Color := RGB(R,G,B);
Canvas.MoveTo(rct.Left,Y);
Canvas.LineTo(rct.Right,Y);
inc(cnt) ;
end;
end;
*)
(*
// black areas if this procedure is present, but no flicker. Occasional flicker of "large" gradient
// if this method is commented.
procedure TGradientPanel.EraseBackground(DC: HDC);
begin
//
end;
*)
end.