如何在C ++控制台应用程序中控制光标位置? [英] How to control a cursor position in c++ console application?

查看:42
本文介绍了如何在C ++控制台应用程序中控制光标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该为学校项目创建一个控制台应用程序,它是关于Sudoku Game的,所以事情是我在算法上没有发现任何困难,但是我想知道是否可以用c ++绘制完整的Sodoku表并将空的正方形用作数据"输入位置,以便用户可以使用箭头键将光标移动到特定数字的位置,以将适当的数字填充到该位置.有没有办法做到这一点?

I'm supposed to create a console application for school project and it's about Sudoku Game, so the thing is i don't find any struggle with the algorithm, but i was wondering if i could draw the full Sodoku table with c++ and make empty squares as "data" input place so the user can move the cursor using the arrow keys to the specific number's place to fill it with the appropriate number. is there a method to do it this way ?

推荐答案

这取决于您的OS/Compiler.例如,在VC ++中,您可以使用,并且可以找到示例此处.

It depends on your OS/Compiler. For example, in VC++ you can use this and example can be found here.

#include <windows.h>
int main(){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {3, 6};
SetConsoleCursorPosition(hConsole, pos);
WriteConsole(hConsole, "Hello", 5, NULL, NULL);
return 0;
}

如果要使用g ++编译器在Linux中进行操作,则可以使用特殊的库(例如curses)或编写自己的实现(这会有些困难).例如,只需将光标放置在所需位置,即可使用以下方法:

If you want to do it in Linux with g++ compiler, you can use special libraries such as curses or write your own implementation(will be a bit difficult). Such for just placing the cursor at the required position, you can use this:

void gotoxy(int x,int y)    
{
    printf("%c[%d;%df",0x1B,y,x);
}
void clrscr(void)
{
    system("clear");
}
int main() {    
    int x=10, y=20;
    clrscr();
    gotoxy(x,y);
    printf("Hello World!");
}

这篇关于如何在C ++控制台应用程序中控制光标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆