在html5中,您可以通过这样做来做彩色文本:
.foo
{
color: rgb(0,0,0);
}
我最近启动了C ,并希望向用户显示彩色文本。我已经搜索过这个问题,并想出了这样的内容:
http://www.cplusplus.com/forum/beginner/5830/
Duoas的代码段(如下所示)看起来很有趣,我尝试在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 << "tttt Hello World tttt" << 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,但它没有在我当前正在使用的编译器上运行。我可以使用什么编译器来运行代码示例?
您要使用的示例代码对Windows控制台高度特异,并且在MacOS中没有直接等效。
Xcode控制台不支持颜色。
如果要在MacOS终端(而不是Xcode控制台)中运行应用程序,则可以使用Ncurses显示颜色。另外,您可以直接使用颜色代码逃脱。