Recent

Author Topic: Canvas Not Painting On Demand  (Read 1243 times)

Dan3468298

  • Full Member
  • ***
  • Posts: 131
Canvas Not Painting On Demand
« on: July 17, 2020, 08:47:40 pm »
My understanding is that all canvas paint-related code should go in its OnPaint event since the OS may call a refresh at any time (for example, if another window is moved).  I cannot get a TPanel to refresh so I must be missing something.  Here's the code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls ;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Edit1: TEdit;
  16.     Label1: TLabel;
  17.     Panel1: TPanel;
  18.     procedure Edit1Change(Sender: TObject);
  19.     procedure Panel1Paint(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Edit1Change(Sender: TObject);
  36. begin
  37.   Label1.Caption := Edit1.Text;
  38.   Panel1.Canvas.Refresh; //trigger a canvas OnPaint event
  39. end;
  40.  
  41. procedure TForm1.Panel1Paint(Sender: TObject);
  42. begin
  43.   Panel1.Canvas.Font.Size := 10;
  44.   Panel1.Canvas.Pen.Color := clBlack;
  45.   Panel1.Canvas.Brush.Style := bsClear;
  46.   Panel1.Canvas.TextOut(20, 20, Edit1.Text);
  47.   Panel1.Canvas.Refresh;
  48. end;
  49.  
  50. end.
  51.  
  52.  
« Last Edit: July 17, 2020, 08:56:57 pm by Dan3468298 »
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Canvas Not Painting On Demand
« Reply #1 on: July 17, 2020, 09:09:55 pm »
Try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. begin
  3.   Label1.Caption := Edit1.Text;
  4.   Panel1.Invalidate;
  5. end;
  6.  
  7. procedure TForm1.Panel1Paint(Sender: TObject);
  8. begin
  9.   with Panel1.Canvas do
  10.     begin
  11.       if Font.Size <> 10 then
  12.         Font.Size := 10;
  13.       if Pen.Color <> clBlack then
  14.         Pen.Color := clBlack;
  15.       if Brush.Style <> bsClear then
  16.         Brush.Style := bsClear;
  17.       TextOut(20, 20, Edit1.Text);
  18.     end;
  19. end;

Dan3468298

  • Full Member
  • ***
  • Posts: 131
Re: Canvas Not Painting On Demand
« Reply #2 on: July 17, 2020, 09:38:55 pm »
My aim is to have the canvas repaint on every Edit1 Change event.   Is there a way to do this?   Thx
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Canvas Not Painting On Demand
« Reply #3 on: July 17, 2020, 10:01:17 pm »
I tested the above code on Linux, and the panel is repainted at every Edit1 OnChange event.
If this is not happening on MacOS, then you need to file a bug report.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Canvas Not Painting On Demand
« Reply #4 on: July 17, 2020, 10:14:47 pm »
Maybe you don't understand what "Invalidate" means. I agree, it is a strange word... It means that the screen display of the control is no longer valid, a repaint command is put into the message queue of the OS and the control is repainted the next time when the program is idle.

There are other direct painting methods (Control.Refresh, Control.Refresh) but Invalidate has the advantage that only the last drawing command is executed if several of them would have found their way into the message queue (and this happens quite often).

So the rule is: whenever you want a control to be redrawn call its Invalidate method.

Dan3468298

  • Full Member
  • ***
  • Posts: 131
Re: Canvas Not Painting On Demand
« Reply #5 on: July 17, 2020, 11:34:07 pm »
On second try it works now. thanks.  Strange name "Invalidate" I thought it was a joke to remove that line!
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Canvas Not Painting On Demand
« Reply #6 on: July 18, 2020, 12:51:00 am »
For jokes there is Java.
Pascal is serious!

Winni

 

TinyPortal © 2005-2018