How to Print the ASCII Character Set in Visual Basic

ID Number: Q75857

1.00

WINDOWS

Summary:

The default font used by Visual Basic is the standard ANSI character

set. To display the ASCII character set, which is more commonly used

by DOS mode applications, you must call two different Windows API

functions. The Windows API functions are GetStockObject and

SelectObject.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

Windows supports a second character set, referred to as the OEM

character set. This is generally the character set used internally by

DOS for screen display at the DOS prompt. The character codes 32 to

127 are normally identical for the OEM, ASCII, and ANSI character

sets. The ANSI characters represented by the remaining character codes

(codes 0 to 31 and 128 to 255) are generally different from characters

represented by the OEM and ASCII character sets. The OEM and ASCII

character sets are identical for these ranges. Under the ASCII and OEM

character sets, the character codes 128 to 255 correspond to the

extended ASCII character set, which includes line drawing characters,

"graphics" characters, and special symbols. The characters represented

by this range of character codes generally differ between the ASCII

(or OEM) and ANSI character sets.

To change the selected font from ANSI to the OEM ASCII font, you must

get a handle to the OEM character set by calling GetStockObject. When

this handle is passed as an argument to SelectObject, the ANSI font

will be replaced by the OEM ASCII font. This API function also returns

the handle to the font object previously used. Once you are through

displaying the desired characters, you should call SelectObject again

to reselect the original font object.

Note: There is also an API function called DeleteObject. This function

need not be called to delete a stock object. The purpose of this API

function is to delete objects loaded with the API function GetObject.

The three functions are described as follows:

GetStockObject% (nIndex%)

-------------------------

nIndex%

Specifies the type of stock object desired. Use the constant

OEM_FIXED_FONT to retrieve the handle to the OEM character set.

The value of this constant is 10.

Return Value

The return value identifies the desired logical object if the

function is successful. Otherwise, it is NULL.

SelectObject% (hDC%, hObject%)

------------------------------

hDC%

Identifies the device context.

hObject%

Identifies the object to be selected. Use the return value from

GetStockObject% (above) to select the OEM character set.

Return Value

The return value identifies the handle to the object previously

used. This value should be saved in a variable such that

SelectObject can be called again to restore the original object

used. It is NULL if there is an error.

The following steps describe how to create a program example that

demonstrates how to print ASCII characters.

1. Run Visual Basic.

2. From the File menu, choose New Project (ALT, F, N).

3. Create a command button called Command1 on the default form

(Form1).

4. Add the following declarations to the General Declarations section

of Form1.

Declare Function GetStockObject% Lib "GDI" (ByVal nIndex%)

Declare Function SelectObject% Lib "GDI" (ByVal hdc%, ByVal hObject%)

5. Place the following code in the Command1 click event procedure:

Sub Command1_Click ()

Const OEM_FIXED_FONT = 10

Const PIXEL = 3

Dim hOEM As Integer '* handle to the OEM font object

Dim Y, H As Single

'* save the scale mode so that you can reset later

Saved% = Form1.ScaleMode

Form1.ScaleMode = PIXEL

'* Get the character height and subtract the external leading

H = Form1.TextHeight(Chr$(200)) - 1

'* get the handle to the desired font

hOEM = GetStockObject(OEM_FIXED_FONT)

'* select the object relating to the font handle, if

'* successful then print the desired characters.

PreviousObject% = SelectObject(Form1.hDC, hOEM)

If PreviousObject% Then

Form1.CurrentX = 10: Form1.CurrentY = 10

Print Chr$(201); Chr$(187);

Form1.CurrentX = 10:

Form1.CurrentY = Form1.CurrentY + H

Print Chr$(200); Chr$(188)

'* Reinstate previous font

hOEM = SelectObject(Form1.hDC, PreviousObject%)

Else

'* SelectObject was unsuccessful

MsgBox "Couldn't find OEM fonts", 48

End If

'* reset the scale mode

Form1.ScaleMode = Saved%

End Sub

6. From the Run menu, choose Start.

7. Click the Command1 button.

When the Command1 button is clicked or selected, a small box with a

double border will be drawn in the upper-left corner of the screen.

The box is drawn using characters associated with the extended ASCII

character set.

References:

1. "Programming Windows: the Microsoft Guide to Writing Applications

for Windows 3," by Charles Petzold (published by Microsoft Press,

1990)

2. "Peter Norton's Windows 3.0 Power Programming Techniques," by

Peter Norton & Paul Yao (published by Bantam Computer Books, 1990)

3. "Microsoft Windows 3.0 Software Development Kit: Reference

Volume 1"

4. The WINSDK.HLP file shipped with Microsoft Windows 3.0 Software

Development Kit.

Additional reference words: 1.00 3.00