C语言实现光标移动

C语言实现光标移动

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

HANDLE hout;

char inputcontext()
{
int ch;
COORD c;
CONSOLE_SCREEN_BUFFER_INFO csbi;
c.X=10;
c.Y=10;
ch=_getch();
//0x0d是回车的键码,0xe0是上下左右的键码
while(ch0xe0||ch0x0d)
{
GetConsoleScreenBufferInfo(hout,&csbi);
c.X=csbi.dwCursorPosition.X;
c.Y=csbi.dwCursorPosition.Y;
if(ch0x0d)
{
c.X=0;
c.Y++;
}
else if(ch
0xe0)
{
ch=_getch();
if (ch == 0x48)//上
{
if (c.Y != 0)
c.Y–;
}
else if (ch == 0x50)//下
{
c.Y++;
}
else if (ch == 0x4b)//左
{
if (c.X != 0)
c.X–;
}
else if(ch == 0x4d)//右
{
if (c.X != 79)//向右写79个字换行
c.X++;
else
{
c.X = 0;
c.Y++;
}
}
}
SetConsoleCursorPosition(hout,c);
ch=_getch();
}
return ch;
}

int main()
{
char ch;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
while(1)
{
ch=inputcontext();
putch(ch);
}
}
//注意按方向键输入了两个char第一个char是判断方向键的,第二个键是判断具体按下了哪个方向键,所以对方向键的判断要接收两次字符。

猜你喜欢

转载自blog.csdn.net/weixin_40677431/article/details/86414795