Zooming an Object for the Best Fit
Scaling enables you to fit more on-screen than other display methods. However, to
create legible objects, you must combine scaling with sizing the OLE control. To create a
best fit, use the SizeMode setting vbOLESizeZoom, the Resize event, and the Height and
Width properties together.
To see how to create the best fit for OLE objects inserted at runtime, follow these
steps to create the OLEZoom.VBP sample:
- Create a new project.
- Add an OLE control to the form and move the control so that its upper-left corner is in
the form's upper-left corner. Then add to the form a vertical scroll bar to control
zooming. Figure 18.5 shows the desired form.
FIG. 18.5
The scroll bar controls the zoom ratio of the OLE control.
- Add the following lines of code to the form's Load event procedure. When run, these
lines display the Insert Object dialog box that enables the user to select an OLE object
to insert in the control.
' Declare module-level variables used.
Dim msHeightRatio As Single, msWidthRatio As Single
Dim msIdealHeight As Single, msIdealWidth As Single
Dim msActualHeight As Single, msActualWidth As Single
Dim mResized As Boolean
Private Sub Form_Load()
' Scale the object to fit the control.
OLE1.SizeMode = vbOLESizeZoom
' Display the Insert Object dialog to get
' an object to display.
OLE1.InsertObjDlg
End Sub
- Add the following lines of code to the OLE object's Resize event procedure. This event
procedure calculates the ratio of the object's actual size (HeightNew and WidthNew) to the
size of the control. It also determines the display and Max value of the scroll bar used
to control zooming.
Private Sub OLE1_Resize(HeightNew As Single, WidthNew As Single)
' Get the actual height and width of the object
' from the application.
If Not mResized Then
' Get the control size.
msActualHeight = OLE1.Height
msActualWidth = OLE1.Width
' Temporarily switch SizeMode to get
' the actual size.
OLE1.SizeMode = vbOLESizeAutoSize
' Get the actual height and width of the object.
msIdealHeight = OLE1.Height
msIdealWidth = OLE1.Width
' Reset size mode and height/width
OLE1.SizeMode = vbOLESizeZoom
OLE1.Height = msActualHeight
OLE1.Width = msActualWidth
' Choose which ratio is greater.
msHeightRatio = OLE1.Height / msIdealHeight
msWidthRatio = OLE1.Width / msIdealWidth
' Use the greater ratio for the scroll bar zoom.
If msHeightRatio >= msHeightRatio Then
' Set the maxium value (400%)
VScroll1.Max = msWidthRatio * 4
Else
' Set the maxium value (400%)
VScroll1.Max = msWidthRatio * 4
End If
' Set the initial scrollbar position.
VScroll1.Min = 1
VScroll1.Value = 1
' Set module-level variable.
mResized = True
End If
End Sub
- Add the following lines of code to the scroll bar's Change event procedure. This code
scales the size of the OLE control up or down and also automatically scales the object
displayed in the control to match the control's size.
' Zoom OLE control.
Private Sub VScroll1_Change()
' Scale Height and Width.
OLE1.Height = msActualHeight * VScroll1.Value
OLE1.Width = msActualWidth * VScroll1.Value
End Sub
- Run the project. When the OLE object displays on the form, drag the scroll bar slider to
scale the object up or down (Figure 18.6).
FIG. 18.6
After creating the sample in this section, you can zoom the size of the object by
using the scroll bar.
Unfortunately, relying on the ScaleMode property settings vbOLESizeZoom and
vbOLESizeStretch does not work well for all applications. In particular, Microsoft Word
updates scaled objects in a peculiar way. You can work around these display problems by
capturing the image of the OLE control with its Picture property. To learn how to do so,
see the section "Capturing the Object's Picture," later in this chapter.