VC的conio.h头文件取消了光标定位和清屏等函数,但还是有办法实现这些功能的。
查查MSDN帮助,那里有教你。
我这里有一个自己写好的myconio.h,基本上就是抄下了来^_^
#ifndef _MY_CONIO_H_FILE_INCLUDED
#define _MY_CONIO_H_FILE_INCLUDED
#include
#include
#include
#define PERR(bSuccess,api) {if(!(bSuccess)) printf("%s:Error %d from %s on line %d\n",\
__FILE__,GetLastError(),api,__LINE__);}
void MyCls(HANDLE);
void GotoConsoleXY(HANDLE,int,int);
inline void clrscr(void)
{
HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
MyCls(hStdOut);
return;
}
inline void gotoxy(int x,int y)
{
HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
GotoConsoleXY(hStdOut,x,y);
return;
}
void MyCls(HANDLE hConsole)
{
COORD coordScreen={0,0};//设置清屏后光标返回的屏幕左上角坐标
BOOL bSuccess;
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;//保存缓冲区信息
DWORD dwConSize;//当前缓冲区可容纳的字符数
bSuccess=GetConsoleScreenBufferInfo(hConsole,&csbi);//获得缓冲区信息
PERR(bSuccess,"GetConsoleScreenBufferInfo");
dwConSize=csbi.dwSize.X * csbi.dwSize.Y;//缓冲区容纳字符数目
//用空格填充缓冲区
bSuccess=FillConsoleOutputCharacter(hConsole,(TCHAR)' ',dwConSize,coordScreen,&cCharsWritten);
PERR(bSuccess,"FillConsoleOutputCharacter");
bSuccess=GetConsoleScreenBufferInfo(hConsole,&csbi);//获得缓冲区信息
PERR(bSuccess,"ConsoleScreenBufferInfo");
//填充缓冲区属性
bSuccess=FillConsoleOutputAttribute(hConsole,csbi.wAttributes,dwConSize,coordScreen,&cCharsWritten);
PERR(bSuccess,"FillConsoleOutputAttribute");
//光标返回屏幕左上角坐标
bSuccess=SetConsoleCursorPosition(hConsole,coordScreen);
PERR(bSuccess,"SetConsoleCursorPosition");
return;
}
void GotoConsoleXY(HANDLE hConsole,int x,int y)
{
COORD coordScreen={x,y};
SetConsoleCursorPosition(hConsole,coordScreen);
return;
}
#endif //_MY_CONIO_H_FILE_INCLUDED
阅读(2936) | 评论(0) | 转发(0) |