Ncurses窗口没有显示任何内容

5
我正在使用ncurses库编写控制台应用程序。我有一个函数,它应该写入窗口缓冲区,然后刷新窗口。在我的测试函数中,我调用了窗口函数,确实发生了一些事情,因为程序移动到了下一行(getch()只是等待我输入字符),但是没有东西显示出来。
以下是我的函数:
void UI::drawAudience(int audience1, int audience2)
{

    string bar1 = "", bar2 = "";

    for (int i; i < BAR_SIZE; i++)
    {

        bar1 += (i <= audience1) ? ' ' : '+';

        if (i <= audience2)
            bar2 += '+';
    }

    string audienceName = "Audience Name";

    //mvwprintw(audience, 0, 11 - audienceName.size() / 2, "%s", audienceName.c_str());
    //mvwprintw(audience, 1, 0, "%s|%s", bar1.c_str(), bar2.c_str());
    wprintw(audience, "Test");

    wrefresh(audience);
}

这是我的测试代码:

#include "Tests.h"
#include <string>

using namespace std;

void test()
{
    int y = 1;

    UI testUI;

    initscr();
    cbreak();

    WINDOW* windowTest = newwin(1, 23, 3, 0);

    wprintw(windowTest, "This is a new window");
    wrefresh(windowTest);

    getch();

    clear();

    delwin(windowTest);

    testUI.drawAudience(4,5);

    getch();

    endwin();

}

你能否只发布class UI的代码? - Sakthi Kumar
2个回答

17

编辑:你问题的原因是getch()这一行。通过调用getch()函数,您的程序会将stdscr置于顶层。解决方法是使用如https://dev59.com/1W865IYBdhLWcg3wceNU#3808913所述的wgetch()函数。

我找到的另一个解决方案如下,不幸的是,这可能取决于UI类的实现。请尝试使用两个refresh()行都被注释掉的以下代码,然后再试着取消其中一个或两个行的注释。如果在刷新窗口之前没有刷新屏幕,则永远无法看到窗口。

#include <ncurses.h>

int main(int argc, char** argv)
{
    initscr();
    cbreak();
    refresh();      // Important to refresh screen before refresh window

    WINDOW* windowTest = newwin(1, 23, 3, 0);
    wprintw(windowTest, "Hello World");

    //refresh();      // Refresh here works as well
    wrefresh(windowTest);

    getch();

    delwin(windowTest);
    endwin();

    return 0;
}

0
如@ilent2所提到的,在创建每个窗口后,需要更新stdscr以包括您的新窗口。但是,可以使用wnoutrefresh(stdscr)来实现相同的效果,它将命名窗口复制到虚拟屏幕中,并且不调用doupdate()执行实际输出,您可以在完成窗口后自己调用doupdate()具有更少的CPU时间的好处

但是,甚至更好的解决方案:使用面板

#include <curses.h>
#include <panel.h>

int main()
{
    initscr();

    // use default width and height
    WINDOW* p = newwin(0, 0, LINES/2, COLS/2);
    // create a panel of a newly created window 
    // managing windows is easy now
    PANEL* p_panel = new_panel(p);
    waddstr(p, "that's what i'm talking about!");

    update_panels(); // update windows once and for all

    doupdate(); // the actual updating (calculate changes with the curscr)

    endwin();
}

如你所见,面板可以帮助很多。单独管理每个窗口是乏味且容易出错的。因此,显然你可以做几个窗口:

WINDOW* win = newwin(height, width, beginy, beginx); // variables defined somewhere
WINDOW* win2 = newwin(height, width, beginy+5, beginx+10);

PANEL* p1 = new_panel(win);
PANEL* P2 = new_panel(win2);

// changing my windows without worrying about updating in the correct order (because they're overlapping)

update_panels();

doupdate();

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