0
votes

I put some code that happens on a Form OnResize event. But it Seems to only call the event when the resize is bigger. Is there any way to change this?

Say i have

procedure TForm1.Resize(Sender: TObject);
begin
  RePaint();
end

I would like to run repaint every time the form is re-sized (big or small). I guess you could add a check that checks width / height but if your going to do that why even use the event resize? Maybe there is another event besides resize that does both? thanks Glen

2
The OnResize event is triggered even when you resize the form to a smaller size. - TLama
If you used Align properties, or anchors, then you would not need OnResize event. Not the OnResize doesn't always fire (it does), but there are slicker ways to adapt controls to form size. - David Heffernan
Question is based on a wrong assumption, that Resize happens only when form becomes bigger. There's no SSSCE code to debug either. - Kromster

2 Answers

1
votes

The OnResize event is triggered when the form is resized by any means, despite it is larger or smaller than before.

To prove this, create a new project, put a Memo on a clean form and add this code to the OnResize event:

procedure TForm1.FormResize(Sender: TObject);
begin
  Memo1.Lines.Add(Format('%d,%d', [Width, Height]));
end;

Execute your project, and drag an edge of the form to make it greater and smaller, you'll notice it is fired always, and how many times the event is triggered also.

Notice also the form has a OnPaint event that maybe is what you're after.

3
votes

There are two questions being asked here.

First, OnResize is triggered when the form resizes, in either direction. Period.

Second, on why Repaint does not work when the form gets smaller is probably because you do your own painting. Normally, only Canvas.Cliprect needs to be repainted. When the form gets smaller, the shown area is not changed and the clipping area is empty. After all, there is not more to paint than there was before. When the form gets bigger in one direction, a similar thing happens: the clipping area consists of only the part which is added to the form's surface. Again, the previous shown area remains intact and is not updated. Only when the form expands in both directions, the clipping area cannot be made of a single rectangle any more, and so it becomes the complete surface.

The solution for you is to signal Windows to repaint the whole contents of the form at any resize, by calling Invalidate (or Update when it has to be repainted instantly), and to perform your custom drawing in the OnPaint event.