给我写一个在VC6.0++画驻波的程序
时间: 2023-12-05 16:02:59 浏览: 139
好的,下面是一个简单的VC6.0++程序,可以画出驻波图形。
```c++
#include <windows.h>
#include <math.h>
#define PI 3.14159265358979323846
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc;
HWND hWnd;
MSG msg;
memset(&wc, 0, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = TEXT("MyClass");
RegisterClass(&wc);
hWnd = CreateWindow(TEXT("MyClass"), TEXT("驻波图形"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxClient, cyClient;
static TCHAR szText[] = TEXT("驻波图形");
static HPEN hPen;
static HDC hdc;
static double f = 0;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hWnd);
hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
SelectObject(hdc, hPen);
break;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd, &ps);
SetBkMode(hdc, TRANSPARENT);
int x1, x2, y1, y2;
//绘制坐标轴
MoveToEx(hdc, 10, cyClient / 2, NULL);
LineTo(hdc, cxClient - 10, cyClient / 2);
MoveToEx(hdc, cxClient / 2, 10, NULL);
LineTo(hdc, cxClient / 2, cyClient - 10);
//绘制驻波图形
for (int i = -cxClient / 2; i <= cxClient / 2; i++)
{
x1 = i;
y1 = (int)(cyClient / 2 * (1 + sin(2 * PI * i / 80 + f)));
x2 = x1 + 1;
y2 = (int)(cyClient / 2 * (1 + sin(2 * PI * (i + 1) / 80 + f)));
MoveToEx(hdc, cxClient / 2 + x1, y1, NULL);
LineTo(hdc, cxClient / 2 + x2, y2);
}
f += 0.02;
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
DeleteObject(hPen);
ReleaseDC(hWnd, hdc);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
```
程序中使用了sine函数来绘制驻波图形,sin函数中的参数可以根据需要进行调整,可以得到不同的驻波图形。同时使用`WM_SIZE`消息获取窗口大小,使得驻波图形可以自适应窗口大小。
阅读全文