INF: How _clearscreen(), ANSI.SYS Affect Text and Cursor Color

ID Number: Q74614

5.10 6.00 6.00a 6.00ax 7.00

MS-DOS

Summary:

The text and cursor color in a Microsoft C program are affected in

various ways by the _settextcolor() function in conjunction with the

_clearscreen() function and whether or not the ANSI.SYS driver is

loaded.

More Information:

The following are several ways text and cursor color are affected in a

C application:

- The function _settextcolor() sets the text color for the functions

_outtext() and _outmem() only.

- The function _clearscreen() will set the text and cursor color to

white by default, but will use a different color if one has been

previously set [for example, with _settextcolor()].

- The ANSI.SYS driver defines functions that change display graphics,

control cursor movement, and reassign keys. Text output is

controlled by ANSI.SYS if it is installed.

The following table summarizes how various colors are affected by

combinations of ANSI.SYS, _clearscreen(), and _settextcolor(). An "X"

indicates that the displayed color was the color set by

_settextcolor(). The results were generated by running the sample code

below with and without ANSI.SYS installed.

-----------------------------------------------------------------------|

| Combinations of ANSI.SYS | _outtext() | printf() | User | Cursor |

| and C Functions* | Output | Output | Input | |

|-----------------------------|------------|----------|-------|--------|

|1. ANSI / CLRSCR / SETTXTCLR | X | | | |

|2. CLRSCR / SETTXTCLR | X | | | |

|3. ANSI / SETTXTCLR / CLRSCR | X | | | X |

|4. SETTXTCLR / CLRSCR | X | X | X | X |

|----------------------------------------------------------------------|

| *ANSI=ANSI.SYS CLRSCR=_clearscreen() SETTXTCLR=_settextcolor() |

------------------------------------------------------------------------

In the first case, the color of _outtext() is affected by

_settextcolor(). The only way that the output of printf(), what the

user types, and the cursor color can be affected is by use of ANSI

escape sequences.

In the second case, the color of _outtext() is affected by

_settextcolor().

In the third case, the color of _outtext() is affected by

_settextcolor(). When the subsequent _clearscreen() is performed, the

text color and the cursor color are affected. However, because

ANSI.SYS is installed, the output of printf() and what the user types

appear in the default white. The only way to change those colors is

through the ANSI escape sequences.

In the fourth case, the color of _outtext() is affected by

_settextcolor(). When the subsequent _clearscreen() is performed, the

text color and the cursor color are affected.

Sample Code

-----------

/* Compile options needed: none

*/

#include <graph.h>

#include < stdio.h>

#include <conio.h>

void main( void )

{

char line[81]; /* declare a buffer for user input */

/* Clear the screen first, then set the text color. Pause so that you

can observe the color of the text output with _outtext() and

printf(), the user input, and the cursor.

*/

_clearscreen( _GCLEARSCREEN );

_settextcolor( 5 );

_outtext( "Enter your name: " );

gets( line );

_outtext( "\n\nUsing outtext: Hello " );

_outtext( line );

printf( "\nUsing printf: Press any key to continue...\n" );

while( !kbhit( ) );

getch( );

/* Set the text color first, then clear the screen. */

_settextcolor( 3 );

_clearscreen( _GCLEARSCREEN );

_outtext( "Enter your name: " );

gets( line );

_outtext( "\n\nUsing outtext: Goodbye " );

_outtext( line );

printf( "\nUsing printf: The end.\n" );

}

Additional reference words: 2.0 2.00 2.01 2.5 2.51 5.1 5.10 6.0 6.00

6.0a 6.00a 6.0ax 6.00ax 7.00