C- Ncurses,窗口未显示/打印

5

我曾经尝试寻找解决方案,但我不知道为什么窗口没有显示。这段代码非常简单明了,为什么会出现这种情况呢?我之前问过类似的问题,但没有人能够提供正确的答案,所以我将它简化了一些,只包含了重要的内容。

#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>


int main()
{
    initscr();
    WINDOW* win;
    int height = 10;
    int width = 40;
    int srtheight = 1;
    int srtwidth = 0;
    win = newwin(height, width, srtheight ,srtwidth);
    mvwprintw(win, height/2,width/2,"First line");
    wrefresh(win);
    getch();
    delwin(win);
    endwin();


    return 0;
}
3个回答

9

您忘记调用刷新。

基本上,您在新创建的窗口上调用了刷新,但是您忘记刷新父窗口,因此它从未重新绘制。

#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    WINDOW* my_win;
    int height = 10;
    int width = 40;
    int srtheight = 1;
    int srtwidth = 1;
    initscr();
    printw("first"); // added for relative positioning
    refresh();  //  need to draw the root window
                //  without this, apparently the children never draw
    my_win = newwin(height, width, 5, 5);
    box(my_win, 0, 0);  // added for easy viewing
    mvwprintw(my_win, height/2,width/2,"First line");
    wrefresh(my_win);
    getch();
    delwin(my_win);
    endwin();
    return 0;
}   

提供您所期望的窗口。


5
问题在于getch会刷新标准窗口stdscr,覆盖了在前一行为win所做的刷新。如果你调用wgetch(win)而不是这两行代码,它就能正常工作。
像这样:
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>


int main()
{
    initscr();
    WINDOW* win;
    int height = 10;
    int width = 40;
    int srtheight = 1;
    int srtwidth = 0;
    win = newwin(height, width, srtheight ,srtwidth);
    mvwprintw(win, height/2,width/2,"First line");
    /* wrefresh(win); */
    wgetch(win);
    delwin(win);
    endwin();


    return 0;
}

更多阅读:


1
你需要在newwin()之后调用refresh()
win = newwin(height, width, srtheight ,srtwidth);
refresh(); // <<<
mvwprintw(win, height/2,width/2,"First line");

我认为重要的是在 initscr() 之后调用 refresh(),而不一定是在 newwin() 之后... - Crowman
好的,你比我快了一点;但是,似乎如果你至少刷新()一次根窗口,那么子窗口的wrefresh(...)就可以工作,而无需进一步刷新()父窗口。 - Edwin Buck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接