I want to make a menu with ncurses.h
and more than one color.
I mean something like this:
┌────────────────────┐
│░░░░░░░░░░░░░░░░░░░░│ <- color 1
│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│ <- color 2
└────────────────────┘
But if I use init_pair()
, attron()
and attroff()
the color of the whole screen is the same, and not like I've expected.
initscr();
init_pair(0, COLOR_BLACK, COLOR_RED);
init_pair(1, COLOR_BLACK, COLOR_GREEN);
attron(0);
printw("This should be printed in black with a red background!\n");
refresh();
attron(1);
printw("And this in a green background!\n");
refresh()
sleep(2);
endwin();
What's wrong with this code?
Thanks for every answer!
Here's a working version:
Notes:
start_color()
afterinitscr()
to use color.COLOR_PAIR
macro to pass a color pair allocated withinit_pair
toattron
et al.refresh()
once, and only if you want your output to be seen at that point, and you're not calling an input function likegetch()
.This HOWTO is very helpful.
You need to initialize colors and use the COLOR_PAIR macro.
Color pair
0
is reserved for default colors so you have to start your indexing at1
.