Recent

Author Topic: TForm.OnResize frozes Canvas  (Read 2530 times)

Marska

  • Newbie
  • Posts: 2
TForm.OnResize frozes Canvas
« on: April 04, 2017, 06:19:19 pm »
Lazarus in Raspberry Pi 3
Raspbian Jessie, Lazarus 1.7, FPC 3.1.1

If you determine OnResize event to procedure which uses canvas drawing functions it disables other procedures to show canvas drawings. 

Here's a test code where there is meant to show second counter on canvas. Timer is triggered every 1000 ms. Nothing appears in the canvas while the TForm1.FormResize is coded to do something canvas drawing. If you remove OnResize - FormResize binding the counter is showing normally.

The same source works properly in Windows 64 PC with Lazarus 1.2.6 / FPC 2.6.4

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. begin
  3.   Inc(Cnt);
  4.   FormPaint(Sender);
  5. end;
  6.  
  7. procedure TForm1.FormPaint(Sender: TObject);
  8. begin
  9.   Canvas.Brush.Color := clDefault;
  10.   Canvas.Font.Color := clBlack;
  11.   Canvas.FillRect(20, 20, 40, 40);
  12.   Canvas.TextOut(20, 20, IntToStr(Cnt));
  13. end;
  14.  
  15. procedure TForm1.FormResize(Sender: TObject);
  16. begin
  17.   Canvas.Pen.Color := clBlack;
  18.   Canvas.Line(10, 10, 100, 100);
  19. end;
  20.  

Marska

  • Newbie
  • Posts: 2
Re: TForm.OnResize frozes Canvas
« Reply #1 on: April 07, 2017, 12:21:05 pm »
Here more reduced example.
OnTimer is triggered every 1 second.
The aim is to show incrementing Cnt to display its value.
It works fine until you need to use OnResize to do something canvas drawing with FormResize procedure.
Then the canvas remains blank.

As example there is the line Canvas.Line(10, 10, 100, 100) in the FormResize procedure.
If it is commented out the counter begins to display again.

This problem appears with Lazarus in Raspberry Pi not in Windows.

In RPi it seems that if you use any canvas functions in FormResize  it not only refuses to work but also prevent all other canvas drawings to display.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Graphics, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Timer1: TTimer;
  16.     procedure FormResize(Sender: TObject);
  17.     procedure Timer1Timer(Sender: TObject);
  18.   private
  19.     Cnt: Integer;
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.Timer1Timer(Sender: TObject);
  34. begin
  35.   Inc(Cnt);
  36.   Canvas.Brush.Color := clDefault;
  37.   Canvas.Font.Color := clBlack;
  38.   Canvas.FillRect(20, 20, 40, 40);
  39.   Canvas.TextOut(20, 20, IntToStr(Cnt));
  40. end;
  41.  
  42. procedure TForm1.FormResize(Sender: TObject);
  43. begin
  44.   Canvas.Pen.Color := clBlack;
  45.   Canvas.Line(10, 10, 100, 100);
  46. end;
  47.  
  48. end.
  49.  

Doesn't work: Raspberry Pi 3 Raspbian Jessie, Lazarus 1.7, FPC 3.1.1
Works: Windows 64, Lazarus 1.6.4, FPC 3.0.2

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: TForm.OnResize frozes Canvas
« Reply #2 on: April 07, 2017, 12:57:17 pm »
Painting should only be done in the OnPaint event. Period.

If drawing of the line is included in the OnPaint event handler, an OnResize handler is not needed at all because the form will internally be forced to repaint itself when its size changes.

BTW, in your onTimer event you enforce painting of the form by calling the FormPaint event handler. The more general way would be to delegate the task to the OS by calling Invalidate. This puts the request for repainting the form into the message loop of the OS, and the OS will take care of it.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. begin
  3.   Inc(Cnt);
  4.   Invalidate;
  5. end;
  6.  
  7. procedure TForm1.FormPaint(Sender: TObject);
  8. begin
  9.   Canvas.Brush.Color := clDefault;
  10.   Canvas.Font.Color := clBlack;
  11.   Canvas.FillRect(20, 20, 40, 40);
  12.   Canvas.TextOut(20, 20, IntToStr(Cnt));
  13.  
  14.   Canvas.Pen.Color := clBlack;
  15.   Canvas.Line(10, 10, 100, 100);
  16. end;  

 

TinyPortal © 2005-2018