//Windows 有与 DOS 完全不同的方式来支持文本。
//Windows 控制台模式程序 (Win9x/Me/2000/XP/2003等都支持)
//请用 Visual C++ 或者 Borland C++ Builder
//在 Windows 的 DOS 窗口下 或者 DOS 窗口全屏幕模式运行。
//这个程序可以实现清屏,可以让 cout 和 printf 等标准输出带颜色、光标定位输出
#include
int main(int argc, char* argv[])
{
COORD CurPos;
DWORD nBytesWrite;
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO ConInfo;
//控制台信息
SetConsoleTitle("程序执行时控制台视窗的标题");
GetConsoleScreenBufferInfo(hCon, & ConInfo);
//执行清屏命令
CurPos.X = ConInfo.srWindow.Left;
CurPos.Y = ConInfo.srWindow.Top;
FillConsoleOutputAttribute(hCon, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE, ConInfo.dwSize.X*ConInfo.dwSize.Y, CurPos, & nBytesWrite);
FillConsoleOutputCharacter(hCon, 0x20, ConInfo.dwSize.X*ConInfo.dwSize.Y, CurPos, & nBytesWrite);
//移动光标,设置颜色
CurPos.X = (ConInfo.srWindow.Right + ConInfo.srWindow.Left - 30)/2;
CurPos.Y = (ConInfo.srWindow.Bottom + ConInfo.srWindow.Top)/2;
SetConsoleCursorPosition(hCon, CurPos);
SetConsoleTextAttribute(hCon, FOREGROUND_GREEN|FOREGROUND_INTENSITY|BACKGROUND_BLUE);
//输出文字
cout << "屏幕中央显示蓝色背景的绿色文字";
//移动光标,设置颜色
CurPos.X = ConInfo.srWindow.Left;
CurPos.Y = ConInfo.srWindow.Bottom;
SetConsoleCursorPosition(hCon, CurPos);
SetConsoleTextAttribute(hCon, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
//等待用户回车
char Buf[128];
cout << "Press Enter...";
cin.getline(Buf,128);
return 0;
}
--------------------next---------------------
阅读(1090) | 评论(0) | 转发(0) |