【问题标题】:XCode windows.h and Displaying Colored TextXCode windows.h 和显示彩色文本
【发布时间】: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,但它没有在我当前使用的编译器上运行。我可以使用什么编译器来运行代码示例?

【问题讨论】:

标签: c++ xcode html colors


【解决方案1】:

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

Xcode 控制台不支持颜色。

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

【讨论】:

  • 感谢您的回复。现在我意识到示例代码不能在 XCode 上运行,我可以在什么编译器上运行它?我有一台 PC,但它没有在我使用的编译器 Ubuntu 上运行。
猜你喜欢
  • 2016-02-25
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 2020-03-15
  • 1970-01-01
  • 2017-07-05
  • 2017-06-03
相关资源
最近更新 更多