12

色付きのテキストをコンソールに出力する方法はありますか?Visual Studio 2010を使用していますが、Windowsで機能するために必要なのはコードだけです。

WindowsのCOLORコマンド以外は見つからなかったのですが、画面全体の色が変わってしまい、出力したい部分だけが変わるものを探しています。私はそれがマネージC++で行われるのを見てきました

例えば、

{color red}
cout << "Hello ";
{color blue}
cout << "world\n";

赤と青で「Helloworld」を生成します。

4

4 に答える 4

30

私はここからこのコードを取りました:

// color your text in Windows console mode
// colors are 0=black 1=blue 2=green and so on to 15=white
// colorattribute = foreground + background * 16
// to get red text on yellow use 4 + 14*16 = 228
// light red on yellow would be 12 + 14*16 = 236
// a Dev-C++ tested console application by vegaseat 07nov2004

#include <iostream>
#include <windows.h> // WinApi header

using namespace std; // std::cout, std::cin

int main()
{
HANDLE hConsole;
int k;

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

// you can loop k higher to see more color choices
for(k = 1; k < 255; k++)
{
// pick the colorattribute k you want
SetConsoleTextAttribute(hConsole, k);
cout << k << " I want to be nice today!" << endl;
}

cin.get(); // wait
return 0;
}
于 2012-02-13T14:40:06.213 に答える
-4

次のように使用される system("") コマンドを使用できます。

cout<<"lol";
system("color 1") // the colours are from 1 to 15. 
cout<<"Coloured text! yay";
于 2012-02-13T14:43:21.513 に答える