Skip to content

Instantly share code, notes, and snippets.

@ccy
Last active August 26, 2021 14:11
Show Gist options
  • Save ccy/485abd2ca6b556bb49cc3be47597ac92 to your computer and use it in GitHub Desktop.
Save ccy/485abd2ca6b556bb49cc3be47597ac92 to your computer and use it in GitHub Desktop.
FastReport: Align linear controls to center horizontally
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