Autoresize a form and its controls when the user changes the font

Automatically resize the form and controls when the user changes the font.

The use of the common dialog control in VB5/6 allowes the programer to easly give the user the ability to change font characterists. However resizing the controls and the form to accommodate the changes in the font characteristics poses some problems for the programmer. The problem is that there is no way to know in advance which font characterists the user will change. If you want to change the size of a control, for example a textbox, to a size that will accommodate a given font then you must know the height and width of the characters when that font is applied.

Screen Shot1

Click here for a larger image.

The windows API GETTEXTMETRICS will supply this information. This API call requires a handle to the device context for the control. Unfortunately many of the commonly used controls supplied with VB5/6 do not have a hDC property. As the form does have a hDC property the solution to this problem is to set the font for the form and then set the fonts for the controls to the same font as the form. This way you can use the hDC of the form in the call to GETTEXTMETRICS.

I have written a class that will use this API call to collect the information necessary for resizing the controls and the form to accommodate changes in the font.

This class sets the fonts of all controls to the same font and then read the text height and average text width. These values are used to compute the ratio of change between the new text height and average text width and the current text height and average text width. This ratio is then used to resize the form and the controls. The class contains an event that will fire after changing the form. This event can be used to reset any default values you do not want the user to change. For example setting the font to bold for all labels.


The form Border style is set to 1-Fixed Single.


Load and run the prjResizeTest or

With an existing project:

  1. Add a menu item with the Name: mnu_ChangeFont, Caption: Change Font.
  2. Add a common dialog control to the form. Use the default name.
  3. Add the cAutoResize class to the project.
  4. In the decelerations section add the following:
    private withevents cResize as cAutoResize
  5. In the Form_Load event:
    private Sub Form_Load()
        set cResize = new cAutoResize
    End sub
  6. In the mnu_ChangeFont_Click event:
    private Sub  mnu_ChangeFont_Click()
      Dim msg as string
      CommonDialog1.Flags = cdlCFBoth
    'get the current font info
    'on error show err msg then exit
      msg = cResize.GetOldFontInfo(me)
      If msg <> "OK" then
        MsgBox msg, vbOKOnly, "error"
        Exit Sub
      End If
    'set the font for the form
      With CommonDialog1
        me.FontBold = .FontBold
        me.FontItalic = .FontItalic
        me.FontName = .FontName
        me.FontSize = .FontSize
        me.FontStrikethru = .FontStrikethru
        me.FontUnderline = .FontUnderline
      End With
    'set the form
      msg = cResize.SetForm(me)
    'Check for errors
    'If error then show msg then out of here
      If msg <> "OK" then
        MsgBox msg, vbOKOnly, "error"
        Exit Sub
      End If
    End sub

  8. In the form_activate event:
    private Sub Form_Activate()
      Dim msg as string
    'Init cAutoresize and set the form
      msg = cResize.Init(me)
      If msg <> "OK" then
        MsgBox msg, vbOKOnly, "error"
        Exit Sub
      End If
    'Center form
      msg = cResize.SetForm(me)
      If msg <> "OK" then
        MsgBox msg, vbOKOnly, "error"
        Exit Sub
      End If
    End Sub

Download Zipped Project File


  • Good work

    Posted by Legacy on 06/19/2003 12:00am

    Originally posted by: Mukhtar Raza

    Mike Du Bois

    you did good job for other. I say thank you.
    As i am experinced programmer on Main fram but
    new in VB6.


Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date