Microsoft KB Archive/43527

From BetaArchive Wiki
Knowledge Base

Article ID: 43527

Article Last Modified on 8/16/2005


  • Microsoft Visual Basic for MS-DOS
  • Microsoft QuickBasic 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBasic 4.5 for MS-DOS
  • Microsoft BASIC Compiler 6.0
  • Microsoft BASIC Compiler 6.0b
  • Microsoft BASIC Professional Development System 7.0
  • Microsoft BASIC Professional Development System 7.1

This article was previously published under Q43527


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.)


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%)
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.

Additional query words: VBmsdos QuickBas BasicCom 1.00 4.00 4.00b 4.50 7.00 7.10 6.00 6.00b

Keywords: KB43527