StockFont

Description

Ever get the urge to do a little "design" with those old box-drawing characters that were so common in the days of DOS? Yeah, me either. But I do see the question asked from time to time. In particular, with regard to the need to redistribute fonts with the final product. The good news is, Windows provides a stock font that emulates the fixed-width ASCII characters we lived with throughout the 80s. In case you're too young to remember, here's what will amount to a dose of déjà vu for the rest of us:

As you can see, there's more than just box-drawing characters there. You can use this font to create your own card games too! <g> Stock fonts are always available via the GetStockObject API function, which retrieves a handle you can pass to SendMessage with WM_SETFONT:

Color-coded with vbMarkUp - try it today!
Public Sub SetOemFont(ByVal hWnd As Long)
   Dim hFont As Long
   Const OEM_FIXED_FONT As Long = 10
   Const WM_SETFONT As Long = &H30
   ' Select the stock font into the client window.
   ' Not necesary to call DeleteObject.
   hFont = GetStockObject(OEM_FIXED_FONT)
   Call SendMessage(hWnd, WM_SETFONT, hFont, True)
End Sub

Applying this to any control that exposes an hWnd property is as simple as passing that value to the above routine. I've marked this project as "VBA-Ready", and technically it is. The only problem with using it in VBA is that most of the controls are windowless, and even those that aren't don't tend to expose the requisite hWnd property. Still, if you can obtain an hWnd, you can select a stock font into it.

Published

This sample, or the one from which it originally derived, was published (or at least peripherally mentioned) in the following article(s):

APIs Usage

This sample uses the following API calls:

Module Library Function
MStockFont.bas gdi32

user32
DeleteObject
GetStockObject
SendMessage

Don't see what you're looking for? Here's a complete API cross-reference.

Download

Download StockFont.zip   Please, enjoy and learn from this sample. Include its code within your own projects, if you wish. But, in order to insure only the most recent code is available to all, I ask that you don't share the sample by any form of mass distribution.

Download StockFont.zip, 8Kb, Last Updated: Tuesday, May 19, 2009

See Also

The following resources may also be of interest: