Source Code File 11-02_overwrite2

11-02_overwrite2.c

#include <ncurses.h>
       
int main()
{   
    WINDOW *red,*blue;
    
    initscr();
    refresh();
        
    /* colors */
    start_color();
    init_pair(1,COLOR_WHITE,COLOR_RED);
    init_pair(2,COLOR_WHITE,COLOR_BLUE);
    
    /* create windows */
    red = newwin(10,20,2,22);
    blue = newwin(10,20,5,32);
    if( red==NULL || blue==NULL)
    {
        endwin();
        puts("Unable to create windows");
        return(1);
    }

    /* color and fill windows */
    wbkgd(red,COLOR_PAIR(1) | 'r');
    wbkgd(blue,COLOR_PAIR(2) | 'b');
    wrefresh(red);
    wrefresh(blue);
    getch();

    /* overwrite windows */
    overwrite(red,blue);
    wbkgd(red,COLOR_PAIR(1) | ' ');
    wrefresh(red);
    wrefresh(blue);
    getch();

    endwin();
    return(0);
}

Output Screenshot


(Click to see result.)

Notes

* The proper result should show a solid red window on top, as shown in the output screen shot above; click the image to see the final screen.

* For my solution, I added two statements. The first is the wbkgd() function at Line 34. This function fills window red with a solid red background. The ' ' (space character) erases the existing window. The second statement is the wrefresh() at Line 35, which updates window red. I then update window blue with the next refresh() statement. That ensures that window red (now a solid color) doesn't appear on top of window blue.