Dynamically Positioning the Control (Access 2.0)

Take the following steps to dynamically scale and position the LEAD control when loading an image. This example scales the control to use most of the form's area while preserving the image's aspect ratio.

1. Start with the project that you created in Loading and Displaying an Image.

2. Modify the LeadLoad button's click procedure so that it appears as follows. For a general explanation of how images are positioned and scaled, refer to Displaying an Image.

Sub LoadLead_Click()
  'Declare variables used for preserving aspect ratios.
  Dim HeightFactor As Long
  Dim WidthFactor As Long
  Dim HeightAllowed As Long
  Dim WidthAllowed As Long
  Dim ViewHeight, ViewWidth

  'Turn off automated behavior while we load and manipulate the image.
  Me![Lead1].Object.AutoSetRects = False
  Me![Lead1].Object.AutoRepaint = False
  Me![Lead1].Object.AutoScroll = False
  Me![Lead1].Object.BackErase = False

  'Load the bitmap. This hard-coded path name may be different on your system.
  Me![Lead1].Object.Load "c:\lead\images\image1.cmp", 0, 0, 1

  'Set the variables used for preserving the aspect ratio.
  'Allow for a border of 1/8 of the form size.
  'The units of measure do not matter, since we are calculating proportions.
  HeightFactor = Me![Lead1].Object.BitmapHeight 
  WidthFactor = Me![Lead1].Object.BitmapWidth 
  HeightAllowed = Me.WindowHeight * 3/4
  WidthAllowed = Me.WindowWidth * 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 Then
    Me![Lead1].Left = Me.WindowWidth / 8
    Me![Lead1].Width = WidthAllowed
    Me![Lead1].Height = (Me![Lead1].Width * HeightFactor) / WidthFactor
    Me![Lead1].Top = (Me.WindowHeight - Me![Lead1].Height) / 2
  Else
    Me![Lead1].Top = Me.WindowHeight / 8
    Me![Lead1].Height = HeightAllowed
    Me![Lead1].Width = (Me![Lead1].Height * WidthFactor) / HeightFactor
    Me![Lead1].Left = (Me.WindowWidth - Me![Lead1].Width) / 2
  End If

  'Set the image display size to match the LEAD control
  Me![Lead1].Object.SetDstRect 0, 0, Me![Lead1].Object.ScaleWidth, Me![Lead1].Object.ScaleHeight 
  Me![Lead1].Object.SetDstClipRect 0, 0, Me![Lead1].Object.ScaleWidth, Me![Lead1].Object.ScaleHeight 

  'Turn on automated behavior while we load and manipulate the image.
  Me![Lead1].Object.AutoSetRects = True
  Me![Lead1].Object.AutoRepaint = True
  Me![Lead1].Object.AutoScroll = True

End Sub

3. Close the code window.

4. Click the Form View icon on the toolbar to test the form.