你对控制台程序的第一感受是什么,是否觉得黑漆漆的,死气沉沉?那么就试着让它变的活泼一点吧,下面我们就动手改变下它的字体颜色,思路非常简单,首先获取到标准输出的句柄,然后设置下控制台的文本属性就可以了。
#pragma
once
#include < iostream >
#include < windows.h >
inline std::ostream & blue(std::ostream & s)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE
|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
return s;
}
inline std::ostream & red(std::ostream & s)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); //获取标准输出句柄
SetConsoleTextAttribute(hStdout,FOREGROUND_RED|FOREGROUND_INTENSITY);//设置文本颜色
return s;
}
inline std::ostream & green(std::ostream & s)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout,
FOREGROUND_GREEN|FOREGROUND_INTENSITY);
return s;
}
inline std::ostream & yellow(std::ostream & s)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout,
FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY);
return s;
}
inline std::ostream & white(std::ostream & s)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout,
FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//R,G,B混合就为白色
return s;
}
struct color
{
color(WORD attribute):m_color(attribute){};
WORD m_color;//颜色值
} ;
// 使用模板函数,推荐这种方式
template < class _Elem, class _Traits >
std::basic_ostream < _Elem,_Traits >&
operator << (std::basic_ostream < _Elem,_Traits >& i, color & c)
{
HANDLE hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout,c.m_color);
return i;
}
#include < iostream >
#include < windows.h >
inline std::ostream & blue(std::ostream & s)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE
|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
return s;
}
inline std::ostream & red(std::ostream & s)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); //获取标准输出句柄
SetConsoleTextAttribute(hStdout,FOREGROUND_RED|FOREGROUND_INTENSITY);//设置文本颜色
return s;
}
inline std::ostream & green(std::ostream & s)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout,
FOREGROUND_GREEN|FOREGROUND_INTENSITY);
return s;
}
inline std::ostream & yellow(std::ostream & s)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout,
FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY);
return s;
}
inline std::ostream & white(std::ostream & s)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout,
FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//R,G,B混合就为白色
return s;
}
struct color
{
color(WORD attribute):m_color(attribute){};
WORD m_color;//颜色值
} ;
// 使用模板函数,推荐这种方式
template < class _Elem, class _Traits >
std::basic_ostream < _Elem,_Traits >&
operator << (std::basic_ostream < _Elem,_Traits >& i, color & c)
{
HANDLE hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout,c.m_color);
return i;
}
#include
"
stdafx.h
"
#include " ConsoleColor.h "
int main()
{
std::cout<<red<<"嘿嘿,这可是红色的字体哦"<<std::endl;
std::cout << green << "再看看这个呢,绿色的,喜欢不?"<< white << std::endl;
std::cout << color(FOREGROUND_RED|FOREGROUND_GREEN) << "该死,这颜色嘎难看了!"<< white << std::endl;
system("pause");
}
#include " ConsoleColor.h "
int main()
{
std::cout<<red<<"嘿嘿,这可是红色的字体哦"<<std::endl;
std::cout << green << "再看看这个呢,绿色的,喜欢不?"<< white << std::endl;
std::cout << color(FOREGROUND_RED|FOREGROUND_GREEN) << "该死,这颜色嘎难看了!"<< white << std::endl;
system("pause");
}