更改输出文本的颜色 C++

发布于 2024-12-15 10:05:02 字数 277 浏览 2 评论 0原文

我试图更改一些 cout 输出的颜色,但之后 cout 再次变为相同的颜色。

我尝试过以下代码:

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
        cout << "Help-Please" << endl;

但即使在 cout 完成后它仍然会继续。另外要补充的是,其中一些颜色变化将在 if 语句内进行。您建议我添加什么代码才能达到这种效果?

Im trying to change the color of some of my cout outputs but after that cout to be again the same color.

Ive tried the following code:

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
        cout << "Help-Please" << endl;

but it continues even after the cout has been completed. Also to add that some of these color changes will be inside if statements. What code would you recommend me to add to to get this effect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美煞众生 2024-12-22 10:05:02

文本颜色保持有效,您必须使用其他(之前的)颜色再次调用 SetConsoleTextAttribute。

编辑:每个文本输出将使用上次调用 SetConsoleTextAttribute 的颜色,因此您可以在不同的行(或单词/字符)中使用不同的颜色。

The text color stays in effect, you must call SetConsoleTextAttribute again with other (the previous) colors.

Edit: Each text output will use the colors of the last call to SetConsoleTextAttribute, so you can use different colors in different lines (or words/characters).

夜声 2024-12-22 10:05:02

在 stdlib.h 中有一个颜色命令可以更改所有文本颜色。
system("color 0B");

in the stdlib.h there is a color command that changes all the the text colors.
Ie system("color 0B");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文