ncurses multi colors on screen

后端 未结 2 537
予麋鹿
予麋鹿 2020-12-11 02:13

I want to make a menu with ncurses.h and more than one color. I mean something like this:

┌────────────────────┐
│░░░░░░░░░░░░░░░░░░░░│ <- co         


        
2条回答
  •  悲哀的现实
    2020-12-11 02:41

    Here's a working version:

    #include 
    
    int main(void) {
        initscr();
        start_color();
    
        init_pair(1, COLOR_BLACK, COLOR_RED);
        init_pair(2, COLOR_BLACK, COLOR_GREEN);
    
        attron(COLOR_PAIR(1));
        printw("This should be printed in black with a red background!\n");
    
        attron(COLOR_PAIR(2));
        printw("And this in a green background!\n");
        refresh();
    
        getch();
    
        endwin();
    }
    

    Notes:

    • you need to call start_color() after initscr() to use color.
    • you have to use the COLOR_PAIR macro to pass a color pair allocated with init_pair to attron et al.
    • you can't use color pair 0.
    • you only have to call refresh() once, and only if you want your output to be seen at that point, and you're not calling an input function like getch().

    This HOWTO is very helpful.

提交回复
热议问题