Basic Program to Read Characters from the Screen into a String
ID: Q43527
|
The information in this article applies to:
-
Microsoft Visual Basic Standard and Professional Editions for MS-DOS, version 1.0
-
Microsoft QuickBASIC for MS-DOS, versions 4.0, 4.0b, 4.5
-
Microsoft BASIC Compiler for MS-DOS, versions 6.0, 6.0b
-
Microsoft BASIC Professional Development System (PDS) for MS-DOS, versions 7.0, 7.1
SUMMARY
The program below demonstrates how to use the PEEK statement to read
text that is printed on the screen into a Basic string variable. The
program PEEKs video memory (at segment B000h) to read the
characters from the screen.
(Note: An easier method to read characters from the screen is to use
the SCREEN function, which reads a character's ASCII value or its
color from a specified screen location.)
MORE INFORMATION
For a color display, you must change the address of the DEF SEG
statement in the function from B000h to B800h. Note that any
attributes (blinking, highlight, and so on) will be lost when the text
is put into the string.
Example Program
' To try this example in VBDOS.EXE:
' 1. From the File menu, choose New Project.
' 2. Copy the code example to the Code window.
' 3. Press F5 to run the program.
DECLARE FUNCTION readstr$ (xline%, xstart%, xstop%)
CLS
PRINT "Now is the time" ' Put some text on the screen.
PRINT "for all good men"
PRINT "to come to the aid"
PRINT "of their country."
a$ = readstr$(1, 1, 6) ' Read the first six characters of the
' first line (that is, "Now is").
b$ = readstr$(4, 10, 18) ' Read the last nine characters (10-18)
' on line four (that is, "country.").
LOCATE 10, 1
PRINT "A$ = "; a$
PRINT "B$ = "; b$
' readstr$ takes three integers and returns a string.
' The integers are: - The line on the screen to read from.
' - The starting character position.
' - The ending character position.
' It returns a string containing the characters on the screen
' on the specified line between the start and stop positions.
'
FUNCTION readstr$ (xline%, xstart%, xstop%)
DEF SEG = &HB000 ' Changes segment for PEEK to B800h for color
' display.
fstart% = (160 * (xline% - 1)) + (2 * (xstart% - 1))
fstop% = (160 * (xline% - 1)) + (2 * (xstop% - 1))
FOR x% = fstart% TO fstop% STEP 2
s$ = s$ + CHR$(PEEK(x%))
NEXT x%
readstr$ = s$
DEF SEG ' Restores DS to default to Basic's DGROUP segment.
END FUNCTION
Additional query words:
VBmsdos QuickBas BasicCom 1.00 4.00 4.00b 4.50 7.00 7.10 6.00 6.00b
Keywords :
Version : MS-DOS:1.0,4.0,4.0b,4.5; :6.0,6.0b,7.0,7.1
Platform : MS-DOS
Issue type :