I have inherited a control from TPanel and in the Paint event handler, I have drawn the entire client rect using a gradient. This works perfectly fine until the user resizes. When the panel is resized, the panel component flickers too much.
How can i avoid this flicker. I saw the gradients in MS office 2007, even if we resize the client area, there will not be a flicker. Please enlighten me on this.
Thanks in anticipation
You may want to look at this question How to eliminate the flicker on the right edge of TPaintBox (for example when resizing)
Good overview of options to avoid flicker and also for TPanel.
Edit : I made a quick test in my Delphi XE version on windows 7.
With this code I cannot reproduce any flicker. The inherited Paint is removed and the Paint routine is quite fast.
If you still can see flicker, the proposal from Simon can be implemented, but better keep the bitmap created for the lifetime of the component itself.
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TGradientPanel = class(TPanel)
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
end;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
sPanel : TGradientPanel;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Uses Math;
procedure GradVertical(Canvas:TCanvas; Rect:TRect; FromColor, ToColor:TColor) ;
var
Y:integer;
dr,dg,db:Extended;
C1,C2:TColor;
r1,r2,g1,g2,b1,b2:Byte;
R,G,B:Byte;
cnt:Integer;
begin
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) / Rect.Bottom-Rect.Top;
dg := (G2-G1) / Rect.Bottom-Rect.Top;
db := (B2-B1) / Rect.Bottom-Rect.Top;
cnt := 0;
for Y := Rect.Top to Rect.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(Rect.Left,Y) ;
Canvas.LineTo(Rect.Right,Y) ;
Inc(cnt) ;
end;
end;
constructor TGradientPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Self.ParentBackground := FALSE;
end;
procedure TGradientPanel.Paint;
var
rect : TRect;
begin
//inherited; // Avoid any inherited paint actions as they may clear the panel background
rect := GetClientRect;
GradVertical( Self.Canvas, rect, clBlue, clRed);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
sPanel := TGradientPanel.Create( Self);
sPanel.Parent := Self;
sPanel.Top := 10;
sPanel.Left := 10;
sPanel.Width := 300;
sPanel.Height := 300;
sPanel.Anchors := [akLeft,akRight,akTop,akBottom];
sPanel.Enabled := TRUE;
sPanel.Visible := TRUE;
end;
end.