すべての文字を別の色にしたい。例えばテキストと背景色を変更するには?
、
cout << "Hello world" << endl;
- Hが青色になり赤色
- Eあろう
- L などオレンジであろう。
私はこれを行うことができます知っている、私はちょうどそのコードを知らない。
と背景色を白に変更したい。どうすればいい?
すべての文字を別の色にしたい。例えばテキストと背景色を変更するには?
、
cout << "Hello world" << endl;
私はこれを行うことができます知っている、私はちょうどそのコードを知らない。
と背景色を白に変更したい。どうすればいい?
これを行うための(標準的な)クロスプラットフォームの方法はありません。 Windowsでは、conio.h
を試してみてください。 :
textcolor(); // and
textbackground();
機能を備えています。例えば
:
textcolor(RED);
cprintf("H");
textcolor(BLUE);
cprintf("e");
// and so on.
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdOut, FOREGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
これは、白い背景に赤いテキストを生成します。
またPDCursesライブラリを使用することができます。 (http://pdcurses.sourceforge.net/)
`enter code here`#include <stdafx.h> // Used with MS Visual Studio Express. Delete line if using something different
#include <conio.h> // Just for WaitKey() routine
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // For use of SetConsoleTextAttribute()
void WaitKey();
int main()
{
int len = 0,x, y=240; // 240 = white background, black foreground
string text = "Hello World. I feel pretty today!";
len = text.length();
cout << endl << endl << endl << "\t\t"; // start 3 down, 2 tabs, right
for (x=0;x<len;x++)
{
SetConsoleTextAttribute(console, y); // set color for the next print
cout << text[x];
y++; // add 1 to y, for a new color
if (y >254) // There are 255 colors. 255 being white on white. Nothing to see. Bypass it
y=240; // if y > 254, start colors back at white background, black chars
Sleep(250); // Pause between letters
}
SetConsoleTextAttribute(console, 15); // set color to black background, white chars
WaitKey(); // Program over, wait for a keypress to close program
}
void WaitKey()
{
cout << endl << endl << endl << "\t\t\tPress any key";
while (_kbhit()) _getch(); // Empty the input buffer
_getch(); // Wait for a key
while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}
機能system
を使用できます。
system("color *background**foreground*");
、0から番号を入力 - 9またはAからの手紙 - 例えばF.
:
system("color A1");
std::cout<<"hi"<<std::endl;
文字を表示するであろう "HI"緑色の背景と青色のテキストが付いています。
だけで入力し、すべての色の選択肢を表示するには:
system("color %");
を何色を表すものを数字または文字を参照すること。
色はビットエンコードされます。 C++言語でテキストの色を変更したい場合は、さまざまな方法があります。コンソールでは、出力のプロパティーを変更できます。 click this icon of the console and go to properties and change color.
2番目の方法はシステムカラーを呼び出すことです。
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
//Changing Font Colors of the System
system("Color 7C");
cout << "\t\t\t ****CEB Electricity Bill Calculator****\t\t\t " << endl;
cout << "\t\t\t *** MENU ***\t\t\t " <<endl;
return 0;
}
コードにいくつかの説明を追加してください –