Search code examples
ncurses

Displaying color in NCurses window


In the code below, why don't I see color text/background?

    #include <ncurses.h>
    
    int main()
    {
            initscr();
    
            int lines = 20;
            int cols = 30;
            int y = 1;
            int x = 1;
    
            WINDOW* win = newwin(lines, cols, y, x);
    
            start_color();
            init_pair(1, COLOR_WHITE, COLOR_RED);
    
            attrset(COLOR_PAIR(1));
            mvwprintw(win, 2, 2, "Some words...\n\n");
            /* mvprintw(2, 2, "Some words...\n\n");// this works! (with refresh())*/
            attroff(COLOR_PAIR(1));
    
            wrefresh(win);
    
            return 0;
    }

I'd like to know how to display color text/background inside ncurses window.


Solution

  • There are more issues:

    1. you don't correctly end ncurses

    2. you set colours for default window (for window named screen), but you used print to own window.

    #include <ncurses.h>
    
    int main()
    {
        int lines = 20;
        int cols = 30;
        int y = 1;
        int x = 1;
        WINDOW* win;
    
        initscr();
        start_color();
    
        win = newwin(lines, cols, y, x);
        init_pair(1, COLOR_WHITE, COLOR_RED);
    
        wattron(win, COLOR_PAIR(1));
        mvwprintw(win, 2, 2, "Some words...\n\n");
        wattroff(win, COLOR_PAIR(1));
        wrefresh(win);
    
        /* wait on press any key */
        getchar();
    
        /* close ncurses */
        endwin();
    
        return 0;
    }