如何使用 derwin() 子窗口函数在 Ncurses 中填充完整的背景颜色?

问题描述 投票:0回答:1

有没有办法使用 Ncurses 中的

derwin()
子窗口函数填充完整的背景颜色?

这是我的程序如下。我期待桑尼子窗口中的所有白色背景,但不幸的是不是我期望出现的。

#include <ncurses.h>

int main(void)
{
    WINDOW *pops,*sonny;
    int x2, y2, x4, y4;

    initscr();
    start_color();
    init_pair(1, COLOR_WHITE, COLOR_BLUE);
    init_pair(2, COLOR_BLACK, COLOR_WHITE);

    x2 = COLS >> 1;
    y2 = LINES >> 1;
    x4 = (COLS - x2) / 2;
    y4 = (LINES - y2) / 2;

    /* create parent and subwindow with derwin() */
    pops = newwin(y2, x2, y4, x4);
    sonny = derwin(pops, y4, x4, 7, 5);
    if (sonny == NULL) {
        endwin();
        puts("Unable to create subwindow\n");
        return(1);
    }

    /* color windows and splash some text */
    wbkgd(pops,COLOR_PAIR(1));
    mvwaddstr(pops, 2, 2, "Hello, son.");
    wrefresh(pops);
    wbkgd(sonny,COLOR_PAIR(2));
    wborder(sonny, 0, 0, 0, 0, 0, 0, 0, 0);
    mvwaddstr(sonny, 3, 3, "Hello, Dad.");
    wrefresh(sonny);
    wgetch(sonny);

    endwin();
    return 0;
}

ncurses curses
1个回答
0
投票

derwin
(如
subwin
)的结果与父窗口共享内存:

子窗口与其祖先窗口 orig 共享内存,因此对一个窗口所做的更改将影响两个窗口。

在阅读

wbkgd
的手册页时请记住这一点:

如果单元格使用颜色,并且与单元格中的颜色不匹配 当前背景,库仅更新非颜色 属性,首先删除那些可能来自 当前背景,然后添加新背景的属性 背景。

派生窗口的背景字符与单元格的颜色不匹配,因为它是由父窗口上的第一个

wbkgd
调用设置的:

    wbkgd(pops,COLOR_PAIR(1));

您可以使用 wbkgdset,

 使派生窗口的背景
字符

与其相同(不修改单元格)
    wbkgdset(sonny,COLOR_PAIR(1));

这样做之后,背景字符的颜色与派生窗口的内容相匹配,并且本段适用:

如果单元格使用颜色,并且与当前的颜色相匹配 背景,库删除了可能来自的属性 当前背景并添加新背景的属性。 它通过将单元格设置为使用新的颜色来完成 背景。

© www.soinside.com 2019 - 2024. All rights reserved.