任意位置输出字符
屏幕坐标系
#include <windows.h>#inlude <iostream>using namespace std;void gotoxy(int x, int y) {HANDLE h; // 句柄,对象的索引COORD c; // 结构体,坐标值c.X = x;c.Y = y;h = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(h, c);}
一个动画小程序
#include <windows.h>#include <iostream>using namespace std;void gotoxy(int x, int y){HANDLE h;COORD c;c.X = x;c.Y = y;h = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(h, c);}void move(int x0, int y0){int x = x0, y = y0;while (true){gotoxy(x, y);cout << "+";Sleep(10);gotoxy(x, y);cout << " ";++x;if (x >= 100) x = 0; // 等价于 x = x % 100;}}int main(){move(0, 10);return 0;}
这里的Sleep()函数是VS中的,其他环境中的需要做修改
按键动作判断
kbhit() :需要头文件”conio.h“ 返回值为bool
将上一个程序的 move 函数改动一下,并且包含上述头文件
void move(int x0, int y0){int x = x0, y = y0;while (!_kbhit()){gotoxy(x, y);cout << "+";Sleep(10);gotoxy(x, y);cout << " ";++x;if (x >= 100) x = 0; // 等价于 x = x % 100;}}
在VS中,函数名是_kbhit()
接收键盘码
getch() 需要头文件 “conio.h“
#include <conio.h>#include <iostream>using namespace std;int main(){char c;c = _getch();cout << (int)c << endl;return 0;}
常用控制键的键盘码

所以按下一次键盘按键,会产生两个键盘码,需要两个 char 类型来接受
#include <conio.h>#include <iostream>using namespace std;int main(){char c1, c2;c1 = _getch();c2 = _getch();cout << (int)c1 << "," << (int)c2 << endl;return 0;}
可以通过 kbhit() 函数来判断是否按下键盘按键;这个函数同样包含在”conio.h”头文件中
void key() {if (kbhit()) {...}}
接收组合键
GetAsyncKeyState(按键的VK值)
组合键的使用必须要配合计时器的使用,否则会产生按键抖动(产生多次对应按键的返回值)
void move(int x0, int y0){int x = x0, y = y0;clock_t t;t = clock();bool flag = true;while (true){if (flag == true) {gotoxy(x, y);cout << "●";flag = false;}if(clock() - t > 10) {if (GetAsyncKeyState(VK_ESCAPE)) exit(0);if (GetAsyncKeyState(VK_LEFT)) {...}if (GetAsyncKeyState(VK_RIGHT)) {...}if (GetAsyncKeyState(VK_UP)) {...}if (GetAsyncKeyState(VK_DOWN)) {...}}}}
键盘控制小动画
使用方向键控制小球移动,使用ESC键退出
#include <conio.h>#include <Windows.h>#include <iostream>using namespace std;void gotoxy(int x, int y){HANDLE handle;COORD coord;coord.X = x;coord.Y = y;handle = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(handle, coord);}void moveto(int x0, int y0){int x = x0, y = y0;char c1, c2;while (true){gotoxy(x, y);cout << "●";c1 = _getch();if (c1 == 27) break;if (c1 == -32){gotoxy(x, y);cout << " ";c2 = _getch();switch (c2){case 72:y = (y - 1) % 50;break;case 75:x = (x - 1) % 100;break;case 77:x = (x + 1) % 100;break;case 80:y = (y + 1) % 50;break;}}}}int main(){moveto(10, 10);return 0;}
完善界面的几个函数
光标的隐藏与显示
void hideCursor(){HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO cursor_info;GetConsoleCursorInfo(handle, &cursor_info);cursor_info.bVisible = false; // 不显示光标// cursor_info.bVisible = false; // 显示光标SetConsoleCursorInfo(handle, &cursor_info);}int main(){hideCursor();moveto(10, 10);showCursor(); // 隐藏了光标后一定要再弄出来,不然别的程序可能看不到了return 0;}
输出彩色字符
#include <Windows.h>#include <iostream>using namespace std;void color(int a) {SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);}int main() {for(int i = 0; i < 128; ++i) {color(i);// 测试颜色cout << i << "-Hello" << endl;}}
