用C++的Windows编写一个程序,在屏幕中出现一个圆心沿正弦曲线轨迹移动的小圆,要求每隔四分之一周期,圆的填充色和圆的周边颜色都发生变化(颜色自己选取),同时,圆的半径在四分之一周期之内由正弦曲线幅值的0.2倍至0.6倍线性增长。小圆能从左向右再从右向左不间断移动。
时间: 2023-05-13 13:06:37 浏览: 143
以下是用C的Windows编写一个程序,在屏幕中出现一个圆心沿正弦曲线轨迹移动的小圆的代码:
```c
#include <windows.h>
#include <math.h>
#define PI 3.14159265358979323846
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Sinusoidal Circle");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Sinusoidal Circle"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxClient, cyClient;
static int cxCenter, cyCenter;
static int iRadius;
static int iColorIndex;
static COLORREF crColors[] = { RGB(255, 0, 0), RGB(0, 255, 0), RGB(0, 0, 255) };
static double dPhase;
static double dAmplitude;
static double dFrequency;
static double dSpeed;
static double dTime;
switch (message)
{
case WM_CREATE:
iRadius = 0;
iColorIndex = 0;
dPhase = 0.0;
dAmplitude = 50.0;
dFrequency = 0.02;
dSpeed = 0.1;
dTime = 0.0;
SetTimer(hwnd, 1, 10, NULL);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
cxCenter = cxClient / 2;
cyCenter = cyClient / 2;
return 0;
case WM_TIMER:
dTime += dSpeed;
if (dTime > 2 * PI)
{
dTime -= 2 * PI;
}
iRadius = (int)(dAmplitude * (0.2 + 0.4 * dTime / PI));
iColorIndex = (int)(dTime / (PI / 2));
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hBrush;
HPEN hPen;
int x, y;
hdc = BeginPaint(hwnd, &ps);
hBrush = CreateSolidBrush(crColors[iColorIndex]);
hPen = CreatePen(PS_SOLID, 1, crColors[(iColorIndex + 1) % 3]);
SelectObject(hdc, hBrush);
SelectObject(hdc, hPen);
x = cxCenter + (int)(dAmplitude * sin(dFrequency * cxClient * dTime + dPhase));
y = cyCenter + (int)(dAmplitude * sin(dFrequency * cyClient * dTime));
Ellipse(hdc, x - iRadius, y - iRadius, x + iRadius, y + iRadius);
DeleteObject(hBrush);
DeleteObject(hPen);
EndPaint(hwnd, &ps);
}
return 0;
case WM_DESTROY:
KillTimer(hwnd, 1);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
```
这个程序会在屏幕中出现一个圆心沿正弦曲线轨迹移动的小圆,每隔四分之一周期,圆的填充色和圆的周边颜色都发生变化,同时,圆的半径在四分之一周期之内由正弦曲线幅值的0.2倍至0.6倍线性增长。小圆能从左向右再从右向左不间断移动。
阅读全文