我如何写彩色文本控制台与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”值被如何解释。

其他提示

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库文档中的控制台功能。

否则,或更一般地,它取决于在控制台上。颜色是不是由C ++库支持。但对于处理控制台库可/支持的颜色。例如。谷歌 “的ncurses颜色”。

有关连接的串行端子并且可以通过输出“转义序列”控制的事情终端仿真器。这些通常开始与ASCII 27(在ASCII转义字符)。有一个ANSI标准和大量的定制方案。

我不知道你真正想做的事,但我的猜测是,你希望你的C ++程序来输出彩色文本控制台,对不对?不知道有关Windows,但在所有的Unix系统(包括Mac OS X),你只需使用 ANSI逃生序列了解这一点。

在Windows中,您可以使用前景(文本)和背景红,绿,蓝的任意组合。

/* 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;

来源: HTTPS ://msdn.microsoft.com/en-us/library/windows/desktop/ms682088(v = vs.85)的.aspx#_win32_character_attributes

在视窗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;

不,如果你不想用颜色填充整个屏幕都使用“系统(”颜色...“)”。这是为了使彩色文本所需的脚本:

#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例如是示例了如何在控制台使用的颜色。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top