Advertisement

Does anyone know how to print text in color?

Started by January 15, 2003 01:58 PM
0 comments, last by SSJCORY 21 years, 10 months ago
Does anyone know how to print text in color in console? I heard it was os specific. If it is I have windows 98. Please help. I looked all over. Cory Fisher
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
There is a function in the windows header file (windows.h) which should help you: SetConsoleTextAttribute(). Here is a simple, yet effective, example I found, but I think you should visit MSDN for more information.


#include <windows.h>

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

void TextColour(int forg, int backg)
{
if (forg < 0 || forg > 15 || backg < 0 || backg > 15)
SetConsoleTextAttribute(handle, 7);
else
SetConsoleTextAttribute(handle, backg * 16 + forg);
}


Here are the codes for the colours:

0: Black
1: Blue
2: Green
3: Cyan
4: Red
5: Magenta
6: Brown
7: White
8: Gray
9: Light Blue
10: Light Green
11: Light Cyan
12: Light Red
13: Light Magenta
14: Yellow
15: High Intensity White

So, for example, one way to call it would be:


TextColour(10,0);


I usually set mine up so that I have default values, generally 0 for backg so you can call with just forground, as you normally want to keep the console background black. I also usually set up forg to have a default value of 7 (the normal console colour) so you can call the function like: TextColour(); to return to the original console colours.

:::: Lektrix ::::
[ Google || ACCU || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on January 15, 2003 3:15:20 PM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]

This topic is closed to new replies.

Advertisement