c - 将 ncurses 光标位置存储为变量

标签 c ncurses

我正在以下位置打印文本:(10, 10)

  • 等待输入,
  • 清屏,
  • 再做一次。

当我打印文本时,它将光标移动到行尾。如何获取 X、Y 位置并将其存储为变量?

我想这样做,以便我可以在文本周围绘制一个动画框。

我知道有 getyx(window, y, x),但它有一个 void 返回。

我尝试使用它,但它并没有改变 xy 的值,它仍然会在 0, 0 处打印。 我不明白如何使用这个方法来做任何事情。

x = y = 0;
getyx(screen, y, x);
move(y, x);
printw("YX TEST"); // prints at 0,0 (bad)

我想做这样的事情:

yPos = getY(screen);
xPos = getX(screen);

然后我可以在哪里处理该信息中的坐标?

感谢您提前提供帮助。

最佳答案

您创建了第二个窗口并使用了第二个窗口,同时仍在 stdscr 上绘图。 。 我可以重现您的问题是:

WINDOW *w1 = initscr();
WINDOW *w2 = newwin(LINES, COLS, 0, 0);
move(10, 10); // this moves the cursor on w1 or stdscr to (10, 10)
printw("YX TEST"); // this draws on w1, ie. stdscr the default screen
// now the cursor on w1 is on (10, 15) and cursor on w2 is on (0,0)
int x, y;
getxy(w2, x, y); // this gets the position of w2 window, which stayed at (0,0)
getxy(w1, x, y); // this will get you the position in w1 window, ie (10, 15)
refresh(); // this will draw window w1 on the screen, ie. `YX TEST`
wrefresh(w2); // this will draw nothing on the screen, as window w2 is empty

如果你有两个窗口,在一个窗口上绘图并从第二个窗口获取光标位置,你会得到奇怪的结果。
如下:

#include <ncurses.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>

void mybox(WINDOW *w, int sx, int sy, int ex, int ey)
{
        assert(w != NULL);
        assert(sx <= ex);
        assert(sy <= ey);
        // up
        wmove(w, sy, sx);
        wprintw(w, "+");
        for (int i = sx + 1; i < ex; ++i) {
                wprintw(w, "-");
        }
        wprintw(w, "+");
        // left right
        for (int i = sy + 1; i < ey; ++i) {
                wmove(w, i, ex);
                wprintw(w, "|");
                wmove(w, i, sx);
                wprintw(w, "|");
        }
        // down
        wmove(w, ey, sx);
        wprintw(w, "+");
        for (int i = sx + 1; i < ex; ++i) {
                wprintw(w, "-");
        }
        wprintw(w, "+");
}

int main() {
        WINDOW *w = initscr();
        for (int i = 2; i; --i) {
                // print text at a location (10,10)
                move(10, 10);
                printw("YX TEST %d", i);
                refresh();
                // let's draw a box around.
                int x, y;
                getyx(w, y, x);
                mybox(w, 9, 9, x, y + 1); // x = 10, y = 19
                refresh();
                // wait for input
                getch();
                // clear screen
                clear();
                // doing it again
        }
        endwin();
        return 0;
}

YX TEST ? 文本周围绘制一个框:

 +---------+
 |YX TEST 2|
 +---------+

如果你想要一个返回光标位置的函数,只需编写它们...

 int getX(WINDOW *win) {
      int x, y;
      getxy(win, y, x);
      return x;
 }

 int getY(WINDOW *win) { 
      int x, y; 
      getxy(win, y, x); 
      return y;
 }

关于c - 将 ncurses 光标位置存储为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52053135/

相关文章:

python - 在 python 中将 curses 与 raw_input 结合使用

c - 矩阵乘法 - 指针和数组

c - if 和 else if 与 strcmp 序列错误/重叠

c - 为指针及其数据分配的内存在哪里?

rust - delwin 不删除窗口

c++ - 如何在 OSX 上为 ncurses 提供鼠标支持?

terminal - 崩溃后清理终端中的 ncurses 困惑

c++ - 为什么这个文本没有被 ncurses 着色?

c - 如何比较两个文件中的字符串?

c - mktime 和 timelocal 的区别