Search code examples
delphicanvasvcl

Forcing a Delphi form to draw when its not visible


I have a form that I scrape a bitmap to send to a small embedded TFT display. I then inject touch events from the display into the form to activate the controls. This all works very well unless the form is not visible. If its moved off the visible desktop, minimized or closed it will not get a paint event and never updates.

Is there a way to force the canvas to redraw itself visible or not? All of the obvious things like called repaint does not work.


Solution

  • Yes you can use the PaintTo method on a form:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Bitmap: TBitmap;
    begin
      Bitmap := TBitmap.Create;
      Bitmap.Width := Form2.Width;
      Bitmap.Height := Form2.Height;
    
      Form2.PaintTo(Bitmap.Canvas, 0, 0);
      Image1.Picture.Assign(Bitmap);
      Bitmap.Free;
    end;
    

    Im my small example I made a project with two forms Form1 and Form2. On Form2 i placed a label and Timer.

    Here's the code for Form2

    procedure TForm2.Timer1Timer(Sender: TObject);
    begin
      Label1.Caption := FloatToStr(now);
    end;
    

    And i woks out well.