微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ncurses多种颜色在屏幕上

我想用ncurses.h和多种颜色做一个菜单.
我的意思是这样的:
┌────────────────────┐
│░░░░░░░░░░░░░░░░░░░░│ <- color 1
│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│ <- color 2
└────────────────────┘

但是如果我使用init_pair(),attron()和attroff(),整个屏幕的颜色是一样的,而不是像我所料.

initscr();

init_pair(0,COLOR_BLACK,COLOR_RED);
init_pair(1,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_RED);
    init_pair(2,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是非常有帮助的.

原文地址:https://www.jb51.cc/c/111521.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐