How to Print the ASCII Character Set in Visual Basic (75857)
The information in this article applies to:
- Microsoft Visual Basic Standard Edition for Windows 2.0
- Microsoft Visual Basic Standard Edition for Windows 3.0
- Microsoft Visual Basic Professional Edition for Windows 2.0
- Microsoft Visual Basic Professional Edition for Windows 3.0
- Microsoft Visual Basic Standard Edition for Windows 1.0
This article was previously published under Q75857 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 in
MS-DOS-based applications, you must call the two Windows API functions
GetStockObject and SelectObject. In addition, to display the unprintable
characters such as TAB, linefeed, and carriage return characters, you
need to use the TextOut Windows API function because the standard Visual
Basic printer object does not display the unprintable characters. By
using the Windows API TextOut function, you circumvent the Visual Basic
printer object and therefore allow all the characters to be displayed.
MORE INFORMATION
Windows supports a second character set, referred to as the OEM character
set. This is generally the character set used internally by MS-DOS for
screen display at the MS-DOS prompt. The character codes 32 to 127 are
usually 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. However, 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 finish 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.
Here is the syntax for the functions:
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.
Step-by-Step Example
The following example steps demonstrate how to create a program that prints
ASCII characters.
- Start Visual Basic or from the File menu, choose New Project (ALT, F, N)
if Visual Basic is already running. Form1 is created by default.
- Add a command button (Command1) to Form1.
- Add the following code to the General Declarations section of Form1:
' Enter each Declare statement on one, single line.
Declare Function GetStockObject% Lib "GDI" (ByVal nIndex%)
Declare Function SelectObject% Lib "GDI" (ByVal hDC%, ByVal hObject%)
Declare Function TextOut Lib "GDI" (ByVal hDC As Integer,
ByVal X As Integer, ByVal Y As Integer, ByVal lpString As String,
ByVal nCount As Integer) As Integer
- 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 the OEM Font Object
Dim Y, H As Single
'*save the scale mode so that you can reset later
Saved% = Form1.ScaleMode
'*alter the current scale mode
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
PreviousObject% = SelectObject%(Form1.hDC, hOEM)
'* if successful then print the desired characters.
If PreviousObject% Then
'* establish border
Edge$ = "0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 "
'* initialize output location
xMark = 10
yMark = 10
'* position cursor & print top border
Form1.CurrentX = xMark
Form1.CurrentY = yMark
'* print top ruler edge
T$ = " " + Edge$ + " "
ret% = TextOut(Form1.hDC, yMark, xMark, T$, Len(T$))
'* Cycle through 256 characters beginning at character 0
For Row% = 0 To 15
'* prep left border
T$ = Mid$(Edge$, (Row% * 2) + 1, 2)
'* assemble string of characters
For Col% = 0 To 15
Ch = (Row% * 16) + Col%
T$ = T$ + Chr$(Ch) + " "
Next
'* prep right border
T$ = T$ + Mid$(Edge$, (Row% * 2) + 1, 2)
'* prepare for display at next row
xMark = xMark + H
'* print the assembled string of characters
ret% = TextOut(Form1.hDC, yMark, xMark, T$, Len(T$))
Next
'* prepare for display at next row
xMark = xMark + H
'* print bottom border
T$ = " " + Edge$ + " "
ret% = TextOut(Form1.hDC, yMark, xMark, T$, Len(T$))
'* reinstate the 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
- From the Run menu, choose Start.
- 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.
ASCII and ANSI Character Sets
For a listing of the ASCII and ANSI character sets, see the Visual Basic
Help menu.
American Standard Code for Information Interchange (ASCII) is the 7-bit
character set widely used to represent letters and symbols found on a
standard United States keyboard. The ASCII character set is the same as the
first 128 characters (0 to 127) in the American National Standards
Institute (ANSI) character set. The ANSI character set uses all 8 bits in a
byte, and includes 256 characters (0 to 255). Characters 128 to 255 are
sometimes called the extended-ASCII characters.
REFERENCES- "Programming Windows: the Microsoft Guide to Writing Applications
for Windows 3," by Charles Petzold (published by Microsoft Press,
1990)
- "Peter Norton's Windows 3.0 Power Programming Techniques," by
Peter Norton & Paul Yao (published by Bantam Computer Books, 1990)
- "Microsoft Windows 3.0 Software Development Kit: Reference Volume 1"
- The WINSDK.HLP file shipped with Microsoft Windows 3.0 Software
Development Kit.
Modification Type: | Major | Last Reviewed: | 12/12/2003 |
---|
Keywords: | KB75857 |
---|
|