我试过寻找解决方案,我只是不知道为什么窗口没有显示。代码相当简单明了。为什么会这样?我之前问过一个类似的问题,但知道一个似乎能够提供正确答案的人,所以我让它有点简单,只包括重要的东西。
#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;
}
Run Code Online (Sandbox Code Playgroud)
你忘了调用刷新。
基本上,您确实在新创建的窗口上调用了 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;
}
Run Code Online (Sandbox Code Playgroud)
正如你所期望的那样提供窗户。
问题是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;
}
Run Code Online (Sandbox Code Playgroud)
进一步阅读:
wgetch