首页 > 解决方案 > Ncurses mvwprintw 不打印

问题描述

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

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

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

#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);
    }
}

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

标签: c++ncurses

解决方案


给你整理了一下。按“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();
}

推荐阅读