提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
如何使用C++语言在控制台中实现用箭头来回选择并进入调用函数的效果
一、图片演示
二、代码演示
这是伪代码,还需要头文件:
#include<stdio.h> #include<Windows.h> #include<conio.h> #include<iostream>以及一些预处理指令
#define
定义一些常量。#define row 30 //i
#define col 30 //j
#define UP 72 //向上键
#define DOWN 80 //向下键
#define LEFT 75 //向左键
#define RIGHT 77 //向右键
#define SPACE 32 //空格键
#define ESC 27 //退出键以及一些对光标进行改变的函数实现:
void cursorgoto(int x, int y) {
COORD p; //定义光标位置的结构体变量
p.X = x;
p.Y = y;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
SetConsoleCursorPosition(handle, p); //设置光标属性
}
void loginview()
{
string username = "";
string password = "";
int input;//定义一个变量,用来接收等会你在键盘上输入的信息
int choice = 0;
cursorgoto(col / 2 + 8, 5); //这个调用能将光标移动到控制台上的地方(cursorgoto(列,行))
cout << "登陆界面";
cursorgoto(0, 21);
cout << "上下键确认行,左右键确认列,enter键确认选项,esc键不录入成绩";
cursorgoto(col / 2 - 4, 7);
cout << "+------------------------------+" << endl;
cursorgoto(col / 2 - 4, 8);
cout << "|用户名|: |" << endl;
cursorgoto(col / 2 - 4, 9);
cout << "+------------------------------+" << endl;
cursorgoto(col / 2 - 4, 10);
cout << "| 密码 |: |" << endl;
cursorgoto(col / 2 - 4, 11);
cout << "+------------------------------+" << endl;
cursorgoto(col / 2 - 4, 15);
cout << "确认登录" << " " << "忘记密码" << " " << "注册";
int line = col / 2 - 8; //设定光标列坐标变量为col/2-8=7
cursorgoto(line, 8); //将光标放置在第8行,7列的地方
printf("--》");
while (1) { //此循环能够使箭头通过键盘上下左右键来移动
input = _getch(); //接受键盘输入信息到input变量
switch (input) { //实现上下选择
case UP:
choice = (choice - 1 + 3) % 3; //2
break;
case DOWN:
choice = (choice + 1) % 3; //1
break;
case '\r': //enter进入
switch (choice) {
case 0:
cursorgoto(col / 2 + 6, 8);
cin >> username;
user = username;
break;
case 1:
cursorgoto(col / 2 + 6, 10);
cin >> password;
break;
case 2:
int choice2 = 0; /*这个地方一定要写在while的定义域外面,
不然每次循环都会将choice初始化为0,这是chat gpt告诉我的,谢
谢gpt*/
while (1) //进行水平方向的箭头移动
{
int ch = 0;
ch = _getch();
switch (ch)
{
case LEFT:
choice2 = (choice2 - 1 + 3) % 3;//2
break;
case RIGHT:
choice2 = (choice2 + 1) % 3;//1
break;
case '\r':
switch (choice2)
{
case 0:
if (!login(username, password) || username == "" || password == "")
//如果有用户名和密码不匹配,或者用户名和密码为空的话
{
cursorgoto(col / 2 - 4, 17);
cout << "请输入正确的用户名与密码";
break;
}
//匹配信息,录入成绩,结束
system("cls");
menuview();
break;
case 1:
//忘记密码
system("cls");
forgetview();
break;
case 2:
//注册
system("cls");
registerview();
break;
}
break;
}
if (choice2 == 0)//打印垂直方向的箭头移动
{
cursorgoto(7, 15);
printf("--》");
cursorgoto(line + 14, 15);
cout << " ";
cursorgoto(line + 28, 15);
cout << " ";
cursorgoto(line + 4, 15);
}
if (choice2 == 1)
{
cursorgoto(line, 15);
printf(" ");
cursorgoto(line + 14, 15);
cout << "--》";
cursorgoto(line + 28, 15);
cout << " ";
cursorgoto(line + 18, 15);
}
if (choice2 == 2)
{
cursorgoto(line, 15);
printf(" ");
cursorgoto(line + 14, 15);
cout << " ";
cursorgoto(line + 28, 15);
cout << "--》";
cursorgoto(line + 32, 15);
}
}
break;
}
break;
case ESC:
system("cls");
menuview();
break;
}
if (choice == 0) { //打印水平方向的箭头移动
cursorgoto(col / 2 - 8, 8);
printf("--》");
cursorgoto(col / 2 - 8, 10);
cout << " ";
cursorgoto(col / 2 - 8, 15);
cout << " ";
cursorgoto(col / 2 - 4, 8);
}
if (choice == 1) {
cursorgoto(col / 2 - 8, 10);
printf("--》");
cursorgoto(col / 2 - 8, 8);
cout << " ";
cursorgoto(col / 2 - 8, 15);
cout << " ";
cursorgoto(col / 2 - 4, 10);
}
if (choice == 2) {
cursorgoto(col / 2 - 8, 15);
printf("--》");
cursorgoto(col / 2 - 8, 8);
cout << " ";
cursorgoto(col / 2 - 8, 10);
cout << " ";
cursorgoto(col / 2 - 4, 15);
}
}
}
上面的代码是伪代码,可能需要一些函数的调用,下面的代码是我重新敲的简单版,可以直接在编辑器上运行出来。
#include <stdio.h>
#include <Windows.h>
#include <conio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <fstream>
using namespace std;
#define row 30 //i
#define col 30 //j
#define UP 72 //向上键
#define DOWN 80 //向下键
#define LEFT 75 //向左键
#define RIGHT 77 //向右键
#define SPACE 32 //空格键
#define ESC 27 //退出键
void cursorgoto(int x, int y) {
COORD p; //定义光标位置的结构体变量
p.X = x;
p.Y = y;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
SetConsoleCursorPosition(handle, p); //设置光标属性
}
void setPrintColor(int color) { //得到控制台句柄
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
/*调用的是windows API里面的函数,
将标准输出流的句柄和颜色属性作为参数传递给该函数,
可以更改控制台输出文本的颜色和背景颜色。*/
}
void view() {
int input = 0;
int choice = 0;
cout << "*******欢迎来到谭智鹏大本营*******" << endl;
cout << "==================================" << endl;
if (choice == 0)
setPrintColor(111);
cout << "-------------开始界面-------------" << endl;
setPrintColor(15);
cout << "-------------设置界面-------------" << endl;
cout << "-------------排行榜面-------------" << endl;
cout << "-------------我欲封天-------------" ;
while (1) {
input = _getch();
switch (input) {
case UP:
choice = (choice - 1 + 4) % 4;
break;
case DOWN:
choice = (choice + 1) % 4;
break;
case '\r':
switch (choice) {
case 0:
system("cls");
cout << "成神的道路" << endl;
break;
case 1:
system("cls");
cout << "成魔的道路" << endl;
break;
case 2:
system("cls");
cout << "成狗的道路" << endl;
break;
case 3:
exit(1);
}
break;
default:
break;
}
if (choice == 0)
setPrintColor(111);
cursorgoto(0, 2);
cout << "-------------开始界面-------------" << endl;
setPrintColor(15);
if (choice == 1)
setPrintColor(111);
cursorgoto(0, 3);
cout << "-------------设置界面-------------" << endl;
setPrintColor(15);
if (choice == 2)
setPrintColor(111);
cursorgoto(0, 4);
cout << "-------------排行榜面-------------" << endl;
setPrintColor(15);
if (choice == 3)
setPrintColor(111);
cursorgoto(0, 5);
cout << "-------------我欲封天-------------" << endl;
setPrintColor(15);
}
}
int main() {
view();
return 0;
}
总结
总之就是,使用while循环,然后利用switch来判断键盘输入的信息,
来改变某一变量的值,然后用if通过对变量的判断来打印箭头在哪出现。