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

Last reviewed: July 17, 1997
Article ID: Q74614
5.10 6.00 6.00a 6.00ax 7.00 | 1.00 1.50
MS-DOS                      | WINDOWS
kbprg

The information in this article applies to:

  • The C Run-time (CRT), included with:

        - Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, and 6.0ax
        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
    

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: kbinf 5.10 6.00 6.00a 6.00ax 7.00 1.00 1.50
KBCategory: kbprg
KBSubcategory: CRTIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.