Microsoft KB Archive/31157

From BetaArchive Wiki
Knowledge Base


Use PRINT# to MS-DOS "CON" Device to Send ANSI Escape Codes

Article ID: 31157

Article Last Modified on 11/21/2006



APPLIES TO

  • Microsoft GW-BASIC 3.2
  • Microsoft GW-BASIC 3.22
  • Microsoft GW-BASIC 3.23
  • Microsoft BASIC Professional Development System 7.0
  • Microsoft BASIC Professional Development System 7.1
  • Microsoft BASIC Compiler 6.0
  • Microsoft BASIC Compiler 6.0b
  • Microsoft QuickBasic 1.0
  • Microsoft QuickBasic 1.01
  • Microsoft QuickBasic 1.02
  • Microsoft QuickBASIC 1.0b
  • Microsoft QuickBASIC 1.0b
  • Microsoft QuickBasic 2.0
  • Microsoft QuickBasic 2.01
  • Microsoft QuickBasic 3.0
  • Microsoft QuickBasic 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBasic 4.5 for MS-DOS



This article was previously published under Q31157

SUMMARY

Sending ANSI escape codes to the screen with the PRINT statement will not perform ANSI screen control because the PRINT statement (in the products below) circumvents DOS and its device drivers.

To make ANSI escape sequences work properly, you must OPEN the DOS "CON" or "CONS:" (console) device instead, and use PRINT# for output.

This information applies to the following products:

  1. QuickBasic Versions 1.00, 1.01, 1.02, 2.00, 2.01, 3.00, 4.00, 4.00b, and 4.50 for IBM Personal Computers and Compatibles
  2. Microsoft Basic Compiler Version 6.00 and 6.00b for MS-DOS
  3. Microsoft Basic PDS Version 7.00 for MS-DOS and MS OS/2
  4. GW-Basic Versions 3.20, 3.21, 3.22, 3.23
  5. IBM and Compaq versions of BasicA


MORE INFORMATION

To make ANSI control characters work properly, do the following:

  1. Obtain the file ANSI.SYS from the DOS release disk. Put it on the root directory of your boot disk.
  2. Put the statement DEVICE=ANSI.SYS in the CONFIG.SYS file on the root directory of the boot disk.
  3. Reboot. If the message "ANSI.SYS Not Found" displays, the ANSI control codes will not work.
  4. In your Basic program, OPEN the DOS "CON" (console) device for output as #n.
  5. Use PRINT#n to send output to the DOS "CON" (console) device.
  6. CLOSE#n at the end of the program.

For a list of the ANSI escape sequences, refer to the following:

  1. The "Microsoft MS-DOS Version 3.20 User's Reference," Appendix C "Installable Device Drivers"
  2. IBM PC-DOS technical reference manual

The following code demonstrates how to use ANSI screen control codes from Basic:

'The following example changes the background
'and foreground color of the screen, sets blinking and bold characters,
'and positions the cursor:
CLS
escape$ = CHR$(27) + "["
attriboff$ = escape$ + "0m"
blinkon$ = "5m"
bold$ = "1m"
reversv$ = "7m"
red$ = "41m"
green$ = "42m"
blue$ = "44m"
blackback$ = "40m"
x = 30
y = 10
rowcol$ = MID$(STR$(y), 2) + ";" + MID$(STR$(x), 2) + "H"
OPEN "CON" FOR OUTPUT AS 1
PRINT #1, attriboff$;
PRINT #1, escape$ + rowcol$ + "Locate the cursor with ansi.sys"
PRINT #1, attriboff$;
PRINT #1, escape$ + blinkon$ + "Blinking" + attriboff$;
PRINT #1, escape$ + bold$ + "Bold" + attriboff$;
PRINT #1, escape$ + reversv$ + "reverse"
PRINT #1, escape$ + red$ + "red"
PRINT #1, escape$ + green$ + "green"
PRINT #1, escape$ + blue$ + "blue"
CLOSE #1
                


Additional query words: QuickBas BasicCom

Keywords: KB31157