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

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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

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.

Assumptions

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

Testing:

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:
  7. 
    private Sub  mnu_ChangeFont_Click()
      Dim msg as string
    
      CommonDialog1.Flags = cdlCFBoth
      CommonDialog1.ShowFont
    
    '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:
  9. 
    
    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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read