- 利用结构指针实现对光标位置的实时修改以及返回方便再次调用时光标还在原位
- 具体使用方法最后有写的......
-
/** 移动光标并位置
-
* coord:光标初始坐标
-
* span:光标横向移动跨度
-
* column:光标最多移动列数
-
* high:光标可移动最高行数
-
* low:光标可移动最低行数
-
*/
-
HANDLE winHandle; //句柄
-
COORD * movePosition(COORD coord, int span, int column, int high, int low);
-
COORD * movePosition(COORD coord, int span, int column, int high, int low){
-
winHandle = GetStdHandle(STD_OUTPUT_HANDLE);//拿到控制台句柄
-
COORD * pos = &coord; //屏幕上的坐标
-
SetPosition(pos->X, pos->Y); //设置初始光标位置
-
// CONSOLE_SCREEN_BUFFER_INFO csbi; //控制台屏幕缓冲区信息
-
int ch; //输入字符串
-
ch = getch();
-
if(ch == 0xE0 || ch == 0x0d) //0x0d表示回车,0XE0表示上下左右等键的键码
-
{
-
//GetConsoleScreenBufferInfo(winHandle, &csbi);
-
//pos->X = csbi.dwCursorPosition.X; //得到坐标X的值
-
//pos->Y = csbi.dwCursorPosition.Y; //得到坐标Y的值
-
-
if(ch == '\n') //如果是回车
-
{
-
return NULL;
-
}
-
ch = getch();
-
if(ch == 72) //上
-
{
-
if(pos->Y > high) {
-
pos->Y--;
-
}else{
-
pos->Y = low;
-
}
-
SetPosition(pos->X, pos->Y);
-
}else if(ch == 80) //下
-
{
-
if(pos->Y < low) {
-
pos->Y++;
-
}else{
-
pos->Y = high;
-
}
-
SetPosition(pos->X, pos->Y);
-
}
-
-
else if(ch == 75) //左
-
{
-
if(pos->X > MARGIN_X + 3)
-
pos->X -= span;
-
else {
-
pos->X = MARGIN_X + 3 + span * (column - 1);
-
if(pos->Y > high) {
-
pos->Y--;
-
}
-
}
-
SetPosition(pos->X, pos->Y);
-
}
-
-
else if(ch == 77) //右
-
{
-
if(pos->X < MARGIN_X + 3 + span * (column - 1))
-
pos->X += span;
-
else
-
{
-
pos->X = MARGIN_X + 3;
-
if(pos->Y < low)
-
pos->Y++;
-
}
-
SetPosition(pos->X, pos->Y);
-
}
-
SetConsoleCursorPosition(winHandle, *pos);
-
}
-
return pos;
-
}
-
/* 设置光标的位置 */
-
void SetPosition(int x, int y);
-
void SetPosition(int x, int y)
-
{
-
COORD pos = {x, y};
-
winHandle = GetStdHandle(STD_OUTPUT_HANDLE); //拿到控制台句柄
-
// 设置光标的坐标
-
SetConsoleCursorPosition(winHandle, pos);
-
}
-
-
-
int main()
-
{
-
COORD c = {13, 3};
-
while(1){
-
c = *movePosition(c,9,8,3,10);
-
SetPosition(10,12);
-
printf("x : %d, y: %d", c.X, c.Y);
-
SetPosition(c.X, c.Y);
-
}
-
}
灵活的控制光标在控制台移动
猜你喜欢
转载自www.cnblogs.com/11-shou/p/12380005.html
今日推荐
周排行