可以写一个基于easyx库的c++情绪日记代码:其可以实现先用鼠标选择喜,怒,哀,乐四个标签之一,再点击文本框输入文本,文本显示在文本框内,然后回车保存,点击历史记录图标可以分条查看以前写过的文本
时间: 2023-06-18 22:02:37 浏览: 169
以下是一个基于easyx库的c++情绪日记代码。代码中使用了一个vector来保存历史记录,每次保存时将情绪、文本、日期等信息打包成一个结构体存入vector中,点击历史记录按钮时按照日期逆序输出。
```cpp
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <ctime>
#include <vector>
#include <string>
using namespace std;
// 情绪枚举
enum Mood { HAPPY, ANGRY, SAD, JOYFUL };
// 历史记录结构体
struct Record {
Mood mood; // 情绪
string text; // 文本
time_t time; // 时间
char date[20]; // 日期字符串
};
// 窗口大小
const int WIDTH = 600, HEIGHT = 400;
// 文本框位置和大小
const int TEXTBOX_X = 30, TEXTBOX_Y = 50, TEXTBOX_WIDTH = 540, TEXTBOX_HEIGHT = 200;
// 标签位置和大小
const int LABEL_X = 30, LABEL_Y = 270, LABEL_WIDTH = 120, LABEL_HEIGHT = 30;
const int LABEL_MARGIN = 20; // 标签之间的间隔
// 按钮位置和大小
const int SAVE_BUTTON_X = 30, SAVE_BUTTON_Y = 320, SAVE_BUTTON_WIDTH = 80, SAVE_BUTTON_HEIGHT = 30;
const int HISTORY_BUTTON_X = 130, HISTORY_BUTTON_Y = 320, HISTORY_BUTTON_WIDTH = 80, HISTORY_BUTTON_HEIGHT = 30;
// 鼠标点击区域
struct ClickArea {
int x, y, width, height;
};
// 标签的鼠标点击区域
vector<ClickArea> labelClickAreas;
// 按钮的鼠标点击区域
ClickArea saveClickArea, historyClickArea;
// 历史记录
vector<Record> history;
// 获取当前时间的字符串表示
char* getCurrentDate() {
time_t now = time(nullptr);
strftime(static_cast<char*>(malloc(20)), 20, "%Y-%m-%d %H:%M:%S", localtime(&now));
}
// 绘制文本框
void drawTextBox() {
setfillcolor(WHITE);
setlinecolor(BLACK);
roundrect(TEXTBOX_X, TEXTBOX_Y, TEXTBOX_X + TEXTBOX_WIDTH, TEXTBOX_Y + TEXTBOX_HEIGHT, 10, 10);
}
// 绘制标签
void drawLabels() {
settextcolor(BLACK);
setbkmode(TRANSPARENT);
settextstyle(20, 0, _T("微软雅黑"));
const char* labels[] = { "喜", "怒", "哀", "乐" };
const COLORREF colors[] = { RGB(255, 192, 203), RGB(255, 0, 0), RGB(192, 192, 192), RGB(255, 255, 0) };
for (int i = 0; i < 4; i++) {
setfillcolor(colors[i]);
setlinecolor(BLACK);
roundrect(LABEL_X + i * (LABEL_WIDTH + LABEL_MARGIN), LABEL_Y, LABEL_X + i * (LABEL_WIDTH + LABEL_MARGIN) + LABEL_WIDTH, LABEL_Y + LABEL_HEIGHT, 10, 10);
ClickArea ca = { LABEL_X + i * (LABEL_WIDTH + LABEL_MARGIN), LABEL_Y, LABEL_WIDTH, LABEL_HEIGHT };
labelClickAreas.push_back(ca);
outtextxy(LABEL_X + i * (LABEL_WIDTH + LABEL_MARGIN) + 30, LABEL_Y + 5, labels[i]);
}
}
// 绘制按钮
void drawButtons() {
settextcolor(BLACK);
setbkmode(TRANSPARENT);
settextstyle(20, 0, _T("微软雅黑"));
setfillcolor(RGB(135, 206, 250));
setlinecolor(BLACK);
roundrect(SAVE_BUTTON_X, SAVE_BUTTON_Y, SAVE_BUTTON_X + SAVE_BUTTON_WIDTH, SAVE_BUTTON_Y + SAVE_BUTTON_HEIGHT, 10, 10);
saveClickArea = { SAVE_BUTTON_X, SAVE_BUTTON_Y, SAVE_BUTTON_WIDTH, SAVE_BUTTON_HEIGHT };
outtextxy(SAVE_BUTTON_X + 20, SAVE_BUTTON_Y + 5, _T("保存"));
setfillcolor(RGB(135, 206, 250));
setlinecolor(BLACK);
roundrect(HISTORY_BUTTON_X, HISTORY_BUTTON_Y, HISTORY_BUTTON_X + HISTORY_BUTTON_WIDTH, HISTORY_BUTTON_Y + HISTORY_BUTTON_HEIGHT, 10, 10);
historyClickArea = { HISTORY_BUTTON_X, HISTORY_BUTTON_Y, HISTORY_BUTTON_WIDTH, HISTORY_BUTTON_HEIGHT };
outtextxy(HISTORY_BUTTON_X + 10, HISTORY_BUTTON_Y + 5, _T("历史记录"));
}
// 绘制历史记录
void drawHistory() {
settextcolor(BLACK);
setbkmode(TRANSPARENT);
settextstyle(16, 0, _T("微软雅黑"));
int y = 50;
for (auto it = history.rbegin(); it != history.rend(); it++) {
char buf[1000];
sprintf(buf, "%s [%c] %s", it->date, it->mood == HAPPY ? 'H' : it->mood == ANGRY ? 'A' : it->mood == SAD ? 'S' : 'J', it->text.c_str());
outtextxy(30, y, buf);
y += 20;
}
}
// 根据鼠标坐标判断是否点击了标签
int getClickedLabel(int x, int y) {
for (int i = 0; i < labelClickAreas.size(); i++) {
if (x >= labelClickAreas[i].x && x <= labelClickAreas[i].x + labelClickAreas[i].width
&& y >= labelClickAreas[i].y && y <= labelClickAreas[i].y + labelClickAreas[i].height) {
return i;
}
}
return -1;
}
// 判断是否点击了保存按钮
bool isClickedSaveButton(int x, int y) {
return x >= saveClickArea.x && x <= saveClickArea.x + saveClickArea.width
&& y >= saveClickArea.y && y <= saveClickArea.y + saveClickArea.height;
}
// 判断是否点击了历史记录按钮
bool isClickedHistoryButton(int x, int y) {
return x >= historyClickArea.x && x <= historyClickArea.x + historyClickArea.width
&& y >= historyClickArea.y && y <= historyClickArea.y + historyClickArea.height;
}
// 处理鼠标事件
void handleMouseEvent(MOUSEMSG msg) {
if (msg.uMsg == WM_LBUTTONDOWN) {
int labelIndex = getClickedLabel(msg.x, msg.y);
if (labelIndex != -1) {
// 点击了标签
setfillcolor(YELLOW);
setlinecolor(BLACK);
solidroundrect(LABEL_X + labelIndex * (LABEL_WIDTH + LABEL_MARGIN), LABEL_Y, LABEL_X + labelIndex * (LABEL_WIDTH + LABEL_MARGIN) + LABEL_WIDTH, LABEL_Y + LABEL_HEIGHT, 10, 10);
} else if (isClickedSaveButton(msg.x, msg.y)) {
// 点击了保存按钮
Record record;
record.mood = static_cast<Mood>(labelIndex);
char* date = getCurrentDate();
strcpy(record.date, date);
free(date);
record.text = getinput();
history.push_back(record);
cleardevice();
drawTextBox();
drawLabels();
drawButtons();
} else if (isClickedHistoryButton(msg.x, msg.y)) {
// 点击了历史记录按钮
cleardevice();
drawHistory();
while (!kbhit());
cleardevice();
drawTextBox();
drawLabels();
drawButtons();
}
}
}
int main() {
initgraph(WIDTH, HEIGHT);
setbkcolor(WHITE);
cleardevice();
drawTextBox();
drawLabels();
drawButtons();
settextcolor(BLACK);
setbkmode(TRANSPARENT);
settextstyle(20, 0, _T("微软雅黑"));
outtextxy(30, 10, _T("请选择情绪标签,输入文本,按回车保存"));
settextstyle(16, 0, _T("微软雅黑"));
while (true) {
MOUSEMSG msg = GetMouseMsg();
handleMouseEvent(msg);
}
closegraph();
return 0;
}
```
阅读全文