consle color

I need to know how to set the color of
a console program. I can use system("color 01")
but you can only use that on xp. Can you help with this?
C++ doesn't really acknowledge that there is such a thing as a console. Because of this, I don't know of a standard way to change the color of console text across all platforms. I'd recommend installing a library called pdcurses in order to do things like fancy console I/O.
http://pdcurses.sourceforge.net/

Nonetheless, if you cannot use curses, perhaps this link might help you. Happy coding. :)
http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/

-Albatross
Last edited on
UH For windows...
Here is a small program showing color text and backgrounds. Look it over and apply what you need to your program..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Text Color.cpp : main project file.

#include <stdafx.h>
#include <stdio.h>
#include "conio.h"
#include <iostream>
#include <windows.h>

using namespace std;

/*
The different color codes are

0   BLACK
1   BLUE
2   GREEN
3   CYAN
4   RED
5   MAGENTA
6   BROWN
7   LIGHTGRAY
8   DARKGRAY
9   LIGHTBLUE
10  LIGHTGREEN
11  LIGHTCYAN
12  LIGHTRED
13  LIGHTMAGENTA
14  YELLOW
15  WHITE
*/
int main ( void )
{
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  WORD wOldColorAttrs;
  CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

  /*
   * First save the current color information
   */
  GetConsoleScreenBufferInfo(h, &csbiInfo);
  wOldColorAttrs = csbiInfo.wAttributes;

  /*
   * Set the new color information
   */
  SetConsoleTextAttribute ( h, FOREGROUND_RED & FOREGROUND_BLUE | BACKGROUND_BLUE  | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY );

  printf ( "This is a test\n" );

  /*
   * Restore the original colors
   */
  SetConsoleTextAttribute ( h, wOldColorAttrs);
  return 0;
why I type
SetConsoleTextAttribute ( h, FOREGROUND_RED & FOREGROUND_BLUE );
printf("YA\n");
SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_BLUE );
printf("YA\n");
the former can't show anything but the latter can?
1
2
3
4
5
6
7
8
9
10
#include <windows.h>
#include <iostream>
int main()
{
	for (int colour = 0x00; colour <= 0xff; colour ++)
	{
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),colour),	std::cout << "Using colour:" << colour << std::endl;
		cin.get();
	}
}


Try this out. It works for me using VS2008 on XP and on VS2010 with Windows 7.
Last edited on
yet5438 wrote:
the former can't show anything but the latter can?
bcause '&' excludes and '|' includes the values:

010 & 001 = 000
010 | 001 = 011
Topic archived. No new replies allowed.