User Tools

Site Tools


changing_20the_20font_20of_20a_20control

Changing the font of a control

by Richard Russell, October 2006

By default a Windows control (e.g. edit box, list box or static box) uses a font which in most circumstances should be suitable for your needs. This is particularly so for a control in a dialogue box, because the default font size is determined by system-wide settings which are under the control of the user (for example he may have selected Large Fonts).

However there may be rare circumstances when you need to change the font used by a control. In the case of a control on your main window you can do that as follows:

        WM_SETFONT = 48
        *FONT Courier New,9,B
        SYS "SendMessage", hbox%, WM_SETFONT, @vdu.hf%, 0

where hbox% is the window handle of the control, typically as returned from FN_editbox, FN_listbox or FN_staticbox. In this case the font selected is Courier New in bold and with a size of 9 points.

In the case of an edit, list or static control in a dialogue box the equivalent code is as follows:

        WM_SETFONT = 48
        *FONT Courier New,9,B
        SYS "SendDlgItemMessage", !dlg%, idbox%, WM_SETFONT, @vdu.hf%, 0

Here dlg% is the value returned from FN_newdialog and idbox% is the ID number allocated to the control. Note that this code must be executed after the call to PROC_showdialog.

A side effect of this method is that the font used for normal BASIC output to the main window is also changed, so it may not be suitable if you want to output ordinary text at the same time as the control is displayed. You must not issue a new *FONT command until all controls making use of the previous font have been closed.

If this is an issue you can alternatively select the font as follows:

        WM_SETFONT = 48
        SYS "CreateFont",h%,0,0,0,w%,i%,u%,0,0,0,0,0,0,"Courier New" TO hfont%
        SYS "SendMessage", hbox%, WM_SETFONT, hfont%, 0

Where h% is the height in pixels, w% is the weight (400 = normal, 700 = bold), i% is 1 for italic and u% is 1 for underlined. You should delete the font when you have finished with it (i.e. when all the controls using it have been closed):

        SYS "DeleteObject", hfont%

Important note: The text size resulting from a *FONT command varies according to the current Dots Per Inch setting (see Supporting different DPI values). This is what you want for a control in a dialogue box, because the dialogue box itself and all its controls also change their size according to the DPI value. However with an edit box, list box or static box on your main output window the font size will change but the size of the box (in pixels) will not. In that case you can either test the DPI value and choose your font size accordingly, or use the CreateFont method where the size is specified in pixels.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
changing_20the_20font_20of_20a_20control.txt · Last modified: 2024/01/05 00:22 by 127.0.0.1