Coloring console

Can someone explain and show me some examples of how to "color" the console without using "system("color a");"?
You should use WinAPI (or, if you are on Linux, ncurses library: better than pass escape sequences yourself)
http://www.infernodevelopment.com/set-console-text-color
@MiiNiPaa thanks that link doesn't really explain it much I'll look around
Just a note people I'm on windows just looking for a way around system color the WinAPI worked I just want an explaination
Ok, I will boil down example in my link:
1
2
3
4
5
6
7
8
int main()
{
    HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);;
    SetConsoleTextAttribute(hCon, 9);
  
    std::cout << "Test";
    std::cin.get();
}


HANDLE is a window descriptor. Can be seen as pointer to window.
We calling GetStdHandle() on it. Whe called with enum parameter STD_OUTPUT_HANDLE it will return handle of window where all standard output is (console).
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx

Next we are calling SetConsoleTextAttribute() WinAPI function. It takes 2 parameters. First one is console handle (we get it earlier). Second is character attribute. 9 is blue text on black background.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686047%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088%28v=vs.85%29.aspx#_win32_character_attributes
Hmm ok I somewhat understand now I just now have a question:
is hCon a keyword or does it just pick it out of thin air?
You can name it w/e you want. HANDLE dkwodkwo would work. :P
It is just a name of the variable.
Like int x; or std::vector<double> values; HANDLE hCon;
Why do you only need to declare DARKBLUE as 1 and not the rest of the colors as numbers?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <windows.h>
using namespace std;

//returns the current attributes
WORD GetConsoleTextAttribute (HANDLE hCon)
{
  CONSOLE_SCREEN_BUFFER_INFO con_info;
  GetConsoleScreenBufferInfo(hCon, &con_info);
  return con_info.wAttributes;
}

int main (void)
{
  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  const int saved_colors = GetConsoleTextAttribute(hConsole);

  SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
  cout << "This text should be blue" << endl;
  SetConsoleTextAttribute(hConsole, saved_colors);
}
Last edited on
Why do you only need to declare DARKBLUE as 1 and not the rest of the colors as numbers?
It is better to use windows defined constants.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088%28v=vs.85%29.aspx#_win32_character_attributes

Autor declared DARKBLUE as 1 because he make use of enumeration properties.
show me some examples of how to "color" the console

Assorted cplusplus.com articles:

Add color to your console window
http://www.cplusplus.com/articles/2ywTURfi/

Add color to your console 2
http://www.cplusplus.com/articles/Eyhv0pDG/

and

Powerful Console in Windows/VC++ (Color, Cursor, ...)
http://www.cplusplus.com/articles/SEN36Up4/

Plus searching this site for SetConsoleTextAttribute will find loads of exisiting examples, inc. console games.

Note that if your console background is not the default black, you will have to use GetConsoleScreenBufferInfo as well as SetConsoleTextAttribute when you set the foreground color, so you do not lose the background color (the character attribute is foreground color plus background color plus -- sometimes -- multi-byte character set attributes.)

(See the "Character Attributes" section on this page (as MiiNiPaa has already mentioned):
Console Screen Buffers -- Character Attributes
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088%28v=vs.85%29.aspx#_win32_character_attributes )

Andy

PS For those of you who (also) use Linux, if you're distro's terminal implementation supports ANSI escape codes, you can use the same kind of apprroach as this hex view tool:

Hex viewer
http://www.cplusplus.com/articles/NwTbqMoL/
Last edited on
Topic archived. No new replies allowed.