就是用easyx输入字符串到界面上
时间: 2023-12-14 10:02:26 浏览: 68
您可以使用EasyX库中的`inputbox`函数来输入字符串并将其显示在界面上。
以下是一个简单的示例代码:
```C++
#include <graphics.h>
int main()
{
initgraph(640, 480);
char str[100];
inputbox_getline("输入字符串", "请输入字符串:", str, 100);
outtextxy(100, 100, str);
getch();
closegraph();
return 0;
}
```
这个程序会在界面上弹出一个对话框,要求用户输入字符串。输入完成后,程序会将输入的字符串显示在界面上。
相关问题
用easyx做用户登录界面
好的,下面是一个简单的示例代码,演示如何使用easyx库实现一个用户登录界面。
```c++
#include <graphics.h>
#include <conio.h>
#include <string.h>
int main()
{
// 初始化窗口
initgraph(400, 300);
// 绘制登录界面
setbkcolor(WHITE); // 设置背景色为白色
cleardevice(); // 清屏
// 绘制标题
settextstyle(30, 0, _T("微软雅黑"));
settextcolor(BLACK);
outtextxy(100, 50, _T("用户登录"));
// 绘制用户名和密码输入框
settextstyle(20, 0, _T("微软雅黑"));
settextcolor(BLACK);
outtextxy(50, 100, _T("用户名:"));
rectangle(150, 100, 350, 130);
outtextxy(50, 150, _T("密 码:"));
rectangle(150, 150, 350, 180);
// 绘制登录按钮
setfillcolor(BLUE);
settextcolor(WHITE);
solidrectangle(150, 200, 250, 230);
outtextxy(180, 206, _T("登录"));
// 循环等待用户输入
char username[20] = {0};
char password[20] = {0};
int x, y;
while (true)
{
// 检测鼠标事件
if (MouseHit())
{
MOUSEMSG msg = GetMouseMsg();
x = msg.x;
y = msg.y;
// 检测是否点击了登录按钮
if (x >= 150 && x <= 250 && y >= 200 && y <= 230)
{
// 获取输入的用户名和密码
getInputText(150, 100, 200, username);
getInputText(150, 150, 200, password);
// 检测用户名和密码是否正确
if (strcmp(username, "admin") == 0 && strcmp(password, "123456") == 0)
{
// 登录成功,退出循环
break;
}
else
{
// 登录失败,弹出提示窗口
MessageBox(GetHWnd(), _T("用户名或密码错误!"), _T("提示"), MB_OK | MB_ICONEXCLAMATION);
}
}
}
}
// 关闭窗口
closegraph();
return 0;
}
// 获取输入框中的文本内容
void getInputText(int x, int y, int width, char* text)
{
int ch;
int i = 0;
settextstyle(20, 0, _T("微软雅黑"));
settextcolor(BLACK);
rectangle(x, y, x + width, y + 30);
while (true)
{
ch = getch();
if (ch == 13) // 回车键,结束输入
{
break;
}
else if (ch == 8) // 退格键,删除上一个字符
{
if (i > 0)
{
i--;
outtextxy(x + 5 + i * 15, y + 5, _T(" "));
}
}
else if (ch >= 32 && ch <= 126) // 可见字符,追加到输入框中
{
if (i < width / 15 - 1)
{
text[i] = ch;
i++;
outtextxy(x + 5 + (i - 1) * 15, y + 5, _T("*"));
}
}
}
text[i] = '\0'; // 添加字符串结束符
}
```
这个示例代码实现了一个简单的用户登录界面,包括用户名和密码输入框以及登录按钮。当用户点击登录按钮时,程序会获取输入框中的文本内容,并检测用户名和密码是否正确。如果正确则登录成功,否则弹出提示窗口。
注意:这个示例代码仅供参考,实际上线应用中需要进行更加严格的用户验证和安全措施。
easyx图形库简易登录界面
好的,我可以帮您设计一个基于EasyX图形库的简易登录界面。以下是一个示例代码:
```cpp
#include <graphics.h>
#include <conio.h>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
// 初始化图形界面
initgraph(400, 300);
// 绘制登录框
setfillcolor(WHITE);
setlinecolor(BLACK);
roundrect(100, 50, 300, 200, 10, 10);
// 绘制标题
settextcolor(BLACK);
settextstyle(25, 0, _T("微软雅黑"));
outtextxy(150, 75, _T("欢迎使用"));
// 绘制用户名和密码输入框
settextstyle(15, 0, _T("微软雅黑"));
outtextxy(120, 110, _T("用户名:"));
outtextxy(120, 150, _T("密 码:"));
setlinecolor(BLACK);
rectangle(200, 100, 280, 130);
rectangle(200, 140, 280, 170);
// 绘制登录按钮
setfillcolor(RGB(0, 162, 232));
setlinecolor(BLACK);
roundrect(150, 190, 250, 230, 5, 5);
settextcolor(WHITE);
settextstyle(20, 0, _T("微软雅黑"));
outtextxy(180, 200, _T("登录"));
// 循环等待用户输入
char username[20] = "";
char password[20] = "";
int cursorX = 205;
int cursorY = 105;
bool isUsernameInputting = true;
while (true)
{
if (_kbhit())
{
char ch = _getch();
if (ch == '\r') // 用户按下回车键,尝试登录
{
// 进行用户名和密码的验证,这里只是简单的比较字符串
if (strcmp(username, "admin") == 0 && strcmp(password, "123456") == 0)
{
settextcolor(GREEN);
outtextxy(160, 250, _T("登录成功"));
Sleep(2000); // 等待2秒后退出程序
break;
}
else
{
settextcolor(RED);
outtextxy(160, 250, _T("用户名或密码错误"));
}
}
else if (ch == '\b') // 用户按下退格键,删除输入框中的一个字符
{
if (isUsernameInputting)
{
if (strlen(username) > 0)
{
username[strlen(username) - 1] = '\0';
cursorX -= 8;
}
}
else
{
if (strlen(password) > 0)
{
password[strlen(password) - 1] = '\0';
cursorX -= 8;
}
}
}
else if (ch == '\t') // 用户按下Tab键,切换输入框
{
isUsernameInputting = !isUsernameInputting;
if (isUsernameInputting)
{
cursorX = 205;
cursorY = 105;
}
else
{
cursorX = 205;
cursorY = 145;
}
}
else if (strlen(username) < 18 || strlen(password) < 18) // 用户输入字符
{
if (isUsernameInputting)
{
strcat(username, &ch);
outtextxy(cursorX, cursorY, &ch);
cursorX += 8;
}
else
{
strcat(password, &ch);
outtextxy(cursorX, cursorY, _T("*"));
cursorX += 8;
}
}
}
}
// 关闭图形界面
closegraph();
return 0;
}
```
运行上述代码,即可看到一个简易的登录界面。用户可以在用户名和密码输入框中输入内容,并通过按下回车键进行登录验证。如果用户名或密码错误,会在界面下方提示错误信息。如果登录成功,会在界面下方提示登录成功,并等待2秒后退出程序。
需要注意的是,以上代码只是一个简单的示例,真实的登录界面可能需要进行更复杂的验证,比如连接到数据库进行账号验证等等。