Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在变量中存储彩色文本?_C++_String_Winapi_Colors - Fatal编程技术网

C++ 如何在变量中存储彩色文本?

C++ 如何在变量中存储彩色文本?,c++,string,winapi,colors,C++,String,Winapi,Colors,我想将文本及其颜色存储在变量中,然后我可以cout。 我试过用绳子这样做,但不起作用。 我希望在不同的单词上有不同的颜色,并将其存储在一个变量中 string e = "text\ntext" + SetConsoleTextAttribute(hConsole,64) + "\ntext" + SetConsoleTextAttribute(hConsole,32); cout<<e; 对于第一个函数,SetCons

我想将文本及其颜色存储在变量中,然后我可以
cout
。 我试过用绳子这样做,但不起作用。 我希望在不同的单词上有不同的颜色,并将其存储在一个变量中

string e = "text\ntext" + SetConsoleTextAttribute(hConsole,64) + 
           "\ntext" + SetConsoleTextAttribute(hConsole,32);
cout<<e;

对于第一个函数,SetConsoleTextAttribute函数-“设置由WriteFile或WriteConsole函数写入控制台屏幕缓冲区的字符的属性,或由ReadFile或ReadConsole函数回显的字符的属性。此函数影响函数调用后写入的文本。”这意味着它不会以任何方式影响变量,如果它有问题,它将返回0,如果它正常,则返回任何其他值。此函数仅影响控制台输出(函数调用后写入的文本)

例如:

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),64);
    cout << "text\ntext";
SetConsoleTextAttribute(GetStdHandle(STD\u输出\u句柄),64);

cout明确地强制转换
SetConsoleTextAttribute()
std::string(SetConsoleTextAttribute(hConsole,64))
std::cout
不知道如何更改颜色;它只知道如何写字符。没有什么可以放进一个字符串中,这会导致<>代码STD::CUT改变颜色。C++中有没有其他改变文本颜色的方法?C++没有颜色的概念。只有控制台可以。因此,除非控制台通过
cout
接受颜色转义码,否则您将不得不使用特定于控制台的API。
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),64);
    cout << "text\ntext";