您的位置:首页 > 其它

win32 set console text color

2013-12-24 02:55 253 查看
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!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: