OnFilePage Event example for C++ Builder

This example shows how the OnFilePage event can be used to size and position a lead control when an image is loaded.

void __fastcall TForm1::Lead1FilePage(TObject *Sender)
{
   int HeightFactor, WidthFactor, /*Factors for aspect ratio */
       HeightAllowed, WidthAllowed, /*Maximum width and height */
       nWidth, nHeight, nLeft, nTop; /* control dimensions */
   RECT rcWindow;

   /* 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 = (ClientHeight * 3) / 4;
   WidthAllowed = (ClientWidth * 3) / 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))
   {
      nLeft = ClientWidth / 8;
      nWidth = WidthAllowed;
      nHeight = (nWidth * HeightFactor) / WidthFactor;
      nTop = (ClientHeight - nHeight) / 2;
   }
   else
   {
      nTop = ClientHeight / 8;
      nHeight = HeightAllowed;
      nWidth = (nHeight * WidthFactor) / HeightFactor;
      nLeft = (ClientWidth - nWidth) / 2;
   }
   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();
}