Reputation: 841
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
Upvotes: 1
Views: 3059
Reputation: 34919
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.
Upvotes: 3
Reputation: 9425
A way to reduce flicker is to draw the gradient to a temporary bitmap the draw the entire contents of the bitmap to the panel. This method assumes you have an OnPaint method and a canvas to draw on in your inherited panel.
So something like this (untested)
var bmp : Tbitmap;
procedure AfterConstruction;
begin
bmp := TBitmap.Create;
end;
procedure Destroy()
begin
if Assigned(bmp) then FreeandNil(bmp);
end;
//redraw you bmp gradient
procedure Panel1.OnResize();
begin
if Assigned(bmp) then //ensure the bmp s created in your constructor
begin
try
bmp.SetBounds(Panel1.Clientrect); //ensure the bmp is the same size as the panel
//draw your gradient on the bmp
Panel1.OnPaint();//repaint the tpanel
finally
bmp.Free;
end;
end;
//paint to the panel
procedure Panel1.OnPaint()
begin
Panel1.Canvas.Draw(0,0,bmp); //use the OnPaint method to draw to the canvas
end;
end;
end;
Upvotes: 0