为什么颜色对标准标准不起作用?(PDCurses)
Why aren't colors working for stdscr? (PDCurses)
因此,我正在使用pdcurses为控制台应用程序添加一些颜色,但我遇到了问题。如果我制作第二个窗口并尝试为其输出着色,它会很好地工作,但如果我尝试为stdscr的输出着色,则不会发生任何事情。
我想继续使用stdscr,而不是用另一个窗口覆盖它,因为stdscr将正常接收我发送到stdout的输出,允许我使用C++风格的控制台接口。通过将输出发送到cout,它将进入stdscr,这是我目前所知道的使用pdcurses的C++接口的唯一方法。此外,其他库偶尔会将其输出直接发送到stdout,如果我使用stdscr,则输出不会丢失(我非常了解lua的print
函数)。
以下是一些示例代码:
// This prints a red '>' in the inputLine window properly. //
wattron(inputLine, A_BOLD | COLOR_PAIR(COLOR_RED));
wprintw(inputLine, "n> ");
wattroff(inputLine, A_BOLD | COLOR_PAIR(COLOR_RED));
// This prints a light grey "Le Testing." to stdscr. Why isn't it red? //
wattron(stdscr, A_BOLD | COLOR_PAIR(COLOR_RED));
cout << "nLe Testing.n";
wattroff(stdscr, A_BOLD | COLOR_PAIR(COLOR_RED));
// This does nothing. I have no idea why. //
wattron(stdscr, A_BOLD | COLOR_PAIR(COLOR_RED));
wprintw(stdscr, "nLe Testing.n");
wattroff(stdscr, A_BOLD | COLOR_PAIR(COLOR_RED));
以下是我如何初始化pdcurses:
// Open the output log which will mimic stdout. //
if (userPath)
{
string filename = string(userPath) + LOG_FILENAME;
log.open(filename.c_str());
}
// Initialize the pdCurses screen. //
initscr();
// Resize the stdout screen and create a line for input. //
resize_window(stdscr, LINES - 1, COLS);
inputLine = newwin(1, COLS, LINES - 1, 0);
// Initialize colors. //
if (has_colors())
{
start_color();
for (int i = 1; i <= COLOR_WHITE; ++i)
{
init_pair(i, i, COLOR_BLACK);
}
}
else
{
cout << "Terminal cannot print colors.n";
if (log.is_open())
log << "Terminal cannot print colors.n";
}
scrollok(stdscr, true);
scrollok(inputLine, true);
leaveok(stdscr, true);
leaveok(inputLine, true);
nodelay(inputLine, true);
cbreak();
noecho();
keypad(inputLine, true);
我做错了什么?
您错误地认为写入标准输出将写入stdscr
。事实上,这样做完全绕过PDCurses并直接写入控制台,就好像你根本没有使用PDCurses一样。您需要使用PDCurses函数来写入PDCurses窗口,包括stdscr
。您需要说服正在使用的任何库,而不是将输出发送到stdout,因为这会混淆PDCurses。
wprintw(stdstc, "nLe Testing.n");
不起作用的明显原因是您使用了stdstc
而不是stdscr
。假设这只是你文章中的一个拼写错误,并且你确实在程序中写了stdscr
,那么这应该是有效的。你还记得打电话给refresh
让屏幕上显示你对stdscr
的更改吗?
相关文章:
- 使用CMake检测支持的C++标准
- 如何理解C++标准N3337中的expr.const.cast子句8
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 编译标准库类型
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 铸造标准::有没有回到原来的类型
- 标准 N3337 5.2.10 第 7 条中的C++"类型"是什么意思?
- this_thread::sleep_for和计时时钟之间的关系是否由C++11标准指定
- 标准库类型的赋值运算符的引用限定符
- 标准是否严格定义了该程序应该如何编译?
- 如何从Windows应用程序输出到标准?
- 安全到标准:移动会员?
- 如何正确将字符串转换为标准::时间::system_clock::time_point?
- 这是否符合C++标准:双响双响,例如!!(-0.0).
- 标准::变体的赋值运算符
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 如何在 Mac 上使用 c++17 并行标准库算法?
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 为什么颜色对标准标准不起作用?(PDCurses)