英文:
ncurses: alternative to mvwprintw
问题
我正在寻找替代mvwprintw函数的方法。我的问题是,这个函数会移动光标并将文本放在屏幕上。是否有一种替代方法可以做同样的事情,但不会影响光标位置?
谢谢
英文:
I'm looking for an alternative to the mvwprintw function. My problem is that this function moves the cursor and the puts the text on the screen. Is there an alternative that does the same except it doesn't mess with the cursor position?
Thanks
答案1
得分: 1
你可以继续使用mvprintw,只需将光标移回原来的位置。
/* 记录首次光标位置 */
int x, y;
getyx(win, y, x);
/* 你的原始代码 */
mvwprintw(win, other_y, other_x, "hello");
/* 移回原始位置 */
wmove(win, y, x);
只需注意,getyx 是一个宏,所以你不需要发送地址,但x和y将会被赋值。
英文:
You can just use mvprintw as you already are, and just move the cursor back.
/* first record cursor position */
int x,y;
getyx(win, y, x);
/* your original code */
mvwprintw(win, other_y, other_x, "hello");
/* move back to original location */
wmove(win, y, x);
Just note that getyx is a macro so you are not sending an address, but x and y will get assigned.
答案2
得分: 1
正如另一个答案中所提到的,您可以始终使用 getyx(或 getcurx,getcury)和 wmove 来保存和恢复光标位置。
mvwprintw 的作用就像调用了 wmove 并(成功时)wprintw,它也移动了光标:
> printw、wprintw、mvprintw 和 mvwprintw 程序类似于 printf [参见 printf(3)]。实际上,通过 printf 输出的字符串被输出,就好像在给定窗口上使用了 waddstr。
因为 waddstr 的作用就像调用了 waddch。还有一组与 waddstr 相对应的函数,它们不移动光标,即 waddchstr,但对于这种情况,没有不移动的 wprintw 和 waddch 对应项。因此,在 curses 库中,保存/恢复光标是执行所需操作的唯一方式。
英文:
As noted in another answer, you can always save and restore the cursor position using getyx (or getcurx, getcury) and wmove.
mvwprintw acts as if it calls wmove and (on success) wprintw, which also moves the cursor:
>The printw, wprintw, mvprintw and mvwprintw routines are analogous to
printf [see printf(3)]. In effect, the string that would be output by
printf is output instead as though waddstr were used on the given window.
because waddstr acts as if it calls waddch. There is another set of functions corresponding to waddstr which do not move the cursor, i.e., waddchstr, but there is no counterpart/non-advancing wprintw and waddch for that case. So saving/restoring the cursor is the only way in the curses library for doing what is asked.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论