#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <windows.h>
#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 main()
{
string strUserName = "USERNAME";
string strPassword = "PASSWORD";
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字符串
getchar();
}