目录
效果展示
注:此篇文章中 CTools工具类 没有以类的形式呈现,在博主后面的文章中有进行改进,想要学习的可以阅读博主后面的文章
源码分享
main.cpp [主入口测试]
#include<iostream>
using namespace std;
#include "CLabel.h"
#include"CEdit.h"
#include"CButton.h"
#include"CTools.h"
int main()
{
paintWindow(17,4,24,22);
CLabel title(35,7,10,2,"登录界面");
title.show();
CLabel labName(28,11,10,2,"用户名:");
labName.show();
CLabel labPwd(28,15,10,2,"密码:");
labPwd.show();
//int x,int y,int w ,int h,char *pcontent,int maxLen,int inputType,int ips
CEdit editName(38,11,10,3,"",10,1,1);
editName.show();
CEdit editPwd(38,15,10,3,"",10,1,1);
editPwd.show();
CButton btnlogin(25,18,6,3,"登录");
btnlogin.show();
CButton btnexit(45,18,6,3,"退出");
btnexit.show();
gotoxy(0,29);
return 0;
}
CTools.h [工具类 头文件]
#ifndef CTOOLS_H
#define CTOOLS_H
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define KEY_ESC 27
#define KEY_ENTER 13
void paintWindow(int startX,int startY,int width,int height);//打印窗口
void gotoxy(int x,int y);//形参 光标定位
/*
函数功能:字符串输入控制
函数参数:
int maxLen 允许输入最大长度
int inputType 允许输入的字符类型 0:数字 1:字母 2:字母+数字
int ips 数据显示格式 0:密文 1:明文
char str[] 存储输入字符
函数返回值:无
char str[] char str[20] char*str
*/
void glb_string(int maxLen,int inputType,int ips,char str[]);
/*
函数名:int getkey()
函数功能:获取用户输入的任意键盘值
函数参数:无
函数返回:ascii
*/
int getkey();
#endif
CTools.cpp [工具类 源文件]
#include "CTools.h"
#include <windows.h> //引用库头文件
#include<stdio.h>
#include<conio.h>
void gotoxy(int x,int y)//形参
{
HANDLE hOut;
COORD pos= {x,y};
// 光标的起始位(第1列,第3行) 0是第1列 2是第3行
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
//printf("定位光标位置搜索(%d,%d)\n",pos.X,pos.Y);
}
void paintWindow(int startX,int startY,int width,int height)
{
int i=0;
//打印顶部
//光标定位 找规律 ┗ ┛ ┏ ┓ ┃ ━
//顶部 y不变 x 在变 (x-x+w)
gotoxy(startX,startY);
printf("┏");
for(i=0;i<width-2;i++) //控制循环次数width-2次
{
printf("━ ");
}
printf("┓");
//左边 x不变 y在变 (y到y+height)
for(i=1;i<=height-1;i++)
{
gotoxy(startX,startY+i);
printf("┃");
}
//右边 x不变 =startx+width-1 y在变 y 到 y+height
for(i=1;i<=height-1;i++)
{
gotoxy(startX+width*2-3,startY+i);
printf("┃");
}
//底部 x在变化 y不变 (x到x+width) ━
gotoxy(startX,startY+height-1);
printf("┗");
for(i=0;i<width-2;i++)
{
printf("━ ");
}
printf("┛");
gotoxy(startX,startX+height);
}
void glb_string(int maxLen,int inputType,int ips,char str[])
{
char ch=0;
int i=0;
while(1)
{
ch=getch();
if(ch=='\r'&&i>0)//换行结束输入
{
break;
}
switch(inputType)
{
case 0:
if(ch>='0'&&ch<='9'&&i<maxLen)
{
if(ips==0)
{
putch('*');
}
else{
putch(ch);
}
str[i++]=ch;
}
break;
case 1:
if(i<maxLen&&(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
if(ips==0)
{
putch('*');
}
else{
putch(ch);
}
str[i++]=ch;
}
break;
case 2:
if(i<maxLen&&(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9'))
{
if(ips==0)
{
putch('*');
}
else{
putch(ch);
}
str[i++]=ch;
}
break;
default:
break;
}
}
}
/*
函数名:int getkey()
函数功能:获取用户输入的任意键盘值
函数参数:无
函数返回:ascii
*/
int getkey()
{
char key=0;
key=getch();
if(key==-32)//方向按键 第一个值都是32
{
key=getch();
switch(key){
case 72:
return KEY_UP;
case 80:
return KEY_DOWN;
case 75:
return KEY_LEFT;
case 77:
return KEY_RIGHT;
}
}
else if(key==13)
{
return KEY_ENTER;
}
else if(key==27)
{
return KEY_ESC;
}
return key;
}
CLabel.h [文本类 头文件]
#ifndef CLABEL_H
#define CLABEL_H
//属性 x y w h content
//行为: show()
class CLabel
{
public:
//默认构造
CLabel();
//析构函数
~CLabel();
//带参构造
CLabel(int x,int y,int w,int h,char pcontent[20]);
//拷贝构造
//CLabel(CLabel &lab);//因为数据成员中有指针 自己定义拷贝构造
void show();
private:
int startX;
int startY;
int width;
int height;
char content[20]; //内容 数组 系统开空间
};
#endif
CLabel.cpp [文本类 源文件]
#include"CLabel.h"
#include<iostream>
using namespace std;
#include"CTools.h"
CLabel::CLabel()
{
// cout<<"默认构造函数"<<endl;
}
CLabel::CLabel(int x,int y,int w,int h,char pcontent[20])
{
// cout<<"带参数的构造函数"<<endl;
startX = x;
startY = y;
width = w;
height = h;
strcpy(content,pcontent);
}
/*
//自己写拷贝构造函数
CLabel::CLabel(CLabel &lab)
{
cout<<"拷贝构造函数"<<endl;
startX = lab.startX;
startY = lab.startY;
width = lab.width;
height = lab.height;
strcpy(content,lab.content);
}
*/
//类成员函数的实现格式:返回值类型 类名::成员函数名(参数列表){函数体}
void CLabel::show()
{
gotoxy(startX,startY);
cout<<content<<endl;
}
CLabel::~CLabel()
{
}
CEdit.h [编辑框类 头文件]
#ifndef CEDIT_H
#define CEDIT_H
class CEdit
{
public :
CEdit();
CEdit(int x,int y,int w ,int h,char *pcontent,int maxLen,int inputType,int ips);
void show();
private:
int startX;
int startY;
int width;
int height;
char content[20]; //内容
int inputType; //输入的类型
int maxLen; //最大长度
int ips; //明文密文
};
#endif
CEdit.cpp [编辑框类 源文件]
#include"CEdit.h"
#include"CTools.h"
#include<iostream>
using namespace std;
CEdit::CEdit()
{
this->startX = 0;
this->startY = 0;
this->width = 0;
this->height = 0;
memset(this->content,'\0',sizeof(this->content));
}
CEdit::CEdit(int x,int y,int w,int h,char *pcontent,
int maxLen,int inputType,int ips )
{
this->startX = x;
this->startY = y;
this->width = w;
this->height = h;
this->maxLen = maxLen;
this->inputType = inputType;
this->ips = ips;
memset(this->content,0,sizeof(this->content));
if(pcontent!=NULL)
{
strcpy(this->content,pcontent);
}
}
void CEdit::show()
{
paintWindow(startX-1,startY-1,width,height);
gotoxy(startX,startY);
cout<<content<<endl;
}
CButton.h [按钮类 头文件]
#ifndef CBUTTON_H
#define CBUTTON_H
//属性 x y w h content
//行为: show()
class CButton
{
public:
//构造函数
CButton();
//拷贝函数
// CButton(CButton &button);
//带参数的构造
CButton(int x,int y,int w,int h,char pcontent[]);
//析构函数
~CButton();
void show();
private:
int startX;
int startY;
int width;
int height;
char content[20]; //内容
};
#endif
CButton.cpp [按钮类 源文件]
#include"CButton.h"
#include<iostream>
using namespace std;
#include"CTools.h"
CButton::CButton()
{
// cout<<"默认构造函数"<<endl;
}
CButton::CButton(int x,int y,int w,int h,char pcontent[])
{
// cout<<"带参数的构造函数"<<endl;
this->startX = x;
this->startY = y;
this->width = w;
this->height = h;
strcpy(this->content,pcontent);
}
CButton::~CButton()
{
// cout<<"析构函数"<<endl;
}
void CButton::show()
{
paintWindow(startX,startY,width,height);
gotoxy(startX+(width*2-strlen(content))/2-1,startY+1);
cout<<content<<endl;
}