首页 文章

存储ncurses光标位置作为变量

提问于
浏览
-1

我在一个位置打印文本: (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);

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

提前感谢您的帮助 .

3 回答

  • 0

    您创建了第二个窗口,并在仍然绘制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;
     }
    
  • 0

    你可以使用(legacy featuresgetcurygetcurx

  • 2

    由于您没有提供完整的示例,因此很难找到,出了什么问题 .

    这是一个完整的最小工作示例:

    #include <stdlib.h>
    #include <stdio.h>
    #include <curses.h>
    
    int main(void) {
    
        WINDOW * win = initscr();
    
        mvaddstr(10, 10, "Hello, world!");
        refresh();
    
        int y,x;
        getyx(win, y, x);
    
        // Test output two lines below
        y+=2;
        mvprintw(y, x, "X: %d, Y: %d", x, y);
        refresh();
    
        // press the ANY key ;-)
        getch();
        endwin();
    
        return EXIT_SUCCESS;
    }
    

    输出在Hello,World之下!测试字符串并按预期显示 X: 23 Y: 12 .

相关问题