#include <windows.h>
#include <cmath>
// 双缓冲绘图
HDC hdcBuffer;
HBITMAP hbmBuffer;
RECT rect;
// 颜色转换函数
COLORREF HSLtoRGB(float h, float s, float l) { /*...*/ }
// 窗口过程
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// 获取时间参数
static float t = 0.0f;
t += 0.01f;
// 初始化缓冲
hdcBuffer = CreateCompatibleDC(hdc);
GetClientRect(hwnd, &rect);
hbmBuffer = CreateCompatibleBitmap(hdc, rect.right, rect.bottom);
SelectObject(hdcBuffer, hbmBuffer);
// 绘制背景
FillRect(hdcBuffer, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH));
// 绘制爱心
const float a = 50.0f; // 基础大小
for(float θ = 0; θ < 6.283; θ += 0.001f) {
float r = a * (1 - sin(θ));
// 动态参数
float scale = 1.0f + 0.1f * sin(t * 5.0f); // 心跳效果
float phase = t * 2.0f; // 颜色相位
// 坐标变换
int x = scale * r * cos(θ) + rect.right/2;
int y = -scale * r * sin(θ) + rect.bottom/2;
// 设置彩虹颜色
SetPixel(hdcBuffer, x, y, HSLtoRGB(fmod(θ*30 + phase, 360), 1.0f, 0.5f));
}
// 交换缓冲
BitBlt(hdc, 0, 0, rect.right, rect.bottom, hdcBuffer, 0, 0, SRCCOPY);
// 清理资源
DeleteDC(hdcBuffer);
DeleteObject(hbmBuffer);
EndPaint(hwnd, &ps);
InvalidateRect(hwnd, NULL, FALSE); // 持续重绘
break;
}
//...其他消息处理
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
// 主函数
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nCmdShow) {
//...窗口创建代码
}
此代码报错
最新发布