Paintbox stretches in scroll box

I put a paintbox in a scrollbox, having the same size as the scrollbox. When I then draw in the paintbox and beyond the size of the scrollbox, To get the whole drawing, I have to adapt the paintbox size (width and/or height) at runtime, which is OK.

However, when setting back the paintbox size to the scrollbox size before starting a next drawing, no longer produces a complete drawing. Instead, it only draws the surface of the scrollbox and then stretches that up to the actual drawing surface.

So, once a paintbox becomes larger than a scrollbox, it is not possible to start with an original surface.

The example below is code for a scrollbox+paintbox+button.

Attached is the complete example with alternative tries:
PaintboxExample.zip (7.3 KB)

Is there a proper way to adapt the size of a paintbox?

Kind regards,

Michel Huybrechts
Micriconsult BV

procedure TForm1.WebFormShow(Sender: TObject);
begin
v_CanPaint1 := False;
end;

procedure TForm1.WebButton1Click(Sender: TObject);
begin
v_CanPaint1 := True;
WebPaintBox1Paint(WebPaintBox1)
end;

procedure TForm1.WebPaintBox1Paint(Sender: TObject);
{
Works correctly, but only the first time. The next paint stretches the
visible part up to the calculated height???
}
begin { TForm1.WebPaintBox1Paint }
if v_CanPaint1
then
begin
{ Reset the height }
WebPaintBox1.Height := WebScrollBox1.Height;
WebPaintBox1.Canvas.Clear;
WebPaintBox1.Canvas.MoveTo(0, 0);
WebPaintBox1.Canvas.LineTo(150, 150);
WebPaintBox1.Canvas.MoveTo(100, 1000);
WebPaintBox1.Canvas.LineTo(100, 100);
WebPaintBox1.Canvas.TextOut(30, 10, 'Visible text');
WebPaintBox1.Canvas.TextOut(30, 500, 'Invisible text');
{ Set the height: does not work the second time. Drawing is stretched }
WebPaintBox1.Height := 1000
end
end { TForm1.WebPaintBox1Paint };

It is not a good idea to change the size of a control during its painting.
Normally, a change of size would need to force a repaint and you risk to have an endless loop.
Instead here you see the canvas as-is just being scaled.

I recommend to move size changing out of painting code.

Dear Sir,

Indeed. This solves the problem and also prevents useless calls to the paint.

Thank you for this answer.

Kind regards,

Michel Huybrechts

Micriconsult BV

www.micriconsult.be