如何使用curses.h下的移动功能

How to use the move function under curses.h

它不在坐标 y=10, x=20 处打印。

#include <stdio.h>
#include <curses.h>

int main()
{
    initscr();
    refresh();
    WINDOW *win;
    wmove(win, 10, 20);
    refresh();
    printf("hi\n"); 
    return 0;
}

当我这样执行的时候...

./a.out > op_file

这就是op_file

[?1049h[1;24r(B[m[4l[?7h[H[2J-1
hi

谁能解释一下...?

您必须使用 initscr() 函数初始化屏幕,最后使用 endwin() 关闭 window...

如果你move(),你必须使用refresh(),否则光标不会移动。

要将光标移动到 window 上的新位置,请使用函数 int wmove(WINDOW *win, int y, int x)

wmove(赢, y, x); 其中 (x, y) 是 window 中新位置的坐标。如果 window 有 nlines 行和 ncolumns 列,则

  0 <= y < nlines
  0 <= x < ncolumns

刷新。在您执行 wrefresh(win) 之前,实际的光标运动不会显示在屏幕上。

move(y, x) 等同于 wmove(stdscr, y, x).`

move() 和 wmove() 函数将与当前或指定 window 关联的光标移动到相对于 window 原点的 (y, x)。此函数在下一次刷新操作之前不会移动终端的光标。

要将用户定义的windowmy_window中的逻辑光标移动到坐标y=5,x=10,使用:

#include <stdio.h>
#include <curses.h>

    int main(){
        refresh();//First refresh
        WINDOW *my_window;
        int a = wmove(my_window, 5, 10);
        refresh();////Second refresh
        printf("%d\n",a);
        printf("hi\n");
        return 0;
    }

输出

[?1049h[1;24r(B[m[4l[?7h[H[2J-1
hi

显示可打印个字符。如果您查看完整的文本,例如,在文本编辑器中,在 [ 之前会有 ASCII escape 字符和 ( 个字符,因为这是转义序列的一部分。

您的示例未显示 光标移动 (除了您会看到的 home 位置 ^[[H 接近尾声)因为 curses 库没有理由实际移动光标。如果你要求它读取一个字符,例如,使用 getch,它必须停下来并决定光标应该在哪里——以及你的 wmove 会这样做——除了 win 没有被初始化。最简单的做法是使用 stdscr(由 initscr 初始化)。

程序在不执行 endwin 的情况下退出 curses 调用(这使终端处于 原始模式 )。数据确实通过 refresh 调用写入屏幕。用 printf 写入的数据恰好以正确的顺序出现,但这只是巧合,因为它不使用与 ncurses 相同的输出缓冲。

其他两个答案都包含类似的错误。

有效。

#include <stdio.h>
#include <curses.h>

int main()
{
    initscr();
    refresh();

    WINDOW *win;
    win = stdscr;

    wmove(win, 10, 10);

    refresh();
    printf("hi\n");

    return 0;
}

感谢@interjay。