easyx图形库写文字
时间: 2023-09-26 22:11:30 浏览: 75
使用EasyX图形库可以很方便地在窗口中输出文字,可以通过以下代码实现:
```cpp
#include <graphics.h> // 引入 EasyX 头文件
int main()
{
initgraph(640, 480); // 创建一个640*480的窗口
settextcolor(RGB(255,0,0)); // 设置文本颜色为红色
settextstyle(20, 0, _T("宋体")); // 设置字体大小为20,字体为宋体
outtextxy(100, 100, _T("Hello, world!")); // 在(100,100)位置输出文本
getch(); // 暂停程序,等待按键
closegraph(); // 关闭图形界面
return 0;
}
```
在程序中,我们首先调用`initgraph()`函数创建一个窗口,然后通过`settextcolor()`和`settextstyle()`函数设置文本颜色和字体,最后使用`outtextxy()`函数在指定位置输出文本。最后,我们使用`getch()`函数暂停程序,等待按键,然后使用`closegraph()`函数关闭图形界面。
相关问题
easyx图形库createButton如何改变文字颜色
使用EasyX图形库的`createButton`函数创建按钮后,可以通过设置按钮上的字体颜色来改变文字的颜色。具体方法是:
1. 使用`settextcolor`函数设置字体颜色,例如可以使用以下代码将字体颜色设置为红色:
```c++
settextcolor(RED);
```
2. 在按钮绘制函数中绘制按钮上的文字时,使用`outtextxy`函数绘制文字,并将文字的颜色设置为刚才设置的字体颜色,例如:
```c++
outtextxy(x + width / 2 - textwidth(text) / 2, y + height / 2 - textheight(text) / 2, text);
```
在这里,`textwidth`和`textheight`函数用于获取文字的宽度和高度,`x`、`y`、`width`、`height`和`text`分别表示按钮的位置、大小和上面显示的文字。
通过上述方法,即可改变EasyX图形库中使用`createButton`函数创建的按钮上文字的颜色。
easyx图形库简易登录界面
好的,下面是一个简单的使用easyx图形库制作的登录界面的示例代码:
```cpp
#include <graphics.h>
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
initgraph(400, 300); // 初始化图形界面,大小为400x300
setbkcolor(WHITE); // 设置背景色为白色
cleardevice(); // 清空屏幕
// 绘制登录框
setfillcolor(GRAY);
setlinecolor(BLACK);
fillrectangle(100, 80, 300, 200);
// 绘制登录标题
settextstyle(30, 0, _T("宋体"));
settextcolor(BLACK);
outtextxy(150, 100, _T("登录"));
// 绘制用户名和密码输入框
setfillcolor(WHITE);
setlinecolor(BLACK);
fillrectangle(120, 140, 280, 170);
fillrectangle(120, 180, 280, 210);
// 绘制用户名和密码输入提示文字
settextstyle(20, 0, _T("宋体"));
settextcolor(BLACK);
outtextxy(130, 145, _T("用户名:"));
outtextxy(130, 185, _T("密码:"));
// 等待用户输入用户名和密码
string username;
string password;
char ch;
int x = 130, y = 150;
settextstyle(20, 0, _T("宋体"));
while (1)
{
ch = _getch();
if (ch == '\r') // 用户按下回车键,表示输入完成
break;
if (ch == '\b') // 用户按下退格键,删除上一个字符
{
if (x > 130)
{
x -= 20;
settextcolor(WHITE);
outtextxy(x, y, _T(" "));
x -= 20;
}
if (x <= 130 && y == 190)
{
x = 270;
y = 150;
}
if (x <= 130 && y == 150)
x = 270;
if (y == 150)
username = username.substr(0, username.length() - 1);
else if (y == 190)
password = password.substr(0, password.length() - 1);
}
else if (ch != '\t' && ch != 32) // 用户输入一个字符
{
if (y == 150)
username += ch;
else if (y == 190)
password += ch;
settextcolor(BLACK);
outtextxy(x, y, _T("*"));
x += 20;
if (x > 270 && y == 150)
{
x = 130;
y = 190;
}
if (x > 270 && y == 190)
x = 130;
}
}
// 显示用户输入的用户名和密码
settextcolor(BLACK);
outtextxy(130, 230, _T("用户名:"));
outtextxy(220, 230, username.c_str());
outtextxy(130, 260, _T("密码:"));
outtextxy(180, 260, password.c_str());
getch(); // 等待用户按下任意键关闭窗口
closegraph(); // 关闭图形界面
return 0;
}
```
这个程序会画出一个登录框和两个输入框,分别用于输入用户名和密码。用户输入的字符会被显示为“*”号,以保护用户的隐私。用户输入完成后,程序会显示输入的用户名和密码,并等待用户按下任意键关闭窗口。