Dynamically Positioning the Control (Access 95 and 97)

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.

Private 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.
  Lead1.AutoSetRects = False
  Lead1.AutoRepaint = False
  Lead1.AutoScroll = False
  Lead1.BackErase = False

  'Load the bitmap. This hard-coded path name may be different on your system.
  Lead1.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 = Lead1.BitmapHeight
  WidthFactor = Lead1.BitmapWidth
  HeightAllowed = Me.WindowHeight * 0.75
  WidthAllowed = Me.WindowWidth * 0.75

  '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
    Lead1.Left = Me.WindowWidth / 8
    Lead1.Width = WidthAllowed
    Lead1.Height = (Lead1.Width * HeightFactor) / WidthFactor
    Lead1.Top = (Me.WindowHeight - Lead1.Height) / 2
  Else
    Lead1.Top = Me.WindowHeight / 8
    Lead1.Height = HeightAllowed
    Lead1.Width = (Lead1.Height * WidthFactor) / HeightFactor
    Lead1.Left = (Me.WindowWidth - Lead1.Width) / 2
  End If

  'Set the image display size to match the LEAD control
  Lead1.SetDstRect 0, 0, Lead1.ScaleWidth, Lead1.ScaleHeight
  Lead1.SetDstClipRect 0, 0, Lead1.ScaleWidth, Lead1.ScaleHeight

  'Turn on automated behavior.
  Lead1.AutoSetRects = True
  Lead1.AutoRepaint = True
  Lead1.AutoScroll = True

End Sub

3. image\btncmpl.gif Click the compile button on the toolbar; then close the code window.

4. Close the form designer, saving the changes.

5. With Form1 selected in the Database window, click the Open button to test the form.