How to Use SizeMode Property of OLE Control to Size Display

Last reviewed: June 21, 1995
Article ID: Q112043
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0

SUMMARY

This article shows by example how to use the SizeMode property of the OLE Control for Visual Basic version 3.0 to obtain a proportional display in a limited screen space.

MORE INFORMATION

The SizeMode property of the OLE Control determines how the OLE control is sized or how its image is displayed when it contains an object.

Here are the valid settings for the SizeMode property:

 0 (Default) Clip -- The object is displayed in actual size. If the
   object is larger than the OLE control, its image is clipped by
   the control's borders, showing the upper-left portion of the image.

 1 Stretch -- The object's image is sized to fill the OLE control. The
   height of the image is stretched (or shrunk) to fit the OLE control.
   The same thing happens to the width. As a result, you may get a
   distorted image. Height and width are independent, not proportional.

 2 Autosize -- The OLE control is resized to display the entire object.
   The object is displayed in actual size. By using the resize event of
   the OLE control, you can adjust the HeightNew and WidthNew parameters
   to maintain size limits of the OLE control. However, the display
   behaves as if you had used the clip setting.

Step-by-Step Example

The goal of this example is to obtain a proportional display that is restricted to a limited screen space, that of the size of the form the OLE control is on.

The example uses AutoSize and Stretch settings to find a proportional best fit. The height and width of the container form supply the maximum height and width of the image. The Autosize property supplies best-fit information.

Then the Autosize property is set to Stretch mode for display.

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.

  2. Add an MSOLE2 control to the form. When the Insert Object dialog box asks what type of object to insert, select the Create From File option and a Microsoft Word .DOC file.

  3. Set the SizeMode property of the OLE control to 1 (Stretch).

  4. Add the following code to the Form_Load event:

       Sub Form_Load()
          OLE1.Top = 0
          OLE1.Left = 0
       End Sub
    
    

  5. Add the following code to the Resize event of the OLE control:

       Sub OLE1_Resize (heightNew As Single, WidthNew As Single)
          Dim wRatio As Single, hRatio As Single, cRatio As Single
          Dim MaxHeight As Single, MaxWidth As Single
    
          If OLE1.SizeMode = 2 Then
             MaxHeight = Me.Height
             MaxWidth = Me.Width
             'Calculate hRatio
             If heightNew > MaxHeight Then
                hRatio = 1 - (heightNew - MaxHeight) / heightNew
             Else
                hRatio = 1 + (MaxHeight - heightNew) / heightNew
             End If
             'Calculate wRatio
             If WidthNew > MaxWidth Then
                wRatio = 1 - (WidthNew - MaxWidth) / WidthNew
             Else
                wRatio = 1 + (MaxWidth - WidthNew) / WidthNew
             End If
             'Pick best ratio for cRatio
             If hRatio < wRatio Then
                cRatio = hRatio
             Else
                cRatio = wRatio
             End If
             'Apply changes
             OLE1.Height = CInt(cRatio * heightNew)
             OLE1.Width = CInt(cRatio * WidthNew)
             OLE1.SizeMode = 1
          ElseIf OLE1.SizeMode = 1 Then
             'Use AutoSize to recalculate ratio.
             OLE1.SizeMode = 2
          End If
       End Sub
    
    

  6. Run the program.

When you double-click the OLE control and make changes to your document, you should see the control itself change size to fit proportionally on your form.


Additional reference words: 3.00 OLE Automation OA OLE2
KBCategory: kbprg kbcode
KBSubcategory: PrgCtrlsCus


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.