The magazine of the Melbourne PC User Group
Seeing the Basics: Printer and
Screen Fonts and WYSIWSG
Tony Stevenson |
|
Selecting the right font
It is fun to develop a small VB project that displays the screen and printer fonts that are available for a
particular systems configuration.
To do so, start a new project, and add two list boxes naming them lstPrinterFonts and
lstScreenFonts respectively. Make sure the list boxes are wide enough to handle some of the more lengthy
font names that will be displayed, and also that the Sorted property for each list box is set to True.
Now add two command buttons. The first is named cmdListPrinterFonts, with a caption of List Printer
Fonts, and the second is called cmdListScreenFonts with a caption of List Screen Fonts. The following
code is then written into the respective click events of these buttons so that the sample project looks like
Listing 1.
|

Listing 1.
|
The FontCount property is a useful one because it can be used to
determine the number of fonts ready for use with the active display and printer of a particular system
configuration.
And when used in conjunction with the Fonts property, it provides an elegant and quick way of finding
out which printer and screen fonts are available for use within a VB application.
As a VB programming standard, it is always a good idea to discipline yourself to use the Clear method
to empty out the contents of a list box before starting to add list entries with the AddItem method.
The failure to include the Clear can sometimes introduce subtle bugs into an application, making it
difficult to trace and resolve the source of the problem.
While writing code to list both printer and screen fonts may be fun, the resulting application does seem to
be of little practical use. However, with just some small additions to the project, along with a little
reorganisation, this demonstration program can be turned into a useful piece of software that you can add to
your VB programming toolbox.
Add a new command button to the project, and call it cmdListCommonFonts with a caption of List Common
Fonts. Now place a new list box onto the form with a name of lstCommonFonts, again making sure to
switch its Sorted property to True. Position the relevant command buttons under their associated list
boxes. Then code the click event for the List Common Fonts button so that it resembles Listing
2.
|

Listing 2.
|
Because the list of common fonts can take a little time to compile when the
program is running, it is a good idea to change the mouse cursor into an hour glass to inform the user that
the application is indeed still working. Why not go back now and add similar code to the click events of the
other two command buttons.
Start the application, and then click the three command buttons in succession.
On first looks, it appears that there are no differences between the contents of the three list boxes. But a
closer examination does reveal those fonts that can be successfully used with both printed output and
information displayed on a screen. By only using fonts common to both the display and print devices for a
particular user's configuration, it is possible to construct VB applications that are WYSIWYG (What You See
Is What You Get) in nature.
It would be a good idea to include in the project a count of the entries in each list box to emphasise to
users that there are differences in the number of the printer, screen, and common fonts. Do that now by
adding three labels under each of the list boxes, naming them lblPrinterFontCount,
lblScreenFontCount, and lblCommonFontCount respectively. Also set the caption property of each of
the labels to spaces.
Add the following (single) lines of code towards the end of the respective click events of the command
buttons (insert them just before those locations in the application where the mouse pointer is changed back
to its default shape):
For the cmdListPrinterFonts button add:
lblPrinterFontCount = lstPrinterFonts.ListCount
For the cmdListScreenFonts button:
lblScreenFontCount = lstScreenFonts.ListCount
And for the cmdListCommonFonts button:
lblCommonFontCount = lstCommonFonts.ListCount
Adding the common fonts to a menu
Instead of displaying the common fonts in a list box as we have just done, a more user friendly approach
would be to include them as menu options.
Using VBs Menu Editor, add a menu called mnuFonts with an associated caption of Fonts. Then
create a menu control array by setting up a second menu option indented under the first one, giving this a
name of mnuCommonFonts, setting its caption to the word empty, and most importantly, its index to
0.
|

Listing 3
|
Place another command button on the form, name it cmdLoadFontMenu and
set its caption to Load Font Menu Options. Put the following code into its click event:
Run the application, click the List Common Fonts command button, and then click the Load Font Menu Options
button. And presto, there are the available common printer and screen fonts set up as menu options ready to
be used within the application.
Get ready to explore VB 5.0
By the time you are reading this, Visual Basic 5.0 will have been released. So next months column will
provide an overview of the latest release, with a look at its newest features and performance
improvements.
Reprinted from the May 1997 issue of PC Update, the magazine
of Melbourne PC User Group, Australia
|