为什么这些文字没有被ncurses着色?

use*_*072 7 c++ colors ncurses

我想在ncurses中创建一个窗口,用一个框包围它,并在其中写入一些彩色文本.

当我尝试在标准窗口中制作简单的彩色文本时,它可以很好地工作,但是当我尝试将它放在一个新窗口时,文本在黑色上显示为白色(即默认值)

这是我尝试过的代码.为什么它不起作用?

#include <ncurses.h>

int main(int argc, char *argv[])
{
    initscreen();
    WINDOW * win = newwin(8,15,1,1);
    box(win,0,0);
    start_color();
    init_pair(1, COLOR_BLACK, COLOR_RED);
    attron(COLOR_PAIR(1));
    mvwprintw(win,1,1,"colored text");
    wrefresh(win);
    getch();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

use*_*072 15

我通过使用解决了这个问题

wattron(win, COLOR_PAIR(1));
Run Code Online (Sandbox Code Playgroud)

代替

attron(COLOR_PAIR(1));
Run Code Online (Sandbox Code Playgroud)

wattron影响给定的窗口,虽然attron假设你的意思stdscr,而不是当前的窗口.

  • 嘿,我在你的回答中添加了一系列解释,说明为什么这个改变解决了这个问题.如果你不想要它,请随意删除它. (2认同)