微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法使用 mvwprintw() 写入 ncurses 窗口

如何解决无法使用 mvwprintw() 写入 ncurses 窗口

这几天我一直在试图找出这段代码出现问题的原因:

#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,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,--startx);
                break;
            case KEY_RIGHT:
                destroy_win(my_win);
                my_win = create_newwin(height,++startx);
                break;
            case KEY_UP:
                destroy_win(my_win);
                my_win = create_newwin(height,--starty,startx);
                break;
            case KEY_DOWN:
                destroy_win(my_win);
                my_win = create_newwin(height,++starty,startx);
                break;
            case 49:
                // Kann nicht in das Fenster my_win ausgeben,Fkt.
                // wird ignoriert:
                mvwprintw(my_win,1,"Value: %d,H %d,W %d,STy %d,STx %d",height,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 startx) {  
    WINDOW *local_win;

    local_win = newwin(height,startx);
    Box(local_win,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/

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

在我添加的 case 语句 case 49: 中,我尝试在窗口 my_win 中打印,但函数调用被忽略。它没有效果。相反,按下的键“1”会打印在 stdscr 上。 cout 行仅用于调试目的,也打印在 stdscr 中的当前光标位置。

知道我在这里做错了什么吗?或者如何纠正我可能对代码错误理解?

先谢谢大家!

干杯!

解决方法

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

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

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

另外,由于它没有调用 noecho,所以输入被回显。

,

定义正确的初始化序列之后

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。