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: code format="bb4w" WM_SETFONT = 48 *FONT Courier New,9,B SYS "SendMessage", hbox%, WM_SETFONT, @vdu.hf%, 0 code 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: code format="bb4w" WM_SETFONT = 48 *FONT Courier New,9,B SYS "SendDlgItemMessage", !dlg%, idbox%, WM_SETFONT, @vdu.hf%, 0 code 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: code format="bb4w" 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 code 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): code format="bb4w" SYS "DeleteObject", hfont% code


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