C++控制台代码实现一定的界面效果

以下为主要的头文件代码,只需要在头文件中添加即可。

使用此头文件开发的实例可以参考: C++控制台扫雷.

此头文件的更新版本可以参考: ICUF_HEAD_04.h.

个人博客链接:Tanxl.

开发环境:Visual Studio 2019

ICUF_HEAD_03.h

此头文件中包含了ANSI控制码可以在控制台界面中给部分地方绘制颜色。SystemList功能可以在控制台中做到一定的界面效果,在程序开始的界面中,依靠此功能可以实现上下进行选择,同样也支持输入数字。但因为_getch()语句的原因最多只支持10个数字选项显示。

//ICUF HEAD_03.H
#ifndef ICUF_HEAD_03
#define ICUF_HEAD_03
#define CRT_SECURE_NO_DEPRECATE
#define defaultFunction 0
#include<iostream>
#include<conio.h>
using namespace std;
int SystemList(int FunctionMax, int FunctionS);
int BreakWord{};
inline int NumberCheck(int FunctionMax, int Number)//检查循环数字
{
	if (Number > FunctionMax)
	{
		Number = FunctionMax;
		return Number;
	}
	else return Number;
}
inline void ExitSystem()//退出系统功能
{
	exit(1);
}
inline void Col(int ColN)//设置自定义行的背景颜色
{
	if (ColN == 0)cout << "\033[0m";//清除颜色
	else
	{
		cout << "\033[04m\033[1m\033[7m";
		if (ColN == 1)cout << "\033[31m";//红色
		else if (ColN == 2)cout << "\033[32m";//绿色
		else if (ColN == 3)cout << "\033[33m";//黄色
		else if (ColN == 4)cout << "\033[34m";//蓝色
		else if (ColN == 5)cout << "\033[35m";//紫色
		else if (ColN == 6)cout << "\033[36m";//淡蓝
		else if (ColN == 7)cout << "\033[37m";//白色
	}
}
inline int PageService(char PageWord)
{
	if (PageWord == 'Z')return 1;//上一页
	if (PageWord == 'X')return 2;//下一页
	if (PageWord == 'C')return 3;//返回
	else return 0;
}
int SystemList(int FunctionMax, int FunctionS)//FunctionMax为表格最大行数FunctionS为选择序号
{
	char CHOICE = _getch();
	switch (CHOICE)
	{
	case '1': FunctionS = NumberCheck(FunctionMax, 1); ::BreakWord = 1; break;
	case '2': FunctionS = NumberCheck(FunctionMax, 2); ::BreakWord = 1; break;
	case '3': FunctionS = NumberCheck(FunctionMax, 3); ::BreakWord = 1; break;
	case '4': FunctionS = NumberCheck(FunctionMax, 4); ::BreakWord = 1; break;
	case '5': FunctionS = NumberCheck(FunctionMax, 5); ::BreakWord = 1; break;
	case '6': FunctionS = NumberCheck(FunctionMax, 6); ::BreakWord = 1; break;
	case '7': FunctionS = NumberCheck(FunctionMax, 7); ::BreakWord = 1; break;
	case '8': FunctionS = NumberCheck(FunctionMax, 8); ::BreakWord = 1; break;
	case '9': FunctionS = NumberCheck(FunctionMax, 9); ::BreakWord = 1; break;
	case 72:
	case 'W':
	case 'w':
		if (FunctionS == 1)
		{
			FunctionS = FunctionMax;
			break;
		}
		FunctionS--;
		break;
	case 80:
	case 'S':
	case 's':
		if (FunctionS == FunctionMax)
		{
			FunctionS = 1;
			break;
		}
		FunctionS++;
		break;
	case 'z':
	case 'Z': FunctionS = 'Z'; ::BreakWord = 1; break;
	case 'x':
	case 'X': FunctionS = 'X'; ::BreakWord = 1; break;
	case 'c':
	case 'C': FunctionS = 'C'; ::BreakWord = 1; break;
	case '\r':
		::BreakWord = 1;
		break;
	}
	return FunctionS;
}
#endif

关于如何使用这个头文件,我提供了一个C++的源文件代码。
(C++控制台购物系统的壳子)

//ICUF_02.CPP
#include"ICUF_HEAD_03.h"
#define Version "0.00.__.__ "
#define Edition "      ________ Edition     "
//int BuyFunction(int BuyList);
//int SearchFunction(int SearchList);
void SystemManage(int ManageList);
void SystemMenu(int MenuList);
void SystemWork(int SystemListS, int SystemFunction);
//int SkinWiki(int SkinList);
int main()
{
	system("cls");
	cout << "\n\n\n\n\n\n\n\n\n";
	Col(1); cout << "             \033[33m    \033[31m      \033[33m    \033[31m \033[33m    \033[31m \033[33m          \033[31m \033[33m    \033[31m       \033[33m            \033[31m                           " << endl; Col(0);
	Col(2); cout << "             \033[34m    \033[32m      \033[34m    \033[32m \033[34m    \033[32m \033[34m          \033[32m \033[34m    \033[32m       \033[34m            \033[32m" << Edition << endl; Col(0);
	Col(3); cout << "             \033[35m    \033[33m \033[35m    \033[33m \033[35m    \033[33m \033[35m    \033[33m \033[35m    \033[33m       \033[35m          \033[33m \033[35m    \033[33m           ICUF Version " << Version << endl; Col(0);
	Col(4); cout << "             \033[36m    \033[34m \033[36m    \033[34m \033[36m    \033[34m \033[36m    \033[34m \033[36m    \033[34m       \033[36m          \033[34m \033[36m    \033[34m  \033[36m      \033[34m                           " << endl; Col(0);
	Col(5); cout << "             \033[37m    \033[35m \033[37m    \033[35m \033[37m    \033[35m \033[37m    \033[35m \033[37m    \033[35m       \033[37m    \033[35m  \033[37m    \033[35m \033[37m    \033[35m  \033[37m      \033[35m  \033[37m  \033[35m \033[37m      \033[35m \033[37m  \033[35m  \033[37m  \033[35m  \033[37m      \033[35m " << endl; Col(0);
	Col(6); cout << "             \033[31m    \033[36m \033[31m    \033[36m \033[31m    \033[36m \033[31m    \033[36m \033[31m    \033[36m       \033[31m    \033[36m  \033[31m    \033[36m \033[31m    \033[36m    \033[31m    \033[36m  \033[31m  \033[36m \033[31m  \033[36m     \033[31m  \033[36m  \033[31m  \033[36m  \033[31m  \033[36m     " << endl; Col(0);
	Col(7); cout << "             \033[32m              \033[37m \033[32m    \033[37m \033[32m          \033[37m \033[32m    \033[37m  \033[32m    \033[37m \033[32m            \033[37m  \033[32m  \033[37m \033[32m  \033[37m     \033[32m  \033[37m  \033[32m  \033[37m  \033[32m      \033[37m " << endl; Col(0);
	Col(1); cout << "             \033[33m              \033[31m \033[33m    \033[31m \033[33m          \033[31m \033[33m    \033[31m  \033[33m    \033[31m \033[33m            \033[31m  \033[33m  \033[31m \033[33m      \033[31m \033[33m      \033[31m  \033[33m  \033[31m     " << endl;
	cout << "\n\n\n                                      \a";
	cout << "\n                  ";
	system("pause"); Col(0);
	int MenuList{};
	SystemMenu(MenuList);
	return 0;
}
void SystemMenu(int MenuList)//系统主菜单
{
	int SystemFunction{ 1 };
	int SystemListS{ 0 };
	int CheckPage{};
	BreakWord = 0;
	while (1)
	{
		system("cls");
		switch (MenuList)
		{
		case 0://入口界面
			while (1)
			{
				system("cls");
				Col(1); cout << "\n\n\n\n                         欢迎来到ICUF购物平台  WiChG_Trade             \n\n"; Col(0);
				if (SystemFunction == 1) Col(7);//自定义颜色功能(待添加)
				else Col(1);
				cout << "                购买商品                 \n\n"; Col(0);//BuyFunction
				if (SystemFunction == 2) Col(7);
				else Col(1);
				cout << "                查询订单                 \n\n"; Col(0);//SerachFunction
				if (SystemFunction == 3) Col(7);
				else Col(1);
				cout << "                系统管理                 \n\n"; Col(0);//SystemManage
				if (SystemFunction == 4) Col(7);
				else Col(1);
				cout << "                饰品百科                 \n\n"; Col(0);//SkinWiki
				if (SystemFunction == 5) Col(7);
				else Col(1);
				cout << "                退出系统                 "; Col(0);//ExitSystem
				CheckPage = PageService(SystemFunction);
				if (CheckPage == 1) { cout << "此页面没有上一页"; CheckPage = 0; }
				if (CheckPage == 2) { cout << "此页面没有下一页"; CheckPage = 0; }
				if (CheckPage == 3) { cout << "此页面没有前一页"; CheckPage = 0; }
				if (BreakWord == 1)break;
				SystemFunction = SystemList(5, SystemFunction);//循环赋值
			}
			SystemWork(SystemListS, SystemFunction);
		default:
			break;
		}
		if (BreakWord == 1)break;
	}
}
void SystemWork(int SystemListS, int SystemFunction)//系统工作功能
{
	switch (SystemListS)
	{
	case 0:
	{
		switch (SystemFunction)
		{
		case 1:
		case 2:
		case 3: SystemManage(0);
		case 4:
		case 5: ExitSystem();
		}
	}
	case 3:
	{
		switch (SystemFunction)
		{
		case 1:
		case 2:
		case 3:
		case 4:
		case 5: SystemMenu(defaultFunction);
		}
	}//功能开发中
	}
}
void SystemManage(int ManageList)
{
	int ManageFunction{ 1 };
	int CheckPage{};
	int SystemListS{ 3 };
	BreakWord = 0;
	switch (ManageList)
	{
	case 0:
		while (1)
		{
			system("cls");
			Col(7); cout << "   Wild_Chicken_Programing" << endl; cout << "   ";
			if (ManageFunction == 1) Col(7);
			else Col(6);
			cout << "   价格管理" << endl; Col(7); cout << "   ";
			if (ManageFunction == 2) Col(7);
			else Col(6);
			cout << "   新增商品" << endl; Col(7); cout << "   ";
			if (ManageFunction == 3) Col(7);
			else Col(6);
			cout << "   清除痕迹" << endl; Col(7); cout << "   ";
			if (ManageFunction == 4) Col(7);
			else Col(6);
			cout << "   储存设置" << endl; Col(7); cout << "   ";
			if (ManageFunction == 5) Col(7);
			else Col(6);
			cout << "   返回主页" << endl; Col(7); cout << "   "; Col(0);
			CheckPage = PageService(ManageFunction);
			if (CheckPage == 1) { cout << "此页面没有上一页"; CheckPage = 0; }
			if (CheckPage == 2) { cout << "此页面没有下一页"; CheckPage = 0; }
			if (CheckPage == 3) { cout << "此页面没有前一页"; CheckPage = 0; }
			if (BreakWord == 1)break;
			ManageFunction = SystemList(5, ManageFunction);//循环赋值
		}
		SystemWork(SystemListS, ManageFunction);
	}
}

以下为运行的一些截图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Wild_Chicken_Programing TANXL
TIPS:\033[33m等类似的代码在部分系统中会输出字符而不是颜色
2021/1/30 22:53 在改进ICUF_HEAD_03.h时发现ICUF_02代码运行有问题,已修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WiChP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值