#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <vector>
#include <string>
#include <random>
#include <ctime>
#include <windows.h>
#include <cmath>
// 颜色定义
#define COLOR_PINK RGB(255, 204, 229)
#define COLOR_LIGHT_BLUE RGB(204, 229, 255)
#define COLOR_LIGHT_GREEN RGB(204, 255, 229)
#define COLOR_LIGHT_YELLOW RGB(255, 255, 204)
#define COLOR_LIGHT_PURPLE RGB(229, 204, 255)
#define COLOR_LIGHT_ORANGE RGB(255, 229, 204)
#define COLOR_SOFT_RED RGB(255, 179, 186)
#define COLOR_SOFT_BLUE RGB(179, 217, 255)
#define COLOR_SOFT_GREEN RGB(179, 255, 204)
#define COLOR_SOFT_YELLOW RGB(255, 255, 179)
#define COLOR_SOFT_PURPLE RGB(221, 179, 255)
#define COLOR_SOFT_ORANGE RGB(255, 221, 179)
#define COLOR_SOFT_CYAN RGB(179, 255, 255)
#define COLOR_SOFT_MAGENTA RGB(255, 179, 238)
#define COLOR_PALE_PINK RGB(255, 225, 235)
#define COLOR_PALE_BLUE RGB(225, 235, 255)
#define COLOR_PALE_GREEN RGB(225, 255, 235)
#define COLOR_PALE_YELLOW RGB(255, 255, 225)
#define COLOR_PALE_PURPLE RGB(235, 225, 255)
#define COLOR_PALE_ORANGE RGB(255, 235, 225)
#define COLOR_RED_HEART RGB(255, 0, 0) // 爱心符号颜色
#define COLOR_CLOSE_BTN RGB(204, 0, 0) // 关闭按钮颜色
#define COLOR_TEXT_TITLE RGB(0, 0, 0) // 标题文字颜色(黑色)
// 消息结构体
struct Message {
std::wstring text;
int x;
int y;
int width; // 窗口宽度
int height; // 窗口高度
int titleBarHeight; // 标题栏高度
COLORREF color; // 窗口主体颜色(标题栏颜色)
COLORREF contentColor; // 内容区域颜色
bool active;
int alpha;
int lifetime;
bool animating; // 是否正在动画中
int animFrame; // 动画帧数
};
// 祝福语列表
std::vector& lt; std::wstring& gt; loveMessages = {
L"我爱你",
L"你是我的唯一",
L"每天都想你",
L"你让我很开心",
L"遇见你是我的幸运",
L"你是我生命中的阳光",
L"我想和你一起变老",
L"你是最棒的",
L"我喜欢你的笑容",
L"有你真好",
L"你是我的梦",
L"我会永远爱你",
L"你让我的世界更美好",
L"我想和你分享一切",
L"你是我的幸福",
L"我珍惜每一刻和你在一起",
L"你是我的灵感",
L"我为你感到骄傲",
L"你是我的安全感",
L"我爱你的一切",
L"你偷走了我的心",
L"我的心永远属于你",
L"你是我见过最美的风景",
L"和你在一起的时光最美好",
L"你让我相信真爱",
L"我无法停止想你",
L"你是我的命中注定",
L"我的生活因你而精彩",
L"你是我每天的动力",
L"我想和你创造更多回忆",
L"你是我的灵魂伴侣",
L"我愿意为你做任何事",
L"你的爱给了我力量",
L"你是我人生中最美好的礼物",
L"我会永远珍惜你",
L"你让我成为更好的人",
L"我的心为你跳动",
L"你是我的整个世界",
L"我对你的爱永无止境",
L"你是我生命中的奇迹",
L"和你在一起我感到完整",
L"你是我每天的快乐",
L"我想和你一起看遍世界",
L"你的笑容照亮了我的一天",
L"我永远不会让你失望",
L"你是我最想守护的人",
L"我的爱只为你存在",
L"你是我生命中最美好的意外",
L"我想和你一起成长",
L"你让我感到无比幸福"
};
// 标题栏颜色列表
std::vector& lt; COLORREF& gt; titleColors = {
COLOR_PINK,
COLOR_LIGHT_BLUE,
COLOR_LIGHT_GREEN,
COLOR_LIGHT_YELLOW,
COLOR_LIGHT_PURPLE,
COLOR_LIGHT_ORANGE,
COLOR_SOFT_RED,
COLOR_SOFT_BLUE,
COLOR_SOFT_GREEN,
COLOR_SOFT_YELLOW,
COLOR_SOFT_PURPLE,
COLOR_SOFT_ORANGE,
COLOR_SOFT_CYAN,
COLOR_SOFT_MAGENTA
};
// 内容区域颜色列表
std::vector& lt; COLORREF& gt; contentColors = {
COLOR_PALE_PINK,
COLOR_PALE_BLUE,
COLOR_PALE_GREEN,
COLOR_PALE_YELLOW,
COLOR_PALE_PURPLE,
COLOR_PALE_ORANGE
};
// 绘制爱心符号
void drawHeartSymbol(int x, int y, int size, COLORREF color) {
settextstyle(size, 0, _T("宋体"));
settextcolor(color);
setbkmode(TRANSPARENT);
std::wstring heart = L"♥";
int symbolWidth = textwidth(heart.c_str());
int symbolHeight = textheight(heart.c_str());
int drawX = x - symbolWidth / 2;
int drawY = y - symbolHeight / 2;
outtextxy(drawX, drawY, heart.c_str());
}
// 绘制关闭按钮
void drawCloseBtn(int x, int y, int size, COLORREF color) {
setfillcolor(color);
solidrectangle(x, y, x + size, y + size);
setlinecolor(WHITE);
setlinestyle(PS_SOLID, 2);
line(x + size / 4, y + size / 4, x + size * 3 / 4, y + size * 3 / 4);
line(x + size * 3 / 4, y + size / 4, x + size / 4, y + size * 3 / 4);
}
// 绘制消息窗口
void drawMessageWindow(Message& msg) {
if (!msg.active) return;
int titleBarH = msg.titleBarHeight;
int contentY = msg.y + titleBarH;
int contentH = msg.height - titleBarH;
// 绘制标题栏
setfillcolor(msg.color);
solidrectangle(msg.x, msg.y, msg.x + msg.width, msg.y + titleBarH);
// 绘制爱心符号
drawHeartSymbol(msg.x + 20, msg.y + titleBarH / 2, 18, COLOR_RED_HEART);
// 绘制标题文字
settextstyle(20, 0, _T("宋体"));
settextcolor(COLOR_TEXT_TITLE);
int textX = msg.x + msg.width / 2 - textwidth(_T("爱意传递")) / 2;
int textY = msg.y + (titleBarH - 20) / 2;
outtextxy(textX, textY, _T("爱意传递"));
// 绘制关闭按钮
drawCloseBtn(msg.x + msg.width - 30, msg.y + 5, 20, COLOR_CLOSE_BTN);
// 绘制内容区域
setfillcolor(msg.contentColor);
solidrectangle(msg.x, contentY, msg.x + msg.width, msg.y + msg.height);
setlinecolor(RGB(GetRValue(msg.color) - 30, GetGValue(msg.color) - 30, GetBValue(msg.color) - 30));
setlinestyle(PS_SOLID, 2);
rectangle(msg.x, msg.y, msg.x + msg.width, msg.y + msg.height);
// 绘制居中文本
settextstyle(24, 0, _T("宋体"));
settextcolor(COLOR_TEXT_TITLE);
int textWidth = textwidth(msg.text.c_str());
int textHeight = textheight(msg.text.c_str());
int textXContent = msg.x + (msg.width - textWidth) / 2;
int textYContent = contentY + (contentH - textHeight) / 2;
outtextxy(textXContent, textYContent, msg.text.c_str());
}
// 计算文本宽度
int getTextWidth(const std::wstring& text, int fontHeight) {
settextstyle(fontHeight, 0, _T("宋体"));
return textwidth(text.c_str());
}
// 计算文本高度
int getTextHeight(const std::wstring& text, int fontHeight) {
settextstyle(fontHeight, 0, _T("宋体"));
return textheight(text.c_str());
}
// 获取随机数
int getRandom(int min, int max) {
return min + rand() % (max - min + 1);
}
// 获取随机浮点数
float getRandomFloat(float min, float max) {
return min + static_cast & lt; float& gt; (rand()) / RAND_MAX * (max - min);
}
int main() {
srand(static_cast & lt; unsigned int& gt; (time(0)));
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
initgraph(screenWidth, screenHeight);
setbkcolor(BLACK);
cleardevice();
std::vector& lt; Message& gt; messages;
int fontHeight = 24;
int titleBarHeight = 30;
int horizontalPadding = 40;
int verticalPadding = 40;
int messageCounter = 0;
int messageInterval = 3;
int frameCount = 0;
settextstyle(fontHeight, 0, _T("宋体"));
setbkmode(TRANSPARENT);
BeginBatchDraw();
while (true) {
// 处理键盘输入
if (_kbhit()) {
char key = _getch();
if (key == 27) break; // ESC键退出
}
// 生成新消息
if (frameCount % messageInterval == 0) {
Message msg;
msg.text = loveMessages[messageCounter % loveMessages.size()];
messageCounter++;
// 随机选择标题栏颜色
int titleColorIndex = rand() % titleColors.size();
msg.color = titleColors[titleColorIndex];
// 随机选择内容区域颜色
int contentColorIndex = rand() % contentColors.size();
msg.contentColor = contentColors[contentColorIndex];
// 根据文本长度计算窗口大小
int textWidth = getTextWidth(msg.text, fontHeight);
int textHeight = getTextHeight(msg.text, fontHeight);
msg.width = textWidth + horizontalPadding;
msg.height = textHeight + verticalPadding + titleBarHeight;
msg.titleBarHeight = titleBarHeight;
// 确保窗口有最小尺寸
int minWidth = 100;
int minHeight = 40;
if (msg.width& lt; minWidth) msg.width = minWidth;
if (msg.height& lt; minHeight) msg.height = minHeight;
// 计算随机位置(确保窗口完全显示在屏幕内)
int margin = 20;
msg.x = getRandom(margin, screenWidth - msg.width - margin);
msg.y = getRandom(margin, screenHeight - msg.height - margin);
// 初始化消息属性
msg.active = true;
msg.alpha = 255;
msg.lifetime = 0;
msg.animating = true;
msg.animFrame = 0;
messages.push_back(msg);
}
// 清除屏幕
cleardevice();
// 更新和绘制消息
for (auto& msg : messages) {
if (msg.active) {
// 更新动画
if (msg.animating) {
msg.animFrame++;
float progress = static_cast & lt; float& gt; (msg.animFrame) / 10.0f;
progress = sin(progress * 3.14159f / 2.0f); // 缓动效果
// 计算当前尺寸(基于目标尺寸)
int currentWidth = static_cast & lt; int& gt; (msg.width * progress);
int currentHeight = static_cast & lt; int& gt; (msg.height * progress);
// 绘制动画中的窗口
Message animMsg = msg;
animMsg.width = currentWidth;
animMsg.height = currentHeight;
drawMessageWindow(animMsg);
// 动画结束
if (msg.animFrame& gt; = 10) {
msg.animating = false;
}
}
else {
drawMessageWindow(msg);
}
msg.lifetime++;
}
}
frameCount++;
FlushBatchDraw();
Sleep(10);
}
EndBatchDraw();
closegraph();
return 0;
}
这串代码若要在c语言上能运行,需要修改什么地方