Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement custom TGraphicControl.OnResize?

Should I use WM_WINDOWPOSCHANGED;? (I havn't seen CM_WINDOWPOSCHANGED or similar)

Will the TGraphicControl even be notified of this message (it has no Handle)? What is the correct method?

Thanks.


Just a thoght after accepting the answer:

Strangely or maybe intentionally OnResize fires even if only the Top/Left of the control position where changed:
in Delphi 7 Resize is called in TControl.SetBounds right after it calls Perform(WM_WINDOWPOSCHANGED) even if no actual resize was made and the control was moved.
Is this by design?

like image 851
Vlad Avatar asked Dec 15 '22 03:12

Vlad


2 Answers

The OnResize Event is already implemented in TControl , it's just protected. To access it you just have to redeclare it for your component. You also might use a interposer class or a "Hack" class to access it. As example for a TImage:

using it for an own component:

TMycontrol=Class(TGraphicControl)
    published
    Property OnResize;
End;

using a interposer class:

type
  TImage=Class(ExtCtrls.TImage)
   Property OnResize; 
  End;

  TForm3 = class(TForm)
//....

procedure TForm3.MyResize(Sender: TObject);
begin
  Showmessage(Sender.ClassName)
end;


procedure TForm3.Button1Click(Sender: TObject);
begin
  Image1.OnResize := MyResize;
  Image1.Width := 300;
end;

using a "hack" just in place:

implementation

{$R *.dfm}
Type THack=Class(TControl)
    Property OnResize;
End;

procedure TForm3.MyResize(Sender: TObject);
begin
  Showmessage(Sender.ClassName)
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  THack(Image1).OnResize :=  MyResize;
  Image1.Width := 300;
end;

The event is triggered by the parentcontrol iterating the contained controls in TWinControl.AlignControls.

like image 141
bummi Avatar answered Dec 27 '22 06:12

bummi


... OnResize fires even if ... no actual resize was made and the control was moved. Is this by design?

Yes. Resize does not necessarily only indicate a size change, but a change in any of the properties that defines its size, being: Left, Top, Width and Height.

Will the TGraphicControl ...

Your question implies you are designing your own TGraphicControl descendant. Then you should not only publish the already present OnResize event, but also override the Resize method, as said by TLama in his comment, and as I answered here.

like image 43
NGLN Avatar answered Dec 27 '22 07:12

NGLN