Last active
August 26, 2021 14:11
-
-
Save ccy/485abd2ca6b556bb49cc3be47597ac92 to your computer and use it in GitHub Desktop.
FastReport: Align linear controls to center horizontally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
procedure AlignHorzCenter(aControls: array of TfrxView); | |
var i: Integer; | |
iTotalWidth, iLeft: Extended; | |
CalcWidths: array of Extended; | |
Gaps: array of Extended; | |
begin | |
if not Engine.FinalPass then Exit; | |
if Length(aControls) = 0 then Exit; | |
// Calculate actual width of controls | |
SetLength(CalcWidths, Length(aControls)); | |
for i := 0 to Length(aControls) - 1 do begin | |
if aControls[i] is TfrxCustomMemoView then | |
CalcWidths[i] := TfrxCustomMemoView(aControls[i]).CalcWidth | |
else | |
CalcWidths[i] := aControls[i].Width; | |
end; | |
// Calculate Gaps between controls | |
SetLength(Gaps, Length(aControls)); | |
Gaps[0] := 0; | |
for i := 1 to Length(aControls) - 1 do | |
Gaps[i] := aControls[i].Left - (aControls[i-1].Left + aControls[i-1].Width); | |
// Calcualte total width of controls in linear position | |
iTotalWidth := 0; | |
for i := 0 to Length(CalcWidths) - 1 do | |
iTotalWidth := iTotalWidth + CalcWidths[i]; | |
for i := 0 to Length(Gaps) - 1 do | |
iTotalWidth := iTotalWidth + Gaps[i]; | |
// Re-align controls to the center horizontally | |
iLeft := (TfrxView(aControls[0].Parent).Width - iTotalWidth) / 2; | |
for i := 0 to Length(aControls) - 1 do begin | |
iLeft := iLeft + Gaps[i]; | |
aControls[i].Left := iLeft; | |
iLeft := iLeft + CalcWidths[i]; | |
end; | |
end; | |
procedure PageHeader1OnAfterCalcHeight(Sender: TfrxComponent); | |
begin | |
AlignHorzCenter([Memo1, Memo2]); | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment