Screen Control with MS-DOS Functions

With version 2.0 or later, if MS-DOS loads the optional device driver ANSI.SYS in response to a DEVICE directive in the CONFIG.SYS file, programs can clear the screen, control the cursor position, and select foreground and background colors by embedding escape sequences in the text output. Escape sequences are so called because they begin with an escape character (1BH), which alerts the driver to intercept and interpret the subsequent characters in the sequence. When the ANSI driver is not loaded, MS-DOS simply passes the escape sequence to the display like any other text, usually resulting in a chaotic screen.

The escape sequences that can be used with the ANSI driver for screen control are a subset of those defined in the ANSI 3.64—1979 Standard. These standard sequences are summarized in Figure 6-2. Note that case is significant for the last character in an escape sequence and that numbers must always be represented as ASCII digit strings, not as their binary values. (A separate set of escape sequences supported by ANSI.SYS, but not compatible with the ANSI standard, may be used for reprogramming and remapping the keyboard.)

Escape sequence Meaning

Esc[2J Clear screen; place cursor in upper left corner (home

position).

Esc[K Clear from cursor to end of line.

Esc[row;colH Position cursor. (Row is the y coordinate in the range

1—25 and col is the x coordinate in the range 1—80 for

80-by-25 text display modes.) Escape sequences

terminated with the letter f instead of H have the same

effect.

Esc[nA Move cursor up n rows.

Esc[nB Move cursor down n rows.

Esc[nC Move cursor right n columns.

Esc[nD Move cursor left n columns.

Esc[s Save current cursor position.

Esc[u Restore cursor to saved position.

Esc[6n Return current cursor position on the standard input

handle in the format Esc[row;colR.

Esc[nm Select character attributes:

0 = no special attributes

1 = high intensity

2 = low intensity

3 = italic

4 = underline

5 = blink

6 = rapid blink

7 = reverse video

8 = concealed text (no display)

30 = foreground black

31 = foreground red

32 = foreground green

33 = foreground yellow

34 = foreground blue

35 = foreground magenta

36 = foreground cyan

37 = foreground white

40 = background black

41 = background red

42 = background green

43 = background yellow

44 = background blue

45 = background magenta

46 = background cyan

47 = background white

Esc[=nh Select display mode:

0 = 40-by-25, 16-color text (color burst off)

1 = 40-by-25, 16-color text

2 = 80-by-25, 16-color text (color burst off)

3 = 80-by-25, 16-color text

4 = 320-by-200, 4-color graphics

5 = 320-by-200, 4-color graphics (color burst off)

6 = 620-by-200, 2-color graphics

14 = 640-by-200, 16-color graphics (EGA and VGA,

MS-DOS 4.0)

15 = 640-by-350, 2-color graphics (EGA and VGA,

MS-DOS 4.0)

16 = 640-by-350, 16-color graphics (EGA and VGA,

MS-DOS 4.0)

17 = 640-by-480, 2-color graphics (MCGA and VGA,

MS-DOS 4.0)

18 = 640-by-480, 16-color graphics (VGA, MS-DOS 4.0)

19 = 320-by-200, 256-color graphics (MCGA and VGA,

MS-DOS 4.0)

Escape sequences terminated with l instead of h have

the same effect.

Esc[=7h Enable line wrap.

Esc[=7l Disable line wrap.

Figure 6-2. The ANSI escape sequences supported by the MS-DOS ANSI.SYS driver. Programs running under MS-DOS 2.0 or later may use these functions, if ANSI.SYS is loaded, to control the appearance of the display in a hardware-independent manner. The symbol Esc indicates an ASCII escape code——a character with the value 1BH. Note that cursor positions in ANSI escape sequences are one-based, unlike the cursor coordinates used by the IBM ROM BIOS, which are zero-based. Numbers embedded in an escape sequence must always be represented as a string of ASCII digits, not as their binary values.