我试过寻找解决方案,我只是不知道为什么窗口没有显示。代码相当简单直接。为什么会这样?我之前问了一个类似的问题,但知道一个人似乎能够提供正确的答案,所以我做了一点简单,只包括了重要的东西。
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
initscr();
WINDOW* win;
int height = 10;
int width = 40;
int srtheight = 1;
int srtwidth = 0;
win = newwin(height, width, srtheight ,srtwidth);
mvwprintw(win, height/2,width/2,"First line");
wrefresh(win);
getch();
delwin(win);
endwin();
return 0;
}
答案 0 :(得分:2)
你忘了打电话刷新。
基本上,你确实在新创建的窗口上调用了refresh,但是你忘了刷新父窗口,所以它永远不会刷新。
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
WINDOW* my_win;
int height = 10;
int width = 40;
int srtheight = 1;
int srtwidth = 1;
initscr();
printw("first"); // added for relative positioning
refresh(); // need to draw the root window
// without this, apparently the children never draw
my_win = newwin(height, width, 5, 5);
box(my_win, 0, 0); // added for easy viewing
mvwprintw(my_win, height/2,width/2,"First line");
wrefresh(my_win);
getch();
delwin(my_win);
endwin();
return 0;
}
按预期给出了窗口。
答案 1 :(得分:1)
您需要在refresh()
后调用newwin()
:
win = newwin(height, width, srtheight ,srtwidth);
refresh(); // <<<
mvwprintw(win, height/2,width/2,"First line");
答案 2 :(得分:1)
问题是getch
刷新标准窗口stdscr
,覆盖了对前一行win
的刷新。如果你打电话给wgetch(win)
而不是那两行,那就行了。
像这样:
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
initscr();
WINDOW* win;
int height = 10;
int width = 40;
int srtheight = 1;
int srtwidth = 0;
win = newwin(height, width, srtheight ,srtwidth);
mvwprintw(win, height/2,width/2,"First line");
/* wrefresh(win); */
wgetch(win);
delwin(win);
endwin();
return 0;
}
进一步阅读:
wgetch