2016-12-05 143 views
1

在HTML5中,你可以通过做这样使彩色文本:的XCode WINDOWS.H和显示彩色文字

.foo 
{ 
    color: rgb(0,0,0); 
} 

我最近开始C++,并想显示彩色文本给用户。我GOOGLE了这一点,并用东西想出了这样的:

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 << "\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,但它没有运行在我目前使用的编译器上。我可以使用什么编译器来运行代码示例?

+0

在这里有一个厕所: http://stackoverflow.com/questions/9158150/colored-output-in-c – carrotcake

+0

好的,这有很大的帮助。但是,我仍然无法运行代码。尽管感谢您的快速回复! –

回答

0

您尝试使用的示例代码与Windows控制台高度特定,并且在macOS中没有直接的等效项。

Xcode控制台不支持颜色。

如果您在macOS终端(不是Xcode控制台)上运行应用程序,则可以使用use ncurses to display colors。或者,you can use color code escapes directly

+0

感谢您的回复。现在我意识到示例代码无法在XCode上运行,我可以在什么编译器上运行它?我有一台PC,但它没有运行在我使用的编译器Ubuntu上。 –