Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.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++。我目前正在使用Sololearn和Google作为资源和代码块作为我的IDE,我想从Batch first移植一些小项目,我不想使用GUI或扩展之类的东西_C++_Winapi - Fatal编程技术网

打印彩色文本,无需额外输出 我想学习C++。我目前正在使用Sololearn和Google作为资源和代码块作为我的IDE,我想从Batch first移植一些小项目,我不想使用GUI或扩展之类的东西

打印彩色文本,无需额外输出 我想学习C++。我目前正在使用Sololearn和Google作为资源和代码块作为我的IDE,我想从Batch first移植一些小项目,我不想使用GUI或扩展之类的东西,c++,winapi,C++,Winapi,现在我有一个问题 我正在搜索打印彩色文本的函数,我修改了一个代码段(找到),如下所示: void colorText(string ct, int col) { HANDLE hConsole; hConsole = GetStdHandle(STD_OUTPUT_HANDLE); FlushConsoleInputBuffer(hConsole); SetConsoleTextAttribute(hConsole, col); cout <&l

现在我有一个问题

我正在搜索打印彩色文本的函数,我修改了一个代码段(找到),如下所示:

void colorText(string ct, int col)
{

   HANDLE  hConsole;
   hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

   FlushConsoleInputBuffer(hConsole);
   SetConsoleTextAttribute(hConsole, col);

      cout << ct;

   SetConsoleTextAttribute(hConsole, 15); //set back to black background and white text
}
CMD.EXE was started with the Path given above as the current Directory.
UNC-Paths aren't supported.
The Windows - Directory will be set as current Directory.
但是,这样做时,我会得到不必要的额外输出,如下所示:

void colorText(string ct, int col)
{

   HANDLE  hConsole;
   hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

   FlushConsoleInputBuffer(hConsole);
   SetConsoleTextAttribute(hConsole, col);

      cout << ct;

   SetConsoleTextAttribute(hConsole, 15); //set back to black background and white text
}
CMD.EXE was started with the Path given above as the current Directory.
UNC-Paths aren't supported.
The Windows - Directory will be set as current Directory.

现在,有人知道如何通过使用不同的着色或静音技术来避免这种情况吗?

GetStdHandle可能返回一个与您运行的调试控制台不兼容的句柄。在程序启动时调用函数一次并保持句柄可能会更好。

我很确定此输出与颜色无关,只是由Code::Blocks启动终端的方式引起的。它也出现在一个简单的“你好世界”上吗?@Quentin不,实际上不是。它的正常输出是运行时,“按任意键”Color函数添加了上面的函数,我通过注释调用测试了这一点。如果你想学习,不要从最糟糕的IDE开始。从那里下载并获取它。代码::块的默认值很差,这会妨碍构建应用程序,而且几乎不受支持。@IInspectable是的,谢谢。显然,这都是关于调试器的。Visual Studio中没有出现此错误…我只是尝试了Visual Studio。。。哇。这实际上只是关于调试器的问题。VisualStudio对此完全没有问题,并且不会发生错误。谢谢你的帮助