Canvas size confusion lazarus / 31-LCL ---------------------------------------------------------------------- mark.chambers · 2019-10-29 02:47 Hi Folks, I have a strange problem with Canvas on both a TPanel and a TForm - it seems to change size randomly. This is bad because I'm trying to paint onto it and I need to know how big it is. In the following output I show the location in code and width and height of the panel. The form is actually 602x349. The panel is actually 180x158. As you can see, sometimes the panel thinks it is the size of the form, sometimes the size of the panel and sometimes 0x0. FormCreate   180, 158 FormShow     180, 158 Panel1Paint  602, 349 Panel1Click  0, 0 FormClick    180, 158 Panel1Click  180, 158 Panel1Paint  602, 349 Panel1Click  0, 0 FormClick    180, 158 Panel1Paint  602, 349 I have looked into the LCL code but got lost quite quickly. Can anyone suggest what have I have done wrong or a way to ensure the panel size is reported correctly? Thanks, Mark Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Memo1: TMemo;    Panel1: TPanel;    procedure FormClick(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure FormShow(Sender: TObject);    procedure Panel1Click(Sender: TObject);    procedure Panel1Paint(Sender: TObject);  private    C: TCanvas;    procedure DoIt;    procedure Log(S: String); overload;    procedure Log(S: String; args: array of const); overload;  public   end; var  Form1: TForm1; implementation {$R *.frm} { TForm1 } procedure TForm1.Log(S: String);begin  Memo1.Lines.Add(S);end; procedure TForm1.Log(S: String; args: array of const);begin  Log(Format(S, args));end;  procedure TForm1.FormCreate(Sender: TObject);begin  Log('FormCreate');  C := Panel1.Canvas;  C.Line(10,10,300,300);  doit;end; procedure TForm1.FormClick(Sender: TObject);begin  Log('FormClick');  doit;end; procedure TForm1.Panel1Click(Sender: TObject);begin  Log('Panel1Click');  doit;end; procedure TForm1.FormShow(Sender: TObject);begin  Log('FormShow');  doit;end; procedure TForm1.Panel1Paint(Sender: TObject);begin  Log('Panel1Paint');  doit;end; procedure TForm1.Doit;begin  Log('%d, %d', [Panel1.Canvas.Width, Panel1.Canvas.Height]);  C.Line(10,10,300,300);end; end. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i object Form1: TForm1  Left = 368  Height = 349  Top = 230  Width = 602  Caption = 'Form1'  ClientHeight = 349  ClientWidth = 602  DesignTimePPI = 120  OnClick = FormClick  OnCreate = FormCreate  OnShow = FormShow  LCLVersion = '6.8'  object Panel1: TPanel    Left = 408    Height = 158    Top = 104    Width = 180    Caption = 'Panel1'    TabOrder = 0    OnClick = Panel1Click    OnPaint = Panel1Paint  end  object Memo1: TMemo    Left = 4    Height = 333    Top = 12    Width = 375    Lines.Strings = (      'Memo1'    )    TabOrder = 1  endend ---------------------------------------------------------------------- Handoko · 2019-10-29 05:43 Instead of paint on TPanel's canvas, you should use TPaintBox. ---------------------------------------------------------------------- mark.chambers · 2019-10-29 06:14 Thanks Handoko, I tried your suggestion of using a TPaintBox instead of a TPanel. The Canvas width and height are reported as either 0x0 or the Form's width and height, rather then the dimensions of the TPaintBox. So, no different.  Any other ideas? Regards, Mark ---------------------------------------------------------------------- howardpc · 2019-10-29 08:54 Try this, removing C, your independent reference to a canvas. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TForm1 = class(TForm)    Memo1: TMemo;    Panel1: TPanel;    procedure FormClick(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure FormShow(Sender: TObject);    procedure Panel1Click(Sender: TObject);    procedure Panel1Paint(Sender: TObject);  private    procedure DoIt;    procedure Log(S: String); overload;    procedure Log(S: String; args: array of const); overload;  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.FormCreate(Sender: TObject);begin  Log('FormCreate');  DoIt;end; procedure TForm1.FormClick(Sender: TObject);begin  Log('FormClick');  DoIt;end; procedure TForm1.FormShow(Sender: TObject);begin  Log('FormClick');  DoIt;end; procedure TForm1.Panel1Click(Sender: TObject);begin  Log('Panel1Click');  DoIt;end; procedure TForm1.Panel1Paint(Sender: TObject);begin  Log('Panel1Paint');  DoIt;end; procedure TForm1.DoIt;const  t: Integer = 10;begin  Panel1.Canvas.Clear;  Log('%d, %d', [Panel1.Canvas.Width, Panel1.Canvas.Height]);  Inc(t, 10);  Panel1.Canvas.Line(10,t,300,300);end; procedure TForm1.Log(S: String);begin  Memo1.Lines.Add(S);end; procedure TForm1.Log(S: String; args: array of const);begin  Memo1.Lines.Add(S, args);end; ---------------------------------------------------------------------- wp · 2019-10-29 09:17 In Delphi there is no such thing as TCanvas.Width or TCanvas.Height - the effective size of the canvas is determined by the class to which the canvas belongs, e.g. TBitmap.Width, TForm.Width, TPaintbox.Width. I know that Lazarus does provide these properties for TCanvas, but I am not sure whether the values are always reliable because the Canvas is tied to its Handle and this is usually valid only withing a painting operation. In fact, your DoIt method is drawing outside paint operations which is even prohibited in some widgetsets. ---------------------------------------------------------------------- Thaddy · 2019-10-29 09:55 Quote from: wp on October 29, 2019, 10:17:39 amIn Delphi there is no such thing as TCanvas.Width or TCanvas.Height - the effective size of the canvas is determined by the class to which the canvas belongs, e.g. TBitmap.Width, TForm.Width, TPaintbox.Width. I know that Lazarus does provide these properties for TCanvas, but I am not sure whether the values are always reliable because the Canvas is tied to its Handle and this is usually valid only withing a painting operation. In fact, your DoIt method is drawing outside paint operations which is even prohibited in some widgetsets. Not even that: the paint area is determined by ClientHeight and ClientWidth. ---------------------------------------------------------------------- kupferstecher · 2019-10-29 10:00 For self-drawing I use TCustomControl, works for me. Description here: https://wiki.lazarus.freepascal.org/Developing_with_Graphics#Create_a_ custom_control_which_draws_itself ---------------------------------------------------------------------- Thaddy · 2019-10-29 10:04 Yup. Good tip. ---------------------------------------------------------------------- mark.chambers · 2019-10-29 10:09 OK, I see. So I need to use the Canvas's owner's ClientHeight and ClientWidth, not the Height and Width on the Canvas (which sound like they shouldn't even exist). What do you mean though by "drawing outside paint operations"? ---------------------------------------------------------------------- kupferstecher · 2019-10-29 10:25 You should read the link that I postet. You can't randomly draw on the canvas, because you don't know when the OS reads from it for displaying the contents. In short: Draw on the canvas of a TBitmap and assign it to the control's canvas ONLY within the Paint-Event. ---------------------------------------------------------------------- kupferstecher · 2019-10-29 10:34 In addition: If you want to change the content, i.e. force a redraw, the you should call the control's Invalidate-Method:  AControl.Invalidate; Then the Paint-method will be called by the system, where you draw the updated contents. ---------------------------------------------------------------------- mark.chambers · 2019-10-29 11:33 Read and understood. I have a double buffered example going now. Thanks kupferstecher.