Mega Code Archive

 
Categories / Delphi / Examples
 

The best way to emulate the Size tool at run time

Title: The best way to emulate the "Size" tool at run-time Question: One of my last articles was a unit of routines givin the ability to resize the controls at run-time like using the "Size" tool in the Delphi IDE at design time. This simple routine is best, it can differentiate width and height and is only one for all actions. Answer: Type TSizeType = ( stNoChange, stSmallest, stLargest, stConstant ); Procedure SizeCtrls( Ctrls : Array Of TControl; HeightType : TSizeType = stNoChange; WidthType : TSizeType = stNoChange; HeightConst : Integer = -1; WidthConst : Integer = -1 ); Var IdX : Integer; Cnt : Integer; Begin // Only one or no controls given. If ( Low( Ctrls ) = High( Ctrls ) ) Then Exit; // Costant width and height. If ( HeightConst = -1 ) Then HeightConst := TControl( Low( Ctrls ) ).Height; If ( WidthConst = -1 ) Then WidthConst := TControl( Low( Ctrls ) ).Width; // Height size. If ( Not( HeightType = stNoChange ) ) Then Begin // Finds the height to use. Cnt := TControl( Ctrls[ Low( Ctrls ) ] ).Height; Case ( HeightType ) Of stSmallest : For IdX := Low( Ctrls ) To High( Ctrls ) Do If ( TControl( Ctrls[ IdX ] ).Height Cnt := TControl( Ctrls[ IdX ] ).Height; stLargest : For IdX := Low( Ctrls ) To High( Ctrls ) Do If ( TControl( Ctrls[ IdX ] ).Height Cnt ) Then Cnt := TControl( Ctrls[ IdX ] ).Height; stConstant : Cnt := HeightConst; End; // Applyes changes. For IdX := Low( Ctrls ) To High( Ctrls ) Do TControl( Ctrls[ IdX ] ).Height := Cnt; End; // Width size. If ( Not( WidthType = stNoChange ) ) Then Begin // Finds the width to use. Cnt := TControl( Ctrls[ Low( Ctrls ) ] ).Width; Case ( WidthType ) Of stSmallest : For IdX := Low( Ctrls ) To High( Ctrls ) Do If ( TControl( Ctrls[ IdX ] ).Width Cnt := TControl( Ctrls[ IdX ] ).Width; stLargest : For IdX := Low( Ctrls ) To High( Ctrls ) Do If ( TControl( Ctrls[ IdX ] ).Width Cnt ) Then Cnt := TControl( Ctrls[ IdX ] ).Width; stConstant : Cnt := WidthConst; End; // Applyes changes. For IdX := Low( Ctrls ) To High( Ctrls ) Do TControl( Ctrls[ IdX ] ).Width := Cnt; End; End; Use that like this: SizeCtrls( [ Button1, Button2, Button3 ], stLargest, stConstant, -1, 150 );