Recent

Author Topic: PaintBox vs. Canvas dimensions  (Read 361 times)

lucamar

  • Hero Member
  • *****
  • Posts: 2975
PaintBox vs. Canvas dimensions
« on: July 04, 2019, 01:18:01 pm »
Antecedents
As shown in the attached image, the Width and Height of a TPaintBox and its Canvas are different, which means this code will not do the expected:
Code: Pascal  [Select][+][-]
  1.   with PaintBox.Canvas do begin
  2.     Brush.Color := clCream;
  3.     FillRect(0, 0, Width, Height);
  4.     Frame(0, 0, Width, Height);
  5.   end;
while this one wiill do it:
Code: Pascal  [Select][+][-]
  1.   with PaintBox do begin
  2.     Canvas.Brush.Color := clCream;
  3.     Canvas.FillRect(0, 0, Width, Height);
  4.     Canvas.Frame(0, 0, Width, Height);
  5.   end;

In case it matters, the PaintBox is aligned to Client with "BorderSpacing.Around = 6". The dimensions of the canvas are the same than those of the form's client area.

Question:
Is this a known (and "won't solve") problem or should I report in the bug tracker? Or is it how it's supposed to work?

Environment
Linux-i686 3.2.0/GTK 2.24.10
Lazarus/FPC: 2.0.2/3.0.4 (vanilla gtk install)
« Last Edit: July 04, 2019, 01:21:00 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 7390
Re: PaintBox vs. Canvas dimensions
« Reply #1 on: July 04, 2019, 01:35:09 pm »
As shown in the attached image, the Width and Height of a TPaintBox and its Canvas are different, which means this code will not do the expected:
Code: Pascal  [Select][+][-]
  1.   with PaintBox.Canvas do begin
  2.     Brush.Color := clCream;
  3.     FillRect(0, 0, Width, Height);
  4.     Frame(0, 0, Width, Height);
  5.   end;
I always thought that the Canvas is infinite, it does not have neither a Width nor a Height. In this case, Width and Height in above code would refer to the parameters of the Form ( a dangerous usage of the "with" syntax BTW).

But I am wrong, there are properties TCanvas.Width and TCanvas.Height indeed. Peeking into the source code shows
Code: Pascal  [Select][+][-]
  1. function TCanvas.GetWidth: integer;
  2. var
  3.   p: TPoint;
  4. begin
  5.   if HandleAllocated then begin
  6.     GetDeviceSize(Handle,p);
  7.     Result:=p.x;
  8.   end else
  9.     Result:=0;
  10. end;
and GetDeviceSize leads into a widgetset-related method. In case of Windows, this calls "WindowFromDC" which in fact seems to do what I suspected above: it links the extent of the canvas to the exent of the form. Of course, every widgetset may handle this case differently. So, for device-independent code, I would refrain from referring to Canvas.Width and Canvas.Height, like I always did.

Just my 2 cents...
« Last Edit: July 04, 2019, 01:54:09 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

lucamar

  • Hero Member
  • *****
  • Posts: 2975
Re: PaintBox vs. Canvas dimensions
« Reply #2 on: July 04, 2019, 01:46:13 pm »
Yes, as I said the PaintBox' Canvas is getting the dimensions either of the form client area or of the paintbox before applying BorderSpacing.

I suspect the former, rather than the later, because TPaintBox is a TGraphicControl.

The question is: Is this already known and as should be or a bug?

BTW: Yes, it's a rather dangerous use of "with" but it's just a small test and I was in something of a hurry :-[
And I checked it did what it was suposed to do ...
(famous last words, huh? :) )
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018