c语言easyx登录系统代码
时间: 2024-01-18 10:04:40 浏览: 32
由于EasyX库主要用于Windows平台的图形界面开发,而登录系统通常需要与数据库或文件进行交互,因此需要借助其他库或API完成登录功能。
以下是一个使用EasyX库实现的简单的登录界面:
```
#include <graphics.h>
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
initwindow(400, 300, "Login");
setbkcolor(WHITE);
// 用户名和密码输入框
char username[20] = {0};
char password[20] = {0};
outtextxy(100, 100, "Username:");
outtextxy(100, 140, "Password:");
setcolor(BLACK);
rectangle(200, 90, 380, 120);
rectangle(200, 130, 380, 160);
// 登录按钮
setfillcolor(BLUE);
settextstyle(20, 0, "宋体");
fillellipse(290, 220, 60, 30);
outtextxy(270, 205, "Login");
// 循环读取用户输入
while (true)
{
// 判断是否点击了登录按钮
if (MouseHit())
{
MOUSEMSG msg = GetMouseMsg();
int x = msg.x;
int y = msg.y;
int button = msg.uMsg;
if (button == WM_LBUTTONDOWN && (x - 290) * (x - 290) + (y - 220) * (y - 220) <= 60 * 30)
{
if (strcmp(username, "admin") == 0 && strcmp(password, "123456") == 0)
{
// 登录成功
cleardevice();
settextstyle(30, 0, "宋体");
outtextxy(100, 100, "Welcome, admin!");
getch();
break;
}
else
{
// 登录失败
clearrectangle(200, 90, 380, 160);
outtextxy(200, 200, "Invalid username or password!");
memset(username, 0, sizeof(username));
memset(password, 0, sizeof(password));
}
}
}
// 读取键盘输入
if (kbhit())
{
char ch = getch();
if (ch == '\r')
{
// 回车键相当于点击登录按钮
continue;
}
else if (ch == '\b')
{
// 退格键删除字符
if (password[strlen(password) - 1] != 0)
{
password[strlen(password) - 1] = 0;
}
else if (username[strlen(username) - 1] != 0)
{
username[strlen(username) - 1] = 0;
}
}
else
{
// 输入字符
if (strlen(username) < 20)
{
username[strlen(username)] = ch;
}
else if (strlen(password) < 20)
{
password[strlen(password)] = ch;
}
}
// 更新输入框内容
clearrectangle(200, 90, 380, 160);
outtextxy(200, 100, username);
outtextxy(200, 140, password);
setcolor(BLACK);
rectangle(200, 90, 380, 120);
rectangle(200, 130, 380, 160);
}
}
closegraph();
return 0;
}
```
这个程序实现了一个简单的登录界面,用户可以输入用户名和密码,点击登录按钮后判断用户名和密码是否正确,如果正确则显示欢迎界面,否则提示用户名或密码错误。程序使用了EasyX库提供的图形界面绘制函数和鼠标键盘输入函数。但是由于没有涉及到数据库或文件等高级操作,因此这个登录系统的实用性比较有限,仅供参考。