Mega Code Archive

 
Categories / Delphi / Examples
 

The best way to emulate the Align tool at run time

Title: The best way to emulate the "Align" tool at run-time Question: One of my last articles was a unit of routines givin the ability to align the controls at run-time like using the "Align" tool in the Delphi IDE at design time. This simple routine is best, it can differentiate horizontal and vertical and is only one for all actions. The space equally is still in beta... sorry. Answer: Type THorizontalAlign = ( haNoChange, haLeftSides, haCenters, haRightSides, haSpaceEqually, haCenterInWindow ); TVerticalAlign = ( vaNoChange, vaTops, vaCenters, vaBottoms, vaSpaceEqually, vaCenterInWindow ); Procedure AlignCtrls( Ctrls : Array Of TControl; Hor : THorizontalAlign = haNoChange; Vert : TVerticalAlign = vaNoChange ); Var IdX : Integer; TopS : Integer; LeftS : Integer; Center : Integer; RightS : Integer; MinTop : Integer; MaxTop : Integer; StepTop : Integer; BottomS : Integer; MinLeft : Integer; MaxLeft : Integer; StepLeft : Integer; Begin // Only one, or no controls selected. If ( Low( Ctrls ) = High( Ctrls ) ) Then Exit; If ( Not( Hor = haNoChange ) ) Then Begin Case ( Hor ) Of haLeftSides : Begin // Finds the left side of the first selected control. LeftS := TControl( Ctrls[ Low( Ctrls ) ] ).Left; // Applyes that. For IdX := Low( Ctrls ) To High( Ctrls ) Do TControl( Ctrls[ IdX ] ).Left := LeftS; End; haCenters : Begin // Finds the center of the first selected control. Center := TControl( Ctrls[ Low( Ctrls ) ] ).Left + ( TControl( Ctrls[ Low( Ctrls ) ] ).Width Div 2 ); // Applyes that. For IdX := Low( Ctrls ) To High( Ctrls ) Do TControl( Ctrls[ IdX ] ).Left := Center - ( TControl( Ctrls[ IdX ] ).Width Div 2 ); End; haRightSides : Begin // Finds the right side of the first selected control. RightS := TControl( Ctrls[ Low( Ctrls ) ] ).Left + TControl( Ctrls[ Low( Ctrls ) ] ).Width; // Applyes that. For IdX := Low( Ctrls ) To High( Ctrls ) Do TControl( Ctrls[ IdX ] ).Left := RightS - TControl( Ctrls[ IdX ] ).Width; End; haSpaceEqually : Begin // Findst the minimum and the maximum left value. MinLeft := TControl( Ctrls[ Low( Ctrls ) ] ).Left; For IdX := Low( Ctrls ) To High( Ctrls ) Do If ( TControl( Ctrls[ IdX ] ).Left MinLeft := TControl( Ctrls[ IdX ] ).Left; MaxLeft := TControl( Ctrls[ High( Ctrls ) ] ).Left; For IdX := Low( Ctrls ) To High( Ctrls ) Do If ( TControl( Ctrls[ IdX ] ).Left MaxLeft ) Then MaxLeft := TControl( Ctrls[ IdX ] ).Left; // Applyes that. StepLeft := ( MaxLeft - MinLeft ) Div ( High( Ctrls ) - Low( Ctrls ) ); For IdX := Low( Ctrls ) To High( Ctrls ) Do TControl( Ctrls[ IdX ] ).Left := MinLeft + ( StepLeft * IdX ); End; haCenterInWindow : Begin // Finds the center of the window. If ( TControl( Ctrls[ Low( Ctrls ) ] ).Parent Is TForm ) Then Center := TForm( TControl( Ctrls[ Low( Ctrls ) ] ).Parent ).Width Div 2; // Applyes that. For IdX := Low( Ctrls ) To High( Ctrls ) Do TControl( Ctrls[ IdX ] ).Left := Center - ( TControl( Ctrls[ IdX ] ).Width Div 2 ); End; End; End; If ( Not( Vert = vaNoChange ) ) Then Begin Case ( Vert ) Of vaTops : Begin // Finds the top side of the first selected control. TopS := TControl( Ctrls[ Low( Ctrls ) ] ).Top; // Applyes that. For IdX := Low( Ctrls ) To High( Ctrls ) Do TControl( Ctrls[ IdX ] ).Top := TopS; End; vaCenters : Begin // Finds the center of the first selected control. Center := TControl( Ctrls[ Low( Ctrls ) ] ).Top + ( TControl( Ctrls[ Low( Ctrls ) ] ).Height Div 2 ); // Applyes that. For IdX := Low( Ctrls ) To High( Ctrls ) Do TControl( Ctrls[ IdX ] ).Top := Center - ( TControl( Ctrls[ IdX ] ).Height Div 2 ); End; vaBottoms : Begin // Finds the bottom side of the first selected control. BottomS := TControl( Ctrls[ Low( Ctrls ) ] ).Top + TControl( Ctrls[ Low( Ctrls ) ] ).Height; // Applyes that. For IdX := Low( Ctrls ) To High( Ctrls ) Do TControl( Ctrls[ IdX ] ).Top := BottomS - TControl( Ctrls[ IdX ] ).Height; End; vaSpaceEqually : Begin // Finds the minimum and the maximum top value. MinTop := TControl( Ctrls[ Low( Ctrls ) ] ).Top; For IdX := Low( Ctrls ) To High( Ctrls ) Do If ( TControl( Ctrls[ IdX ] ).Top MinTop := TControl( Ctrls[ IdX ] ).Top; MaxTop := TControl( Ctrls[ High( Ctrls ) ] ).Top; For IdX := Low( Ctrls ) To High( Ctrls ) Do If ( TControl( Ctrls[ IdX ] ).Top MaxTop ) Then MaxTop := TControl( Ctrls[ IdX ] ).Top; // Applyes that. StepTop := ( MaxTop - MinTop ) Div ( High( Ctrls ) - Low( Ctrls ) ); For IdX := Low( Ctrls ) To High( Ctrls ) Do TControl( Ctrls[ IdX ] ).Top := MinTop + ( StepTop * IdX ); End; vaCenterInWindow : Begin // Finds the center of the window. If ( TControl( Ctrls[ Low( Ctrls ) ] ).Parent Is TForm ) Then Center := TForm( TControl( Ctrls[ Low( Ctrls ) ] ).Parent ).Height Div 2; // Applyes that. For IdX := Low( Ctrls ) To High( Ctrls ) Do TControl( Ctrls[ IdX ] ).Top := Center - ( TControl( Ctrls[ IdX ] ).Height Div 2 ); End; End; End; End; Use code like this: AlignCtrls( [ Button1, Button2, Button3 ], haLeftSides, vaSpaceEqually );