C ++로 콘솔의 텍스트 색상 지정
C ++로 콘솔에 컬러 텍스트를 어떻게 쓸 수 있습니까? 즉, 다른 색상으로 다른 텍스트를 어떻게 쓸 수 있습니까?
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// you can loop k higher to see more color choices
for(int k = 1; k < 255; k++)
{
// pick the colorattribute k you want
SetConsoleTextAttribute(hConsole, k);
cout << k << " I want to be nice today!" << endl;
}
문자 속성 "k"값을 해석하는 방법은 다음과 같습니다.
표준 C ++에는 '색상'이라는 개념이 없습니다. 그래서 당신이 묻는 것은 운영 체제에 따라 다릅니다.
Windows의 경우 SetConsoleTextAttribute 함수를 확인할 수 있습니다 .
* nix에서는 ANSI 이스케이프 시퀀스 를 사용해야합니다 .
ANSI 이스케이프 색상 코드 :
Name BG FG
Black 30 40
Red 31 41
Green 32 42
Yellow 33 43
Blue 34 44
Magenta 35 45
Cyan 36 46
White 37 47
Bright Black 90 100
Bright Red 91 101
Bright Green 92 102
Bright Yellow 93 103
Bright Blue 94 104
Bright Magenta 95 105
Bright Cyan 96 106
Bright White 97 107
C / C ++ 용 샘플 코드 :
#include <iostream>
#include <string>
int main(int argc, char ** argv){
printf("\n");
printf("\x1B[31mTexting\033[0m\t\t");
printf("\x1B[32mTexting\033[0m\t\t");
printf("\x1B[33mTexting\033[0m\t\t");
printf("\x1B[34mTexting\033[0m\t\t");
printf("\x1B[35mTexting\033[0m\n");
printf("\x1B[36mTexting\033[0m\t\t");
printf("\x1B[36mTexting\033[0m\t\t");
printf("\x1B[36mTexting\033[0m\t\t");
printf("\x1B[37mTexting\033[0m\t\t");
printf("\x1B[93mTexting\033[0m\n");
printf("\033[3;42;30mTexting\033[0m\t\t");
printf("\033[3;43;30mTexting\033[0m\t\t");
printf("\033[3;44;30mTexting\033[0m\t\t");
printf("\033[3;104;30mTexting\033[0m\t\t");
printf("\033[3;100;30mTexting\033[0m\n");
printf("\033[3;47;35mTexting\033[0m\t\t");
printf("\033[2;47;35mTexting\033[0m\t\t");
printf("\033[1;47;35mTexting\033[0m\t\t");
printf("\t\t");
printf("\n");
return 0;
}
GCC :
g++ cpp_interactive_terminal.cpp -o cpp_interactive_terminal.cgi
chmod +x cpp_interactive_terminal.cgi
./cpp_interactive_terminal.cgi
메소드를 작성하고 다음과 같이 호출 할 수 있습니다.
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
int col=12;
// 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
FlushConsoleInputBuffer(hConsole);
SetConsoleTextAttribute(hConsole, col);
cout << "Color Text";
SetConsoleTextAttribute(hConsole, 15); //set back to black background and white text
Windows 콘솔 창에 대해 이야기하고 있다고 가정하고 MSDN Library 설명서에서 콘솔 기능을 찾아보십시오.
Otherwise, or more generally, it depends on the console. Colors are not supported by the C++ library. But a library for console handling may/will support colors. E.g. google "ncurses colors".
For connected serial terminals and terminal emulators you can control things by outputting "escape sequences". These typically start with ASCII 27 (the escape character in ASCII). There is an ANSI standard and a lot of custom schemes.
I'm not sure what you really want to do, but my guess is you want your C++ program to output colored text in the console, right ? Don't know about Windows, but on all Unices (including Mac OS X), you'd simply use ANSI escape sequences for that.
In Windows, you can use any combination of red green and blue on the foreground (text) and the background.
/* you can use these constants
FOREGROUND_BLUE
FOREGROUND_GREEN
FOREGROUND_RED
FOREGROUND_INTENSITY
BACKGROUND_BLUE
BACKGROUND_GREEN
BACKGROUND_RED
BACKGROUND_INTENSITY
*/
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::cout << "I'm cyan! Who are you?" << std::endl;
Windows 10에서는 다음과 같이 이스케이프 시퀀스를 사용할 수 있습니다.
#ifdef _WIN32
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_VIRTUAL_TERMINAL_PROCESSING);
#endif
// print in red and restore colors default
std::cout << "\033[32m" << "Error!" << "\033[0m" << std::endl;
할 수있는 가장 간단한 방법은 다음과 같습니다.
#include <stdlib.h>
system("Color F3");
여기서 "F"는 배경색에 대한 코드이고 3은 텍스트 색상에 대한 코드입니다.
다른 색상 조합을 보려면 주변을 엉망으로 만드십시오.
system("Color 1A");
cout << "Hello, what is your name?" << endl;
system("Color 3B");
cout << "Hello, what is your name?" << endl;
sytem("Color 4c");
cout << "Hello, what is your name?" << endl;
전체 화면을 색상으로 채우지 않으려면 "system ("Color… ")"을 사용하지 마십시오. 다음은 컬러 텍스트를 만드는 데 필요한 스크립트입니다.
#include <iostream>
#include <windows.h>
int main()
{
const WORD colors[] =
{
0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x6F,
0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6
};
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
WORD index = 0;
SetConsoleTextAttribute(hstdout, colors[index]);
std::cout << "Hello world" << std::endl;
FlushConsoleInputBuffer(hstdin);
return 0;
}
여기 cplusplus 예제 는 콘솔에서 색상을 사용하는 방법의 예입니다.
참고 URL : https://stackoverflow.com/questions/4053837/colorizing-text-in-the-console-with-c
'development' 카테고리의 다른 글
현재 .NET SDK는 .NET Core 2.1 대상 지정을 지원하지 않습니다. (0) | 2020.11.07 |
---|---|
자세한 가비지 컬렉션 출력을 파일로 리디렉션하는 방법은 무엇입니까? (0) | 2020.11.07 |
CSS에서 미디어 쿼리의 순서가 중요한 이유는 무엇입니까? (0) | 2020.11.07 |
E : gnupg, gnupg2 및 gnupg1이 설치되지 않은 것 같지만이 작업을 수행하려면 둘 중 하나가 필요합니다. (0) | 2020.11.07 |
유용한 (어려운) SQL 스크립트 라이브러리 (0) | 2020.11.07 |