C++ 将文本光标移动到特定的屏幕坐标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10401724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move text cursor to particular screen coordinate?
提问by user1232138
How can I set the cursor at the desired location on the console in C or C++?
如何将光标设置在 C 或 C++ 控制台上的所需位置?
I remember a function called gotoxy(x,y)
, but I think its deprecated. Is there any alternative?
我记得有一个名为 的函数gotoxy(x,y)
,但我认为它已被弃用。有什么替代方法吗?
采纳答案by Harry Johnston
There are a bunch of other functions in the same part of the MSDN library. Some of them may be useful too.
MSDN 库的同一部分还有许多其他函数。其中一些也可能有用。
回答by John Bode
Neither C nor C++ have any notion of a screen or console; they only see streams of bytes, which have no inherent display characteristics. There are a number of third-party APIs like ncursesto help you do that.
C 和 C++ 都没有屏幕或控制台的概念;他们只看到字节流,没有固有的显示特性。有许多像ncurses这样的第三方 API可以帮助您做到这一点。
If you want a quick-n-dirty solution andthe terminal you're working with understands ANSI escape sequences, then you can do things like
如果您想要一个快速的解决方案并且您正在使用的终端理解ANSI 转义序列,那么您可以执行以下操作
printf("3[%d;%dH", row, col);
to move the cursor to a specific row and column (where the top left corner is {1,1}). You'd be better off using ncurses, though (or the equivalent for your platform).
将光标移动到特定的行和列(其中左上角为 {1,1})。不过,您最好使用 ncurses(或适用于您的平台的等价物)。
回答by T.Mahon
I use a really simple method. You don't overly need to know what a HANDLE is unless you're really diving into console applications, a COORD object is in the windows.h standard library and has two member data intergers X and Y. 0,0 is the top left corner and Y increases to go down the screen. You can use this command and just continue to use std::cout<< to print whatever you need.
我使用了一个非常简单的方法。除非您真的深入研究控制台应用程序,否则您不必过度了解 HANDLE 是什么,一个 COORD 对象位于 windows.h 标准库中,并且有两个成员数据整数 X 和 Y。0,0 是左上角角和 Y 增加到屏幕下方。您可以使用此命令并继续使用 std::cout<< 打印您需要的任何内容。
#include <windows.h>
int main(void){
//initialize objects for cursor manipulation
HANDLE hStdout;
COORD destCoord;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
//position cursor at start of window
destCoord.X = 0;
destCoord.Y = 0;
SetConsoleCursorPosition(hStdout, destCoord);
}
回答by Septagram
回答by 808
This was on stackoverflow...
这是在stackoverflow上...
`#include <stdio.h>
// ESC-H, ESC-J (I remember using this sequence on VTs)
#define clear() printf("3[H3[J")
//ESC-BRACK-column;row (same here, used on terminals on an early intranet)
#define gotoxy(x,y) printf("3[%d;%dH", (y), (x))
int main(void)
{
clear();
gotoxy(23, 12);
printf("x");
gotoxy(1, 24);
return 0;
}`
回答by lupz
I figured out this to set the cursor.
我想出了这个来设置光标。
#include <iostream>
void setPos(std::ostream& _os, const std::streamsize& _x, const std::streamsize& _y)
{
char tmp = _os.fill();
if(_y>0) {
_os.fill('\n');
_os.width(_y);
_os << '\n';
}
if(_x>0) {
_os.fill(' ');
_os.width(_x);
_os << ' ';
}
_os.flush();
_os.fill(tmp);
}
int main(int argc, char **argv)
{
setPos(std::cout, 5, 5);
std::cout << "foo" << std::endl;
return 0;
}
To do more you'll need assumptions on the resolution or a lib like ncurses.
要执行更多操作,您需要对分辨率或ncurses 之类的库进行假设。