OnFilePage Event example for Delphi
This example shows how the OnFilePage event can be used to size and position a lead control when an image is loaded.
procedure TForm1.Lead1FilePage(Sender: TObject);
var
HeightFactor,WidthFactor : Integer; {Factors for aspect ratio }
HeightAllowed,WidthAllowed: Integer; {Maximum width and height }
nWidth,nHeight,nLeft,nTop : Integer; {Variables for control dimensions }
rcWindow : TRect;
begin
{ Set the variables used for preserving the aspect ratio. }
{ Allow for a border of 1/8 of the form size. }
HeightFactor := Lead1.InfoHeight;
WidthFactor := Lead1.InfoWidth;
HeightAllowed := Round(ClientHeight * 3.0 / 4);
WidthAllowed := Round(ClientWidth * 3.0 / 4);
{ Center the LEAD control on the form, preserving the aspect ratio. }
{ Check to see if using the maximum width will make the image too tall. }
{ Set the dimensions based on the result. }
if(((WidthAllowed * HeightFactor) / WidthFactor) < HeightAllowed) then
begin
nLeft := Round(ClientWidth / 8.0);
nWidth := WidthAllowed;
nHeight := Round((nWidth * HeightFactor) / WidthFactor);
nTop := Round((ClientHeight - nHeight) / 2);
end
else
begin
nTop := Round(ClientHeight / 8.0);
nHeight := HeightAllowed;
nWidth := Round((nHeight * WidthFactor) / HeightFactor);
nLeft := Round((ClientWidth - nWidth) / 2);
end;
rcWindow:=Rect(0, 0, nWidth, nHeight);
OffsetRect(rcWindow, nLeft, nTop);
Lead1.Left:=rcWindow.Left;
Lead1.Top:=rcWindow.Top;
Lead1.Height:=rcWindow.Bottom - rcWindow.Top;
Lead1.Width:=rcWindow.Right - rcWindow.Left;
{ Turn off scroll bars to make sure we use the full client area. }
Lead1.AutoScroll:=False;
{ Set the image display size to match the LEAD control }
Lead1.SetDstRect(0, 0,Lead1.Width, Lead1.Height);
Lead1.SetDstClipRect(0, 0, Lead1.Width, Lead1.Height);
Lead1.ForceRepaint;
end;