任意位置输出字符

屏幕坐标系
image.png

  1. #include <windows.h>
  2. #inlude <iostream>
  3. using namespace std;
  4. void gotoxy(int x, int y) {
  5. HANDLE h; // 句柄,对象的索引
  6. COORD c; // 结构体,坐标值
  7. c.X = x;
  8. c.Y = y;
  9. h = GetStdHandle(STD_OUTPUT_HANDLE);
  10. SetConsoleCursorPosition(h, c);
  11. }

image.png

一个动画小程序

  1. #include <windows.h>
  2. #include <iostream>
  3. using namespace std;
  4. void gotoxy(int x, int y)
  5. {
  6. HANDLE h;
  7. COORD c;
  8. c.X = x;
  9. c.Y = y;
  10. h = GetStdHandle(STD_OUTPUT_HANDLE);
  11. SetConsoleCursorPosition(h, c);
  12. }
  13. void move(int x0, int y0)
  14. {
  15. int x = x0, y = y0;
  16. while (true)
  17. {
  18. gotoxy(x, y);
  19. cout << "+";
  20. Sleep(10);
  21. gotoxy(x, y);
  22. cout << " ";
  23. ++x;
  24. if (x >= 100) x = 0; // 等价于 x = x % 100;
  25. }
  26. }
  27. int main()
  28. {
  29. move(0, 10);
  30. return 0;
  31. }

这里的Sleep()函数是VS中的,其他环境中的需要做修改

按键动作判断

kbhit() :需要头文件”conio.h“ 返回值为bool
将上一个程序的 move 函数改动一下,并且包含上述头文件

  1. void move(int x0, int y0)
  2. {
  3. int x = x0, y = y0;
  4. while (!_kbhit())
  5. {
  6. gotoxy(x, y);
  7. cout << "+";
  8. Sleep(10);
  9. gotoxy(x, y);
  10. cout << " ";
  11. ++x;
  12. if (x >= 100) x = 0; // 等价于 x = x % 100;
  13. }
  14. }

在VS中,函数名是_kbhit()

接收键盘码

getch() 需要头文件 “conio.h

  1. #include <conio.h>
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. char c;
  7. c = _getch();
  8. cout << (int)c << endl;
  9. return 0;
  10. }

常用控制键的键盘码

image.png
所以按下一次键盘按键,会产生两个键盘码,需要两个 char 类型来接受

  1. #include <conio.h>
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. char c1, c2;
  7. c1 = _getch();
  8. c2 = _getch();
  9. cout << (int)c1 << "," << (int)c2 << endl;
  10. return 0;
  11. }

可以通过 kbhit() 函数来判断是否按下键盘按键;这个函数同样包含在”conio.h”头文件中

  1. void key() {
  2. if (kbhit()) {
  3. ...
  4. }
  5. }

接收组合键

GetAsyncKeyState(按键的VK值)
组合键的使用必须要配合计时器的使用,否则会产生按键抖动(产生多次对应按键的返回值)

  1. void move(int x0, int y0)
  2. {
  3. int x = x0, y = y0;
  4. clock_t t;
  5. t = clock();
  6. bool flag = true;
  7. while (true)
  8. {
  9. if (flag == true) {
  10. gotoxy(x, y);
  11. cout << "●";
  12. flag = false;
  13. }
  14. if(clock() - t > 10) {
  15. if (GetAsyncKeyState(VK_ESCAPE)) exit(0);
  16. if (GetAsyncKeyState(VK_LEFT)) {
  17. ...
  18. }
  19. if (GetAsyncKeyState(VK_RIGHT)) {
  20. ...
  21. }
  22. if (GetAsyncKeyState(VK_UP)) {
  23. ...
  24. }
  25. if (GetAsyncKeyState(VK_DOWN)) {
  26. ...
  27. }
  28. }
  29. }
  30. }

键盘控制小动画

使用方向键控制小球移动,使用ESC键退出

  1. #include <conio.h>
  2. #include <Windows.h>
  3. #include <iostream>
  4. using namespace std;
  5. void gotoxy(int x, int y)
  6. {
  7. HANDLE handle;
  8. COORD coord;
  9. coord.X = x;
  10. coord.Y = y;
  11. handle = GetStdHandle(STD_OUTPUT_HANDLE);
  12. SetConsoleCursorPosition(handle, coord);
  13. }
  14. void moveto(int x0, int y0)
  15. {
  16. int x = x0, y = y0;
  17. char c1, c2;
  18. while (true)
  19. {
  20. gotoxy(x, y);
  21. cout << "●";
  22. c1 = _getch();
  23. if (c1 == 27) break;
  24. if (c1 == -32)
  25. {
  26. gotoxy(x, y);
  27. cout << " ";
  28. c2 = _getch();
  29. switch (c2)
  30. {
  31. case 72:
  32. y = (y - 1) % 50;
  33. break;
  34. case 75:
  35. x = (x - 1) % 100;
  36. break;
  37. case 77:
  38. x = (x + 1) % 100;
  39. break;
  40. case 80:
  41. y = (y + 1) % 50;
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. int main()
  48. {
  49. moveto(10, 10);
  50. return 0;
  51. }

完善界面的几个函数

光标的隐藏与显示

  1. void hideCursor()
  2. {
  3. HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
  4. CONSOLE_CURSOR_INFO cursor_info;
  5. GetConsoleCursorInfo(handle, &cursor_info);
  6. cursor_info.bVisible = false; // 不显示光标
  7. // cursor_info.bVisible = false; // 显示光标
  8. SetConsoleCursorInfo(handle, &cursor_info);
  9. }
  10. int main()
  11. {
  12. hideCursor();
  13. moveto(10, 10);
  14. showCursor(); // 隐藏了光标后一定要再弄出来,不然别的程序可能看不到了
  15. return 0;
  16. }

输出彩色字符

  1. #include <Windows.h>
  2. #include <iostream>
  3. using namespace std;
  4. void color(int a) {
  5. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
  6. }
  7. int main() {
  8. for(int i = 0; i < 128; ++i) {
  9. color(i);
  10. // 测试颜色
  11. cout << i << "-Hello" << endl;
  12. }
  13. }