无法使用 mvwprintw() 写入 ncurses window

Cannot write to an ncurses window using mvwprintw()

几天来,我一直在努力找出这段代码出现问题的原因:

#include <ncurses.h>
#include <stdlib.h>
#include<iostream>
using namespace std;

WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);

int main(int argc, char *argv[]) {  
    WINDOW *my_win;
    int startx, starty, width, height;
    int ch;

    initscr();              // Start curses mode        
    // cbreak();            // Line buffering en/disabled, pass on
                            // everything to me         
    keypad(stdscr, TRUE);   // I need that nifty F2

    height = 3;
    width = 10;
    starty = (LINES - height) / 2;  /* Calculating for a center placement */
    startx = (COLS - width) / 2;    /* of the window        */
    printw("Press F2 to exit!\n");
    refresh();
    my_win = create_newwin(height, width, starty, startx);

    // keypad(my_win, TRUE);

    while((ch = getch()) != KEY_F(2))   {   
        switch(ch) {    
            case KEY_LEFT:
                destroy_win(my_win);
                my_win = create_newwin(height, width, starty,--startx);
                break;
            case KEY_RIGHT:
                destroy_win(my_win);
                my_win = create_newwin(height, width, starty,++startx);
                break;
            case KEY_UP:
                destroy_win(my_win);
                my_win = create_newwin(height, width, --starty,startx);
                break;
            case KEY_DOWN:
                destroy_win(my_win);
                my_win = create_newwin(height, width, ++starty,startx);
                break;
            case 49:
                // Kann nicht in das Fenster my_win ausgeben, Fkt.
                // wird ignoriert:
                mvwprintw(my_win, 1, 1, "Value: %d, H %d, W %d, STy %d, STx %d", 1, height, width, starty, startx);
                cout << "   cout -> Values: height " <<  height  << ", width "<< width <<  ", starty " << starty <<  ", startx " << startx << endl;
                break;  
        }
    }
        
    endwin();           /* End curses mode */
    return 0;
}

WINDOW *create_newwin(int height, int width, int starty, int startx) {  
    WINDOW *local_win;

    local_win = newwin(height, width, starty, startx);
    box(local_win, 0 , 0);      // 0, 0 gives default characters 
                                // for the vertical and horizontal
                                // lines.
    wrefresh(local_win);        // Show that box.

    return local_win;
}

void destroy_win(WINDOW *local_win) {
    /* box(local_win, ' ', ' '); : This won't produce the desired
     * result of erasing the window. It will leave it's four corners 
     * and so an ugly remnant of window. 
     */
    wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
    /* The parameters taken are 
     * 1. win: the window on which to operate
     * 2. ls: character to be used for the left side of the window 
     * 3. rs: character to be used for the right side of the window 
     * 4. ts: character to be used for the top side of the window 
     * 5. bs: character to be used for the bottom side of the window 
     * 6. tl: character to be used for the top left corner of the window 
     * 7. tr: character to be used for the top right corner of the window 
     * 8. bl: character to be used for the bottom left corner of the window 
     * 9. br: character to be used for the bottom right corner of the window
     */
    wrefresh(local_win);
    delwin(local_win);
}

我从:https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/windows.html.

一切正常,只有一个例外:

在我添加的 case 语句 case 49: 中,我尝试在 window my_win 中打印,但是函数调用被忽略了。它没有效果。相反,按下的键“1”打印在 stdscr 上。 cout 行仅用于调试目的,也打印在 stdscr.

中的当前光标位置

知道我做错了什么吗?或者如何纠正我对代码的错误理解?

先谢谢大家了!

干杯!

示例未正确初始化(参见 manual page):

initscr();              // Start curses mode        
// cbreak();            // Line buffering en/disabled, pass on
                        // everything to me 

如果程序调用了cbreak or raw,那么程序会立即通过getch接收字符。由于两者都没有被调用,它读取行缓冲数据(在您按下 Enter 之前什么都不会发生)。

此外,由于它不调用 noecho,输入被回显。

定义正确的初始化序列后

    initscr();
    cbreak();
    noecho();   
    intrflush(stdscr, FALSE);
    keypad(stdscr, TRUE);   // To receive F2

我发现,我一直没有意识到的最重要的事情是 wrefresh(my_win); 紧跟在 mvwprintw 行之后。因此,这些打印命令也需要刷新相关 window,否则更改已完成但不会出现在屏幕上。