2

I have a Delphi application with a couple of forms. For each form I used a TImage for a background, to give it a different look from the standard form look. The TImage is aligned to the client. The problem I have is that every time I click a button to open a new form, the form flickers. I do create the forms with the

frmSomeForm : TmfrSomeForm.create(self);
frmSomeForm.ShowModal;
frmSomeForm.Free;

I have tried to use the following code in the onCreate of the forms

DoubleBuffered := true;

but that does not seem to solve the problem, plus if I do use the dubbelbuffered then it makes my groupboxes that appear on my form to not be transparent. Can anyone please help.

I'm using Delphi XE2

Japster
  • 985
  • 6
  • 19
  • 38
  • How about you give up on TImage and paint the background in response to WM_ERASEBACKGROUND? As an aside why don't you use try finally? – David Heffernan Feb 25 '14 at 19:32
  • I would typically use the Form's `OnPaint` event instead of intercepting the `WM_ERASEBKGND` message. – Remy Lebeau Feb 25 '14 at 19:39
  • You can always try and set the ControlStyle = ControlStyle+ [csOpaque] at the Form's **OnPaint** procedure. – Filipe.Fonseca Feb 25 '14 at 19:39
  • @DavidHeffernan I have no idea on how to do that, how to paint the background onto the form with a image in either jpg or png format – Japster Feb 25 '14 at 19:40
  • @Remy That is still susceptible to flicker. Not sure why you wouldn't paint the background in WM_ERASEBACKGROUND. That's what it is for. – David Heffernan Feb 25 '14 at 19:41
  • 1
    possible duplicate of [Tile/Center image in the forms background](http://stackoverflow.com/questions/15132326/tile-center-image-in-the-forms-background) – David Heffernan Feb 25 '14 at 19:43
  • I would definitely advise you to avoid DoubleBuffered where possible and never to use it on a form. – David Heffernan Feb 25 '14 at 19:46
  • @DavidHeffernan I did not even know you can paint an image on a form using the WM_ERASEBACKGROUND. I will start looking into this as the flicker with the TImage makes the application look bad – Japster Feb 25 '14 at 19:47
  • Use the code from the near dupe I linked to. It is flicker free. – David Heffernan Feb 25 '14 at 19:50
  • @DavidHeffernan Thanks David I will try this solution tomorrow morning, it is already late here in South Africa :-) – Japster Feb 25 '14 at 19:58

1 Answers1

2

Catch the WM_ERASEBKG message and make it do nothing. Below code example should work. Windows fires a WM_ERASEBKG event to the window before WM_PAINT, and the default behaviour is 'clear' the form with clBtnFace (or clWindow depending on Windows version and/or theming). By causing WM_ERAGEBKG to do nothing (in advance of the WM_PAINT) the flicker should be avoided.

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  protected
    procedure WMEraseBkgnd(var Message: TWMEraseBkgnd);
      message WM_ERASEBKGND;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
  Message.Result:=0;
end;
Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67