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



The information in this article applies to:

  • Microsoft GW-BASIC 3.2
  • Microsoft GW-BASIC 3.22
  • Microsoft GW-BASIC 3.23
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.1
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
  • Microsoft QuickBASIC 1.0
  • Microsoft QuickBASIC 1.01
  • Microsoft QuickBASIC 1.02
  • Microsoft QuickBASIC 1.0a
  • 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

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
				

Modification Type:MinorLast Reviewed:1/9/2003
Keywords:KB31157