win32 set console text color

Setting console text color is a great way to add spice to your console games. Using a combination of ASCII art and Console coloring you can even make your own RPG game. It's very simple to do, all you need is windows.h.

Most of the colors go from 1 to 16, and after that you get background colors mixed with foreground colors (if you wanted to experiment just cast your number as (Color)).

#include <iostream>
#include <windows.h>

using namespace std;
HANDLE hCon;

enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };

void SetColor (Color c ) {
        if (hCon == NULL )
                hCon = GetStdHandle (STD_OUTPUT_HANDLE );
        SetConsoleTextAttribute (hCon, c );
}

int main ( ) {
  SetColor (RED );
  cout << "InfernoDevelopment.com\n";
  SetColor (DARKRED );
  cout << "Join our forums at www.infernodevelopment.com/forum\n";
  cin. get ( );
  return 0;
}

The SetColor function uses SetConsoleTextAttribute and GetStdHandle to grab the handle to your console and set the current color requested by the Color enum.

After you set the color, you can then use cout like a normal console to print the colored text.

It can really impress your friends who have probably never even seen console text color in their lives!

你可能感兴趣的:(win32 set console text color)