【问题标题】:Ncurses Window not Displaying AnythingNcurses 窗口不显示任何内容
【发布时间】:2012-11-19 16:21:27
【问题描述】:

我正在使用 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吗?

标签: c++ linux ncurses


【解决方案1】:

编辑:问题的原因是getch() 行。通过调用 getch() 您的程序将 stdscr 重新置于顶部。一种解决方案是使用wgetch(),如 (https://stackoverflow.com/a/3808913/2372604) 中所述。

我找到的另一个解决方案如下,不幸的是,这可能无法正常工作,具体取决于您对 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;
}

【讨论】:

    【解决方案2】:

    正如@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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2022-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多