Mail Archives: djgpp/1996/12/06/21:26:52
This is a multi-part message in MIME format.
--------------7FF06D964756
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
A.Appleyard wrote:
>
> In v1, and in v2, when writing to screen in the ordinary text mode by fprint()
> etc, how can I tell the PC to start writing with such-and-such a foreground
> and background color?
If you're using printf()/fprintf() to write to the screen, then there is
no standard way to change colors. You have two choices:
Best: Convert all the output code in your program to use the <conio.h>
functions, which support all sorts of fun stuff like colors, windows,
etc. However, unless you convert your entire program to use <conio>,
you risk some nasty problems.
Example:
/* Set text color to bright blue on black. */
/* Formula: ( blink [0-1] << 7 ) + ( bg [0-7] ) << 4 + fg [0-15] */
textattr( ( 0x0 << 7 ) + ( 0x0 << 4 ) + 0x9 );
cprintf( "Hello there!\n" );
Easiest: Load ANSI.SYS in your CONFIG.SYS, and use ANSI escape
sequences in your printf() strings. This is quick and relatively
painless, once you learn the ANSI codes. However, it _requires_ the
presence of ANSI.SYS on the host computer to work, so it is both awkward
and very non-portable.
Example:
/* Set text color to bright blue on black. */
/* See MS-DOS help for ANSI.SYS for syntax of ESC sequences. */
printf( "\033[0;1;34;40m" ); /* \033 is the ESC character */
printf( "Hello there!\n" );
Attached is a brief program that illustrates the use of conio functions
to display colored text. Enjoy!
--
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I | fighteer AT cs DOT com |
| Proud owner of what might one | http://www.cs.com/fighteer |
| day be a spectacular MUD... | Plan: To make Bill Gates suffer |
---------------------------------------------------------------------
--------------7FF06D964756
Content-Type: text/plain; charset=us-ascii; name="COLORTST.C"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="COLORTST.C"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <conio.h>
void show_usage ( const char * );
int main( int argc, char **argv )
{
char msg[1024];
char blink, bg, fg;
int i;
srandom( (int) time( 0 ) );
blink = random( ) % 2;
bg = random( ) % 8;
fg = random( ) % 16;
if ( argc > 1 )
if ( !isdigit( argv[1][0] ) ||
( blink = atoi( argv[1] ) ) < 0 || blink > 1 )
{
show_usage( argv[0] );
exit( 1 );
}
if ( argc > 2 )
if ( !isdigit( argv[2][0] ) ||
( bg = atoi( argv[2] ) ) < 0 || bg > 7 )
{
show_usage( argv[0] );
exit( 1 );
}
if ( argc > 3 )
if ( !isdigit( argv[3][0] ) ||
( fg = atoi( argv[3] ) ) < 0 || fg > 15 )
{
show_usage( argv[0] );
exit( 1 );
}
/* Grab text from command line? */
strcpy( msg, "Hello there!" );
for ( i = 4; i < argc; i++ )
{
if ( i == 4 )
strcpy( msg, argv[i] );
else
strcat( msg, argv[i] );
strcat( msg, " " );
}
textattr( ( blink << 7 ) + ( bg << 4 ) + fg );
cprintf( "%s\n", msg );
return 0;
}
void show_usage( const char *progname )
{
fprintf( stderr, "\
Sets the text color to the color values indicated on the command line, and
displays a string in that color. If no text is specified, \"Hello, there!\"
is printed. If any colors are omitted, their value is randomly selected from
all available values.
Usage: %s [ blink [ bg [ fg [ text ... ]]]]
blink Blinking text [0 - 1]
0 : normal
1 : blinking
bg Background color [0 - 7]
0 : black 4 : red
1 : blue 5 : magenta
2 : green 6 : yellow
3 : cyan 7 : white
fg Foreground color [0 - 15]
Uses same colors as background. Add 8 to the number
for high-intensity text.
"
, progname );
return;
}
--------------7FF06D964756--
- Raw text -