我的ncurses窗口没有出现在我的终端上。

huangapple go评论61阅读模式
英文:

my ncurses window isn't appearing on my terminal

问题

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

int main() {
    initscr();
    int h, w, y, x;
    h = 10;
    w = 25;
    y = 15;
    x = 20;
    WINDOW *win = newwin(h, w, y, x);
    box(win, 0, 0);
    wrefresh(win);
    printw("Hello");
    wprintw(win, "hi");
    wrefresh(win);
    getch();
    refresh();
    endwin();

    return 0;
}
英文:

I'm new to Ncurses and I tried to make a program in C++ that makes a window then displays a box and text on both the box and standard screen.

Here is my code

#include &lt;iostream&gt;
#include &lt;ncurses.h&gt;
using namespace std;

int main(){
	initscr();
	int h,w,y,x;
	h = 10;
	w = 25;
	y = 15;
	x = 20;
	WINDOW * win = newwin(h ,w, y, x);
	box(win,0,0);
	wrefresh(win);
	printw(&quot;Hello&quot;);
	wprintw(win,&quot;hi&quot;);
	wrefresh(win);
	getch();
	refresh();
	endwin();
	
	return 0;
	}

Any help is greatly appreciated.

答案1

得分: 0

以下是翻译好的部分:

可能问题出在窗口的位置(y 和 x 坐标)。设置 y=0x=0 来将窗口定位在屏幕的左上角,然后查看是否显示出来。

你还可以通过在 main 函数的开头添加以下代码来检查 ncurses 是否正确初始化,以确保 ncurses 已成功初始化:

if (!initscr()) {
    endwin();
    return 1;
}

更新后的代码如下:

#include <iostream>
#include <ncurses.h>

int main() {
  if (!initscr()) {
    endwin();
    return 1;
  }

  int h = 10, w = 25, y = 0, x = 0;
  WINDOW *win = newwin(h, w, y, x);

  box(win, 0, 0);
  wrefresh(win);
  printw("Hello");
  wprintw(win, "hi");
  wrefresh(win);
  getch();
  refresh();
  endwin();

  return 0;
}

希望对你有所帮助。

英文:

It's possible that the problem is with the window's position (y and x coordinates). Set y=0 and x=0 to position the window in the upper-left corner of the screen and see if it appears.

You can also check if ncurses is properly initialised by adding if (!initscr()) endwin(); return 1;} at the beginning of the main function to ensure that ncurses has been successfully initialised.

The updated code is as follows:

#include &lt;iostream&gt;
#include &lt;ncurses.h&gt;

int main() {
  if (!initscr()) {
    endwin();
    return 1;
  }

  int h = 10, w = 25, y = 0, x = 0;
  WINDOW *win = newwin(h, w, y, x);

  box(win, 0, 0);
  wrefresh(win);
  printw(&quot;Hello&quot;);
  wprintw(win, &quot;hi&quot;);
  wrefresh(win);
  getch();
  refresh();
  endwin();

  return 0;
}

huangapple
  • 本文由 发表于 2023年2月6日 07:38:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定