如何用C语言编写一个程序,输出一个汉字,用方向键控制它在屏幕上的显示位置?编译器要用Visual C++6.0
1个回答
展开全部
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define MAX_X 78 //汉字占两个位置
#define MAX_Y 23 //avoid
typedef _CONSOLE_CURSOR_INFO CurInfo;
COORD curPos = {0 , 0};
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
CurInfo*initialInfo = NULL, currentInfo;
int hideCursor(int state) //1 hide
{
if(state == 1){
if(initialInfo == NULL)
{
initialInfo = (CurInfo *)malloc(sizeof(CurInfo));
GetConsoleCursorInfo(hCon, initialInfo);
}
currentInfo.dwSize = initialInfo->dwSize;
currentInfo.bVisible = false;
SetConsoleCursorInfo(hCon, ¤tInfo);
}else{
if(initialInfo != NULL){
SetConsoleCursorInfo(hCon, initialInfo);
free(initialInfo);
initialInfo = NULL;
}
}
return 0;
}
int setCurrentPos(int press){
switch(press)
{
case 75: //left
curPos.X = curPos.X == 0 ? 0 : curPos.X - 1;break;
case 72: //up
curPos.Y = curPos.Y == 0 ? 0 : curPos.Y - 1;break;
case 77: //right
curPos.X = curPos.X >= MAX_X ? MAX_X : curPos.X + 1;break;
case 80: //down
curPos.Y = curPos.Y >= MAX_Y ? MAX_Y : curPos.Y + 1;break;
default:
return -1;
}
return 0;
}
int main(){
char *str = "人";
printf(str);
hideCursor(1);
while(1){
int press = getch();
if(press == 224) {//方向键第一个值
SetConsoleCursorPosition(hCon, curPos); //FOR CLEAR UP
if(setCurrentPos(getch()) == 0)
{
printf(" "); // clear up, or use system("cls");
SetConsoleCursorPosition(hCon, curPos);printf(str);
}
}else if(press == 27){ //if press esc, exit
hideCursor(0);
break;
}
}
return 0;
}
#include <conio.h>
#include <windows.h>
#define MAX_X 78 //汉字占两个位置
#define MAX_Y 23 //avoid
typedef _CONSOLE_CURSOR_INFO CurInfo;
COORD curPos = {0 , 0};
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
CurInfo*initialInfo = NULL, currentInfo;
int hideCursor(int state) //1 hide
{
if(state == 1){
if(initialInfo == NULL)
{
initialInfo = (CurInfo *)malloc(sizeof(CurInfo));
GetConsoleCursorInfo(hCon, initialInfo);
}
currentInfo.dwSize = initialInfo->dwSize;
currentInfo.bVisible = false;
SetConsoleCursorInfo(hCon, ¤tInfo);
}else{
if(initialInfo != NULL){
SetConsoleCursorInfo(hCon, initialInfo);
free(initialInfo);
initialInfo = NULL;
}
}
return 0;
}
int setCurrentPos(int press){
switch(press)
{
case 75: //left
curPos.X = curPos.X == 0 ? 0 : curPos.X - 1;break;
case 72: //up
curPos.Y = curPos.Y == 0 ? 0 : curPos.Y - 1;break;
case 77: //right
curPos.X = curPos.X >= MAX_X ? MAX_X : curPos.X + 1;break;
case 80: //down
curPos.Y = curPos.Y >= MAX_Y ? MAX_Y : curPos.Y + 1;break;
default:
return -1;
}
return 0;
}
int main(){
char *str = "人";
printf(str);
hideCursor(1);
while(1){
int press = getch();
if(press == 224) {//方向键第一个值
SetConsoleCursorPosition(hCon, curPos); //FOR CLEAR UP
if(setCurrentPos(getch()) == 0)
{
printf(" "); // clear up, or use system("cls");
SetConsoleCursorPosition(hCon, curPos);printf(str);
}
}else if(press == 27){ //if press esc, exit
hideCursor(0);
break;
}
}
return 0;
}
追问
.obj - 1 error(s), 0 warning(s)
C:\Users ......obj.cpp(21) : error C2065: 'tInfo' : undeclared identifier
这个源码里许多东西我都看不懂,是纯C不?
追答
line 21, SetConsoleCursorInfo(hCon, ¤tInfo);
改成SetConsoleCursorInfo(hCon, & currentInfo); //把 & 和c 之间的空格去掉
是C 但是用到了 windows一些API
& currentInfo 打不出来 ¤tInfo
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询