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.

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



Comments

  • Good work

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

    Originally posted by: Mukhtar Raza

    Hi
    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.

    Regds
    Raza

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

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Confused by all the agile advice? Relax! With the Agile for Dummies eBook by your side you'll learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill customer needs much faster.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds