How to Print the ASCII Character Set in Visual BasicID: Q75857
|
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.
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:
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.
The return value identifies the desired logical object if the function is successful. Otherwise, it is NULL.
Identifies the device context.
Identifies the object to be selected. Use the return value from GetStockObject% (above) to select the OEM character set.
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.
' 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
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
Additional query words: 2.00 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 10, 1999