c++动态红色爱心代码加名字
时间: 2025-04-03 08:19:10 浏览: 43
### C++ 实现动态红色爱心图案并添加名字
以下是基于 `GDI+` 的 C++ 代码示例,展示如何绘制一个动态的红色心形图案,并在其旁边显示自定义的名字。
#### 动态红色爱心图案绘制逻辑
为了实现动态效果,可以利用定时器或其他机制不断更新窗口的内容。通过调整绘图参数(如位置、大小),可以让心形看起来像是在移动或变化[^1]。
```cpp
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace GDIPlus;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
const char* className = "HeartWindowClass";
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = className;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
className,
"Dynamic Red Heart",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
nullptr, nullptr, hInstance, nullptr
);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
MSG msg = {};
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GdiplusShutdown(gdiplusToken);
return static_cast<int>(msg.wParam);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
static Graphics graphics(hwnd);
static Pen pen(Color(255, 255, 0, 0), 3.0f); // 红色笔
static SolidBrush brush(Color(255, 255, 0, 0)); // 填充刷子
static Font font(L"Arial", 20, FontStyleBold);
static PointF textPoint(400.0f, 300.0f);
static float heartScale = 1.0f; // 心形缩放比例
static bool isScalingUp = true;
switch (msg) {
case WM_PAINT: {
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
// 计算心形路径
GraphicsPath path;
RectF rect(200 * heartScale, 200 * heartScale, 200 * heartScale, 200 * heartScale);
path.AddArc(rect.X, rect.Y, rect.Width / 2, rect.Height, 270, 180);
path.AddArc(rect.X + rect.Width / 2, rect.Y, rect.Width / 2, rect.Height, 90, 180);
path.AddLine(PointF(rect.X + rect.Width / 2, rect.Y + rect.Height),
PointF(rect.X + rect.Width / 2, rect.Y + rect.Height * 2));
// 绘制心形
graphics.FillPath(&brush, &path);
graphics.DrawPath(&pen, &path);
// 显示名字
WCHAR name[] = L"Your Name"; // 自定义名字
graphics.DrawString(name, -1, &font, textPoint, &SolidBrush(Color::Black));
EndPaint(hwnd, &ps);
break;
}
case WM_TIMER: {
if (isScalingUp) {
heartScale += 0.01f;
if (heartScale >= 1.5f) {
isScalingUp = false;
}
} else {
heartScale -= 0.01f;
if (heartScale <= 0.5f) {
isScalingUp = true;
}
}
InvalidateRect(hwnd, nullptr, TRUE); // 刷新窗口
break;
}
case WM_CREATE:
SetTimer(hwnd, 1, 50, nullptr); // 设置计时器
break;
case WM_DESTROY:
KillTimer(hwnd, 1);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
```
此代码实现了以下功能:
- 使用 `GraphicsPath` 定义了一个心形路径。
- 使用 `FillPath` 方法填充心形区域,并用 `DrawPath` 方法绘制边框。
- 添加了动态缩放效果,使心形逐渐放大和缩小。
- 在指定位置显示自定义名字,支持 Unicode 字符串。
---
### 关键技术点说明
1. **GDI+ 图形库**
GDI+ 提供了一组强大的 API 来处理二维矢量图形、图像和字体渲染。上述代码中使用了 `Graphics`, `Pen`, `Brush`, 和 `Font` 类来完成绘图任务。
2. **动态效果**
通过设置 Windows 消息循环中的计时器 (`WM_TIMER`),定期改变心形的比例因子 (`heartScale`) 并调用 `InvalidateRect` 更新屏幕内容。
3. **Unicode 支持**
名字部分采用了宽字符数组 (`WCHAR`) 和 `DrawString` 函数,确保能够正确显示多语言字符串。
---
阅读全文
相关推荐

















