Scrolling the Control for Large Objects


Another way to size objects is to enable the user to scroll large objects up, down, right, or left. To create scrollable OLE objects, you use the ScaleMode setting vbOLESizeAutoSize, as described in these steps which create the OLEScroll.VBP sample:

  1. Create a new project.
  2. Draw horizontal and vertical scroll bars on the form and an OLE control (see Figure 18.3).

    FIG. 18.3

    The OLE control starts at the form's origin (0,0) and covers the entire form. The scroll bars appear on top of the OLE control, so they are obscured when you activate the object for editing.

  3. Add the following lines of code to the form's Load event procedure:

    
            Private Sub Form_Load()
                  ' Use the default SizeMode, OLE control
                  ' automatically resize to match the object's size.
                      OLE1.SizeMode = vbOLESizeAutoSize
                  ' Display the InsertObject dialog box to
                  ' let the user choose anobject to embed or link.
                      OLE1.InsertObjDlg
             End Sub
  4. Add the following lines of code to the scroll bars' Scroll event procedures. These lines scroll the OLE object on the form when the user moves either scroll bar.

    
            Private Sub HScroll1_Change()
                 OLE1.Left = 0 - HScroll1.Value
             End Sub
             Private Sub VScroll1_Change()
                 OLE1.Top = 0 - VScroll1.Value
             End Sub
  5. Add the following lines of code to the OLE control's Resize event procedure. These lines control the scroll bars' display and determine the scroll bars' scale and maximum values based on the OLE object's size.

    
            Private Sub OLE1_Resize(HeightNew As Single, 
                  WidthNew As Single)
                 If HeightNew > Form1.Height Then
                     VScroll1.Visible = True
                     VScroll1.Max = HeightNew
                     VScroll1.LargeChange = _
                          HeightNew / (HeightNew / OLE1.Height)
                     VScroll1.SmallChange = VScroll1.LargeChange / 10
                 Else
                     VScroll1.Visible = False
                 End If
                 If WidthNew > Form1.Width Then
                     HScroll1.Visible = True
                     HScroll1.Max = WidthNew
                     HScroll1.LargeChange = WidthNew / _
                         (WidthNew / OLE1.Width)
                     HScroll1.SmallChange = HScroll1.LargeChange / 10
                 Else
                     HScroll1.Visible = False
                 End If
             End Sub
  6. Add the following lines of code to the form's Resize event procedure. These lines trigger the OLE control's Resize event to elicit the correct behavior when the user resizes the form.

    
            Private Sub Form_Resize()
                 ' Skip first Resize on Load.
                 Static bFlag As Boolean
                 If bFlag Then
                     ' If form resizes, trigger OLE control
                     '  resize behavior.
                     OLE1_Resize OLE1.Height, OLE1.Width
                 Else
                     bFlag = True
                 End If
                  ' Call support procedure to adjust the placement
                  ' and size of scroll bars on the form.
                  AdjustScrollBars Me
             End Sub
  7. Run the project. When displaying on the form, the OLE object appears with scroll bars if it is larger than the current form, as shown in Figure 18.4.

    FIG. 18.4

    After creating the sample in this section, you can resize the form or scroll it to see more of the object.

In Step 6, you use the support procedure in Listing 18.2 to adjust the scroll bar's positions on the form. This procedure is useful in a variety of contexts, so the companion CD presents it as a separate procedure instead of building it into the form's Resize event.

Listing 18.2 - GenForm.BAS - A Procedure for Adjusting Scroll Bars When a Form Is Resized


' AdjustScrollBars procedure
'
' Keeps scroll bars on the outer edges of a form
' after resizing. Assumes that the horizontal
' and scroll bars in the form's controls
' collect apply to the form.
Sub AdjustScrollBars(frmTarget As Form)
   ' Declare size and object variables.
   Dim sHeight As Single, sWidth As Single
   Dim objCount As Object
   Dim scrHScroll As Control, scrVScroll As Control
   ' Search through the form's controls collection...
   For Each objCount In frmTarget.Controls
       ' Find the horizontal scroll bar.
       If TypeName(objCount) = "HScrollBar" Then
           ' Initialize object variable.
           Set scrHScroll = objCount
           ' If visible, then record height to help position
           ' vertical scroll bar later.
           If scrHScroll.Visible = True Then
               sHeight = scrHScroll.Height
           End If
       ' Find the vertical scroll bar.
       ElseIf TypeName(objCount) = "VScrollBar" Then
           ' Initialize object variable.
           Set scrVScroll = objCount
           ' If visible, then record width to help position
           ' horizontal scroll bar later.
           If scrVScroll.Visible = True Then
               sWidth = scrVScroll.Width
           End If
       End If
   Next objCount
   ' Set position of horizontal scroll bar (if one exists).
   If Not IsEmpty(scrHScroll) Then
       scrHScroll.Top = frmTarget.ScaleHeight - sHeight
       scrHScroll.Width = frmTarget.ScaleWidth - sWidth
   End If
   ' Set position of vertical scroll bar (if one exists).
   If Not IsEmpty(scrVScroll) Then
       scrVScroll.Left = frmTarget.ScaleWidth - sWidth
       scrVScroll.Height = frmTarget.ScaleHeight - sHeight
   End If
End Sub