加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ncurses多种颜色在屏幕上

发布时间:2020-12-16 03:25:02 所属栏目:百科 来源:网络整理
导读:我想用ncurses.h和多种颜色做一个菜单. 我的意思是这样的: ┌────────────────────┐│????????????????????│ - color 1│????????????????????│ - color 2└────────────────────┘ 但是如果我使用init_pair(),
我想用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是非常有帮助的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读