【问题标题】:Ncurses mvwprintw doesnt printNcurses mvwprintw 不打印
【发布时间】:2020-07-22 03:36:36
【问题描述】:

我有一个简单的程序,它有一个主窗口和一个底部的小窗口(没有线条,这只是为了让您可以看到两个窗口:

+------------------+
|                  |
|                  | 
|                  |
+------------------+
|                  |
+------------------+

我希望底部区域是你可以输入的地方,这是我的源代码:

#include <termios.h>
#include <bits/stdc++.h>
#include <ncurses.h>
int main()
{
    int scrx;
    int scry;

    initscr();
    cbreak();
    noecho();
    clear();
    raw();

    getmaxyx(stdscr, scrx, scry);
    WINDOW* input = newwin(1, scrx, scry, 0);

    std::string cmdbuf;

    while(true)
    {
        int newx;
        int newy;
        getmaxyx(stdscr, newx, newy);

        if(newx != scrx || newy != scry)
        {
            // do stuff;
        }

        char c = wgetch(input);
        cmdbuf.push_back(c);
        werase(input);

        mvwprintw(input, 0, 0, cmdbuf.c_str());
        refresh();
        wrefresh(input);
    }
}

但是,它似乎没有打印任何内容,只需移动我的光标(它会在屏幕中途被吸走)。如何才能使文本真正被打印并且我的光标实际上在全屏上移动?

【问题讨论】:

  • 你是在 while 循环中擦除而不是在它之前。你实际上并没有使用 newx & newy。看看以下内容:www6.uniovi.es/cscene/CS3/CS3-08.html
  • @Geoff 我已经尝试评论 werase 行,它似乎没有帮助
  • 并且没有使用newx & newy...
  • @Geoff 是的,它们没有被使用,但这仅适用于调整窗口大小时(在我的测试中从未发生过)

标签: c++ ncurses


【解决方案1】:

为你整理了一下。按“q”退出。你明白了。

#include <termios.h>                                                                                                                                                                         
#include <bits/stdc++.h>
#include <ncurses.h>

int main()
{
  int scrx, scry;
  initscr();
  getmaxyx(stdscr, scry, scrx);
  WINDOW *w = newwin(1, scrx, scry - 1, 0);
  std::string cmdbuf {};
  char c = '\0';

  while (c != 'q')
  {
    int newx, newy;
    getmaxyx(stdscr, newx, newy);

    if(newx != scrx || newy != scry)
    {
      // do stuff;
    }

    c = wgetch(w);
    cmdbuf += c;
    mvwprintw(w, 0, 0, "%s", cmdbuf.c_str());
    wrefresh(w);
  }

  delwin(w);
  endwin();
}

【讨论】:

    【解决方案2】:

    refresh 正在覆盖 mvwprintw,因为它们是不同的窗口。对于给定的示例,没有理由刷新 stdscr,因为没有任何东西(initscr 调用除外)更新了该窗口。将refresh 移出循环会有所帮助(但“做事”显然会干扰这一点)。

    newx/newy 逻辑太零碎,无法评论(我会使用getbegyx ...)。

    【讨论】:

    • newx/newy 逻辑用于调整屏幕大小
    【解决方案3】:

    newwin的声明是:

    WINDOW *newwin(
             int nlines, int ncols,
             int begin_y, int begin_x);
    

    你在打电话:

    newwin(1,scry,scrx,0)
    

    它将窗口的大小设置为1 高和scry 宽,并将其放在坐标(0,srcx)。你想要的是:

    newwin(1,scry,scrx-1,0)
    

    1 是窗口的高度。

    另外,cbreak 会覆盖 raw,因此调用两者没有意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2016-08-07
      • 2018-07-06
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 2019-07-06
      相关资源
      最近更新 更多