Recent

Author Topic: canvas size  (Read 630 times)

speter

  • Sr. Member
  • ****
  • Posts: 302
canvas size
« on: February 01, 2023, 05:59:57 am »
Is there a more elegant way to work out a canvas' size!?

It the attached project I am using:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.drawPattern(ac: Tcanvas; msg : string);
  2. var
  3.   sz : tpoint;
  4. begin
  5.     with ac.ClipRect do
  6.       sz := point(width,height);
  7.    // ...
  8. end;

which works; but I was wondering if there is a better way!?

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

howardpc

  • Hero Member
  • *****
  • Posts: 4121
Re: canvas size
« Reply #1 on: February 01, 2023, 09:04:49 am »
Your code does not seem inelegant to me if you are not using the canvas's Width and Height directly.
« Last Edit: February 01, 2023, 09:08:23 am by howardpc »

speter

  • Sr. Member
  • ****
  • Posts: 302
Re: canvas size
« Reply #2 on: February 01, 2023, 11:15:36 am »
Your code does not seem inelegant to me if you are not using the canvas's Width and Height directly.
Thanks howardpc.

I found (using Lazarus 2.2.0 and Windows 11) that canvas.width & .height are not reliable; hence the work-around.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

wp

  • Hero Member
  • *****
  • Posts: 10657
Re: canvas size
« Reply #3 on: February 01, 2023, 11:54:05 am »
I found (using Lazarus 2.2.0 and Windows 11) that canvas.width & .height are not reliable; hence the work-around.
ClipRect is nor reliable either, you can change it at any time:
Code: Pascal  [Select][+][-]
  1. // Add a button to call Paintbox1.Invalidate. With every button click the ClipRect will be different.
  2. procedure TForm1.PaintBox1Paint(Sender: TObject);  
  3. var
  4.   R: TRect;
  5. begin
  6.   R := Rect(Random(Paintbox1.Width), Random(Paintbox1.Height), Random(Paintbox1.Width), Random(Paintbox1.Height));
  7.   Paintbox1.Canvas.ClipRect := R;
  8.   Paintbox1.Canvas.Brush.Color := Random($FFFFFF);
  9.   Paintbox1.Canvas.FillRect(R);
  10. end;
« Last Edit: February 01, 2023, 01:55:20 pm by wp »

speter

  • Sr. Member
  • ****
  • Posts: 302
Re: canvas size
« Reply #4 on: February 03, 2023, 05:01:05 am »
Thanks wp,

I will make sure that I don't change canvas.cliprect (if I'm using it to calc the canvas' size).

I've attached a edited version of my previous project; this version includes the container's width/height and the canvas' width/height.

I would be interested in hearing if the canvas' width/height is correct (that is it matches the container's size), in any OS.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

 

TinyPortal © 2005-2018