gpt4 book ai didi

c - ncurses 屏幕上的多种颜色

转载 作者:太空狗 更新时间:2023-10-29 16:51:27 27 4
gpt4 key购买 nike

我想用 ncurses.h 和不止一种颜色制作一个菜单。我的意思是这样的:

┌────────────────────┐
│░░░░░░░░░░░░░░░░░░░░│ <- color 1
│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│ <- color 2
└────────────────────┘

但是如果我使用init_pair()attron()attroff(),整个屏幕的颜色是一样的,而且不像我预期的那样。

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();

这段代码有什么问题?

感谢您的每一个回答!

最佳答案

这是一个工作版本:

#include <curses.h>

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();
}

注意事项:

  • 您需要在 initscr() 之后调用 start_color() 才能使用颜色。
  • 您必须使用 COLOR_PAIR 宏将由 init_pair 分配的颜色对传递给 attron 等。
  • 你不能使用颜色对 0。
  • 您只需调用一次 refresh(),并且仅当您希望此时可以看到您的输出时,并且您没有调用输入函数像 getch()

This HOWTO很有帮助。

关于c - ncurses 屏幕上的多种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10487166/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com