程序片段----CMD颜色设置

C++ 控制台的字体颜色控制。

错误输出可以使用。


// 设置各种CMD的颜色,缺点是不能恢复默认值(字灰色)

#include "stdafx.h"
#include <Windows.h>
#include <iostream>


inline BOOL SetConsoleTextColor(WORD wAttributes)
{
	if (!SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wAttributes))
	{
		SetLastError(GetLastError());
		return FALSE;
	}
	return TRUE;
}

inline std::ostream& greenColor(std::ostream &out)
{
	SetConsoleTextColor(FOREGROUND_INTENSITY | FOREGROUND_GREEN);
	return out;
}

inline std::ostream& redColor(std::ostream &out)
{
	SetConsoleTextColor(FOREGROUND_INTENSITY | FOREGROUND_RED);
	return out;
}

inline std::ostream& blueColor(std::ostream &out)
{
	SetConsoleTextColor(FOREGROUND_INTENSITY | FOREGROUND_BLUE);
	return out;
}

inline std::ostream& whiteColor(std::ostream &out)
{
	SetConsoleTextColor(FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
	return out;
}

int main()
{
	std::cout << greenColor << "Hello world !" << std::endl;
	std::cout << redColor << "Hello world !" << std::endl;
	std::cout << blueColor << "Hello world !" << std::endl;
	std::cout << whiteColor << "Hello world !" << std::endl;
	return 0;
}





猜你喜欢

转载自blog.csdn.net/u014488388/article/details/78503352