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:
The scroll bar controls the zoom ratio of the OLE 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
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
' Zoom OLE control. Private Sub VScroll1_Change() ' Scale Height and Width. OLE1.Height = msActualHeight * VScroll1.Value OLE1.Width = msActualWidth * VScroll1.Value End Sub
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.