views:

88

answers:

2

I'm building a custom control, and I need it to be able to respond when it gets resized. I need the old dimensions and the new dimensions available in order to do some calculations.

Unfortunately, the SetWidth and SetHeight methods are private to TControl, not protected, and so I can't override them. Is there any other way to know that my control's about to be resized, and to have the old size and the new size both available?

+7  A: 

Override the SetBounds public method. It passes in the new size, and you can use the Width and Height properties to get the current width/height.

procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
Jon Benedicto
A: 

An alternative solution, would simply be to use the OnResize event of TControl. This is not necessarily published in some controls but if it isnt you can still attach a handler in code.

MyControl.OnResize := MyResizeEvent;

Although this will only allow you to get the new size, if you have kept the oldsize in a set of variables then you can do what you wish.

function Myform.MyResizeEvent(Sender: TObject) ;
var

begin

  DoSomethingOnResize(OldHeight, OldWidth, (Sender as TControl).Height,(Sender as TControl).Width);
  OldHeight := (Sender as TControl).Height;
  OldWidth := (Sender as TControl).Width;

end;
Toby Allen
C'mon. He's building a custom control. It should work on any parent, not just on Myform.
TOndrej
-1. When writing a control, your own control's event handlers are off-limits. Event handlers are for consumers of the control, not authors. If you want the equivalent of handling that event, then override the method that triggers that event. In this case, override `TControl.Resize`.
Rob Kennedy
Sorry I missed the bit that he was writing a custom control, I thought he was just looking to get details of when a control was resized.
Toby Allen