Question

I'm having problems resizing a TPaintBox and drawing on it:

On my Form (named FMain) I dropped a TPaintBox (named DisplayImage), which I am trying to resize as the Form is resized.

To do that I wrote an OnResize() method for FMain (I confirmed that it's being called correctly) in which I try to resize the DisplayImage:

procedure TFMain.FormResize(Sender: TObject);
begin
   DisplayImage.Width := FMain.ClientWidth;
   DisplayImage.Height := FMain.ClientHeight;

   DisplayImage.Canvas.Brush.Color := clGreen;
   DisplayImage.Canvas.Brush.Style := bsSolid;
   DisplayImage.Canvas.Rectangle(0, 0, DisplayImage.Width, DisplayImage.Height);
end;

IMHO the last code should draw a full-image green rectangle over the complete image, making it effectively always green. Instead I get a grey image (just like the standard bg-color of Delphi) and every once in a while during resize for a split second the green image flashes up.

What am I missing, is there some hidden component I need to update after resizing?

Thank you in advance,

BliZZarD

Was it helpful?

Solution

First of all, instead of doing

DisplayImage.Width := FMain.ClientWidth;
DisplayImage.Height := FMain.ClientHeight;

on each resize, simply set Align := alClient of the paint box.

Secondly, to draw to the paint box, use the OnPaint event of the paint box:

procedure TFMain.DisplayImagePaint(Sender: TObject);
begin
   DisplayImage.Canvas.Brush.Color := clGreen;
   DisplayImage.Canvas.Brush.Style := bsSolid;
   DisplayImage.Canvas.Rectangle(0, 0, DisplayImage.Width, DisplayImage.Height)
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top