User Tools

Site Tools


changing_20the_20font_20of_20a_20control

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

changing_20the_20font_20of_20a_20control [2018/03/31 13:19]
127.0.0.1 external edit
changing_20the_20font_20of_20a_20control [2018/04/15 17:17] (current)
richardrussell Added syntax highlighting
Line 1: Line 1:
 =====Changing the font of a control===== =====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:\\ +//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 bb4w> ​
         WM_SETFONT = 48         WM_SETFONT = 48
         *FONT Courier New,9,B         *FONT Courier New,9,B
         SYS "​SendMessage",​ hbox%, WM_SETFONT, @vdu.hf%, 0         SYS "​SendMessage",​ hbox%, WM_SETFONT, @vdu.hf%, 0
-where **hbox%** is the window handle of the control, typically as returned from [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​fneditbox|FN_editbox]],​ [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​fnlistbox|FN_listbox]] or [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​fnstatic|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>​ 
 +         
 +where **hbox%** is the window handle of the control, typically as returned from [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​fneditbox|FN_editbox]],​ [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​fnlistbox|FN_listbox]] or [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​fnstatic|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 bb4w> ​
         WM_SETFONT = 48         WM_SETFONT = 48
         *FONT Courier New,9,B         *FONT Courier New,9,B
         SYS "​SendDlgItemMessage",​ !dlg%, idbox%, WM_SETFONT, @vdu.hf%, 0         SYS "​SendDlgItemMessage",​ !dlg%, idbox%, WM_SETFONT, @vdu.hf%, 0
-Here **dlg%** is the value returned from [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​newdialog|FN_newdialog]] and **idbox%** is the ID number allocated to the control. Note that this code must be executed //after// the call to [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​showdialog|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>​ 
 + 
 +Here **dlg%** is the value returned from [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​newdialog|FN_newdialog]] and **idbox%** is the ID number allocated to the control. Note that this code must be executed //after// the call to [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​showdialog|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 bb4w>
         WM_SETFONT = 48         WM_SETFONT = 48
         SYS "​CreateFont",​h%,​0,​0,​0,​w%,​i%,​u%,​0,​0,​0,​0,​0,​0,"​Courier New" TO hfont%         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         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):\\ +</​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 bb4w>
         SYS "​DeleteObject",​ hfont%         SYS "​DeleteObject",​ hfont%
-\\ **Important note:** The text size resulting from a *FONT command varies according to the current Dots Per Inch setting (see [[/​Supporting%20different%20DPI%20values|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.+</​code>​ 
 + 
 +**Important note:** The text size resulting from a *FONT command varies according to the current Dots Per Inch setting (see [[/​Supporting%20different%20DPI%20values|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.
changing_20the_20font_20of_20a_20control.txt · Last modified: 2018/04/15 17:17 by richardrussell