Changing Color Of Text

Oct 11, 2011 at 5:42pm
Hello,

How do you change the color of input in a console application, and how do you change the color of output?
Any help would be appreciated.
Last edited on Oct 11, 2011 at 6:08pm
Oct 11, 2011 at 6:14pm
What systems are you targeting?
Windows?
Unix?
Both?
Oct 11, 2011 at 6:17pm
Windows.
Oct 11, 2011 at 6:29pm
Use SetConsoleTextAttribute() before every input or output you wish to colorize:

 
#include <windows.h> 
 
SetConsoleTextAttribute( GetStdHandle( STD_INPUT_HANDLE ), color );


The color is the EGA/VGA color attribute text. Your program cannot control the blink/intensity attribute -- that is something that your user sets -- but Windows consoles by default use intensity (so that you can use all 16 colors in the background). An enum for you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
enum
  {
  COLOR_BLACK,
  COLOR_DBLUE,
  COLOR_DGREEN,
  COLOR_DCYAN,
  COLOR_DRED,
  COLOR_DMAGENTA,
  COLOR_DYELLOW,  COLOR_BROWN = COLOR_DYELLOW,
  COLOR_LGRAY,
  COLOR_DGRAY,
  COLOR_LBLUE,
  COLOR_LGREEN,
  COLOR_LCYAN,
  COLOR_LRED,
  COLOR_LMAGENTA,
  COLOR_LYELLOW, COLOR_YELLOW = COLOR_LYELLOW,
  COLOR_WHITE
  };

Hope thsi helps.
Oct 11, 2011 at 6:59pm
:)

Thanks Duoas.

Just curious though:

What would you use for both Linux and Windows?
Oct 12, 2011 at 2:35am
Depends on how complicated your application is.
For heavy stuff, use NCurses/PDCurses.
For simple stuff, just use some #ifdefs to switch between Win API and terminfo code.
Topic archived. No new replies allowed.