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!
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!
相关文章推荐
- Android_setTextColor无效解决办法
- Android中设置TextView的颜色setTextColor
- Set terminal text color and transparent background
- Android编程设置TextView颜色setTextColor用法实例
- android.textiview.settextcolor失效的问题
- 利用SetConsoleTextAttribute函数设置控制台颜色
- Android进阶(二十五)setTextColor()的参数设置方式
- Android中设置TextView的颜色setTextColor
- 【Android基础】setTextColor(),setBackgroundColor的参数设置方式
- SetConsoleTextAttribute设置颜色后的恢复
- 设置按钮等文本,得用[Button setTitleColor:textColor forState:UIControlStateNormal]
- 通过代码setTextColor,改变不同状态下的textColor
- Windows文本字体函数 - SetTextColor
- setTextColor属性
- android setTextColor()的参数设置不起作用的解决方法
- TextView.setTextColor(int); 括号里那个颜色int值的理解
- SetConsoleTextAttribute 函数--设置控制台文本属性
- set text color
- android 代码设置颜色setTextColor
- 同时兼容高低版本的setBackground跟setTextColor方法