主要包括:
1、输出坐标控制;
2、颜色修改;
3、高亮显示方向键选取菜单项;
4、尺寸修改。
说明:
(1)每种颜色对应的十六进制数如下【0-黑色、1-蓝色、2-绿色、3-湖蓝色、4-红色、5-紫色、6-黄色、7-白色、8-灰色、9-淡蓝色、10-淡绿色、11-淡浅绿色、12-淡红色、13-淡紫色、14-淡黄色、15-亮白色】。
(2)文字前景色:
FOREGROUND_RED 红色
FOREGROUND_GREEN 绿色
FOREGROUND_BLUE 蓝色
FOREGROUND_RED|FOREGROUND_GREEN 黄色
FOREGROUND_RED|FOREGROUND_BLUE 紫红色
FOREGROUND_GREEN|FOREGROUND_BLUE 淡蓝色
FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE 白色
0 黑色
FOREGROUND_INTENSITY 亮色(比原来颜色要亮 )
(3)文字背景色:
BACKGROUND_RED 红色
BACKGROUND_GREEN 绿色
BACKGROUND_BLUE 蓝色
BACKGROUND_RED|BACKGROUND_GREEN 黄色
BACKGROUND_RED|BACKGROUND_BLUE 紫红色
BACKGROUND_GREEN|BACKGROUND_BLUE 淡蓝色
BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE 白色
0 黑色
视频链接:https://www.51zxw.net/Show.aspx?cid=717&id=78204。
C++代码:
1与2的实现
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
using namespace std;
void SetOutputPosition(int x, int y) {
HANDLE h;//句柄
h = GetStdHandle(STD_OUTPUT_HANDLE);//获得标准句柄
COORD pos;//坐标
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(h, pos);//设置控制台界面坐标
}
int main() {
srand(time(0));
int x = 0;//纵坐标
int y = 0;//横坐标
char c;
HANDLE h;//句柄
h = GetStdHandle(STD_OUTPUT_HANDLE);//获得标准句柄
//system("color 0E");
while (true) {
SetOutputPosition(x, y);
SetConsoleTextAttribute(h, 0|0);
cout << " ";
x = rand() % 40;
y = rand() % 18;
SetOutputPosition(x, y);
SetConsoleTextAttribute(h, FOREGROUND_RED | BACKGROUND_BLUE);
cout << "欢迎使用!";
c = _getch();
if (c == VK_SPACE)
break;
}
return 0;
}
3和4的实现
在这里插入代码片#include<iostream>
#include<windows.h>
#include<conio.h>
using namespace std;
void SetOutputPosition(int x, int y) {
HANDLE h;//句柄
h = GetStdHandle(STD_OUTPUT_HANDLE);//获得标准句柄
COORD pos;//坐标
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(h, pos);//设置控制台界面坐标
}
void GetConsoleSize(short *cols, short *lines) {
HANDLE h;//句柄
h = GetStdHandle(STD_OUTPUT_HANDLE);//获得标准句柄
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(h, &csbi);
*cols = csbi.dwSize.X;
*lines = csbi.dwSize.Y;
}
int main() {
system("mode con cols=50 lines=20");
short cols, lines;
GetConsoleSize(&cols, &lines);
HANDLE h;//句柄
h = GetStdHandle(STD_OUTPUT_HANDLE);//获得标准句柄
SetOutputPosition(15, 5);
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "你现在想做什么?" << endl;
int choice = 1;
char c = 0;
while (true) {
if (c == 77)//右方向键
choice = choice % 3 + 1;
if (c == 75)//左方向键
choice = (choice == 1 ? 3 : choice - 1);
if (c == VK_SPACE)
break;
SetOutputPosition(10, 7);
if (choice == 1)
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "学习";
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
SetOutputPosition(20, 7);
if (choice == 2)
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "玩耍";
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
SetOutputPosition(30, 7);
if (choice == 3)
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "看电视";
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
c = _getch();
if (c == 0)
c = _getch();
}
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << endl << endl;
cout << "你做出了正确的选择。" << endl;
return 0;
}
C++运行结果:
1与2的结果
3和4的结果