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.
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:
- Add a menu item with the Name: mnu_ChangeFont, Caption: Change Font.
- Add a common dialog control to the form. Use the default name.
- Add the cAutoResize class to the project.
- In the decelerations section add the following:
private withevents cResize as cAutoResize
- In the Form_Load event:
private Sub Form_Load() set cResize = new cAutoResize End sub
- In the mnu_ChangeFont_Click event:
- In the form_activate event:
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
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