【发布时间】:2017-04-19 07:33:33
【问题描述】:
在 html5 中,您可以通过以下方式制作彩色文本:
.foo
{
color: rgb(0,0,0);
}
我最近开始使用 c++,并希望向用户显示彩色文本。我用谷歌搜索了这个并想出了这样的东西:
http://www.cplusplus.com/forum/beginner/5830/
Duoas 的代码 sn-p(如下所示)看起来很有趣,我尝试在 XCode 上运行它。
#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;
// Remember how things were when we started
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo( hstdout, &csbi );
// Tell the user how to stop
SetConsoleTextAttribute( hstdout, 0xEC );
std::cout << "Press any key to quit.\n";
// Draw pretty colors until the user presses any key
while (WaitForSingleObject( hstdin, 100 ) == WAIT_TIMEOUT)
{
SetConsoleTextAttribute( hstdout, colors[ index ] );
std::cout << "\t\t\t\t Hello World \t\t\t\t" << std::endl;
if (++index > sizeof(colors)/sizeof(colors[0]))
index = 0;
}
FlushConsoleInputBuffer( hstdin );
// Keep users happy
SetConsoleTextAttribute( hstdout, csbi.wAttributes );
return 0;
}
但是,我得到的错误是
2:21: fatal error: windows.h: No such file or directory
compilation terminated.
所以这是我的问题: XCode 中是否有替代 windows.h 的方法? 如果没有,您如何向控制台显示颜色?
(注意:我正在尝试让我的 hello world 程序以另一种颜色显示 hello world,而不是黑色)
感谢任何帮助。 谢谢!
编辑: 好的,感谢duskwuff的回答,我知道windows.h只能在windows上运行。我有一台 PC,但它没有在我当前使用的编译器上运行。我可以使用什么编译器来运行代码示例?
【问题讨论】:
-
好的,这很有帮助。但是,我仍然无法运行代码。感谢您的快速回复!