控制台绘制登陆框(三): TAB切换的改进

TAB切换的改进

一 效果展示:

效果图同上篇  <<控制台绘制登陆框(二)>>一致。

二代码

#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <windows.h>
#define KEY_TAB 9
#define BACKGROUND_BlACK 0
#define BACKGROUND_WHITE 255
using namespace std;
typedef struct myinput{
int height;
int width;
int bgColor;
int fgColor;
int startPosY;
int startPosX;
}MYINPUT;




typedef struct mybutton{
int height;
int width;
int bgColor;
int fgColor;
int startPosY;
int startPosX;
string str;
}MYBUTTON;
/**
function: 将字符输出到缓冲区
@param
ch:        要输入的字符
posLine :   字符所在行的位置
posColumn:  字符所在列的位置
charInfoAarray:缓冲区数组的内容
**/
void WriteCharToBuffer(CHAR_INFO ch, int posLine, int posColumn, CHAR_INFO * charInfoAarray)
{
charInfoAarray[posLine * 80 + posColumn] = ch;
}
/***
function:将缓冲区的字符输出到控制台
@param
startPosRow:显示区域起始点的行号
startPosColumn:显示区域起始点的列号
height:显示区域高
width:显示区域宽
charInfoAarray:缓冲区数组的内容
***/
void OutPutToConsole(int startPosRow, int startPosColumn, int height, int width, CHAR_INFO * charInfoAarray)
{
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwBufferSize = { 80, 25 };//固定值(控制台窗口固定的属性值)
COORD dwBufferCoord = { startPosColumn, startPosRow };//缓冲区左上角的起始位置
SMALL_RECT rect = { startPosColumn, startPosRow, startPosColumn + width - 1, startPosRow + height - 1 };//显示多大的缓冲区:
WriteConsoleOutput(hConsoleOutput, charInfoAarray, dwBufferSize, dwBufferCoord, &rect);
}




/**
function :在控制台输出文本
@param
text:要输出的文本
row:输出文本的行数
column:输出文本的列数
startPosRow: 第一个字符所在行数
startPosColumn: 第一个字符所在列数
bgColor: 背景色
fgColor;前景色




**/
void drawText(string text, int row, int column, int startPosRow, int startPosColumn, int bgColor, int fgColor)
{
int  i = 0;
CHAR_INFO CharInfo;
CharInfo.Attributes = bgColor | fgColor;
CHAR_INFO  charInfoArray[80 * 25];
for (i = 0; i < text.length(); i++)
{
CharInfo.Char.AsciiChar = text[i];
WriteCharToBuffer(CharInfo, startPosRow, startPosColumn + i, charInfoArray);
}
OutPutToConsole(startPosRow, startPosColumn, row, column, charInfoArray);
}
/**
function 画矩形
@param
height:矩形的高
width:矩形的宽
startPosRow:矩形左上角纵坐标的位置
startPosColumn:矩形左上角横坐标的位置
bgColor:背景色
fgColor:前景色
**/
void drawRect(int height, int width, int leftPosY, int leftPosX, int bgColor, int fgColor)
{
int  i = 0, j = 0;
CHAR_INFO CharInfo;
CharInfo.Attributes = bgColor | fgColor;
CHAR_INFO  charInfoAarray[80 * 25];
for (i = leftPosY; i < leftPosY + height; i++)
{
for (j = leftPosX; j < leftPosX + width; j++)
{
CharInfo.Char.AsciiChar = ' ';
WriteCharToBuffer(CharInfo, i, j, charInfoAarray);
}




}




OutPutToConsole(leftPosY, leftPosX, height, width, charInfoAarray);
}




/**
隐藏光标
**/
void HideCursor()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}




/**
显示光标
**/
void ShowCursor()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = true; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}
void main()
{








string strUserName = "USERNAME";
string strPassword = "PASSWORD";
COORD cursorPos = { 0, 0 };//光标所在的位置
int key = 0;//接收键盘输入
int i = 1;
MYINPUT InputUserName;
MYINPUT InputPassWord;
MYBUTTON btnLogin;
drawText(strUserName, 1, 8, 9, 25, FOREGROUND_RED, BACKGROUND_BlACK);//在指定位置输入USERNAME字符串
drawText(strPassword, 1, 8, 11, 25, FOREGROUND_RED, BACKGROUND_BlACK);//在指定位置输入PASSWORD字符串




InputUserName.height = 1;
InputUserName.width = 20;
InputUserName.bgColor = BACKGROUND_WHITE;
InputUserName.fgColor = FOREGROUND_RED;
InputUserName.startPosX = 35;
InputUserName.startPosY = 9;
drawRect(InputUserName.height, InputUserName.width, InputUserName.startPosY, InputUserName.startPosX, InputUserName.bgColor, InputUserName.fgColor);




InputPassWord.height = 1;
InputPassWord.width = 20;
InputPassWord.bgColor = BACKGROUND_WHITE;
InputPassWord.fgColor = FOREGROUND_RED;
InputPassWord.startPosX = 35;
InputPassWord.startPosY = 11;
drawRect(InputPassWord.height, InputPassWord.width, InputPassWord.startPosY, InputPassWord.startPosX, InputPassWord.bgColor, InputPassWord.fgColor);




btnLogin.str = "LOGIN";
btnLogin.bgColor = BACKGROUND_GREEN;
btnLogin.fgColor = FOREGROUND_RED;
btnLogin.height = 3;
btnLogin.width = 10;
btnLogin.startPosX = 35;
btnLogin.startPosY = 14;
drawRect(btnLogin.height, btnLogin.width, btnLogin.startPosY, btnLogin.startPosX, btnLogin.bgColor, btnLogin.fgColor);
drawText(btnLogin.str, btnLogin.height / 2, btnLogin.str.length(), btnLogin.startPosY + (btnLogin.height / 2), btnLogin.startPosX + (btnLogin.width - btnLogin.str.length()) / 2, btnLogin.bgColor, btnLogin.fgColor);//在指定位置输入LOGIN字符串




cursorPos = { InputUserName.startPosX, InputUserName.startPosY };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPos);






while (1)
{
key = _getch();
switch (key)
{
case KEY_TAB:
i = i + 1;
if (i%3 == 1)//余数为1时,切换的用户名输入框
{
//绘制button按钮,改变颜色
btnLogin.bgColor = BACKGROUND_GREEN;
drawRect(btnLogin.height, btnLogin.width, btnLogin.startPosY, btnLogin.startPosX, btnLogin.bgColor, btnLogin.fgColor);
drawText(btnLogin.str, btnLogin.height / 2, btnLogin.str.length(), btnLogin.startPosY + (btnLogin.height / 2), btnLogin.startPosX + (btnLogin.width - btnLogin.str.length()) / 2, btnLogin.bgColor, btnLogin.fgColor);//在指定位置输入LOGIN字符串


ShowCursor();
cursorPos = { InputUserName.startPosX, InputUserName.startPosY };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPos);



}
else if (i % 3 == 2)//余数为2时,切换到密码输入框
{
cursorPos = { InputPassWord.startPosX, InputPassWord.startPosY };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPos);

}
else if (i % 3 == 0)//余数为0时,切换到登陆按钮
{
cursorPos = { btnLogin.startPosX, btnLogin.startPosY };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPos);

HideCursor();
//绘制button按钮,改变颜色
btnLogin.bgColor = BACKGROUND_BLUE;
drawRect(btnLogin.height, btnLogin.width, btnLogin.startPosY, btnLogin.startPosX, btnLogin.bgColor, btnLogin.fgColor);
drawText(btnLogin.str, btnLogin.height / 2, btnLogin.str.length(), btnLogin.startPosY + (btnLogin.height / 2), btnLogin.startPosX + (btnLogin.width - btnLogin.str.length()) / 2, btnLogin.bgColor, btnLogin.fgColor);//在指定位置输入LOGIN字符串
}
break;
default:
break;
}
}
getchar();




}



三 改进的地方

用求余的算法进行TAB切换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值