#include <Windows.h>
#include <tchar.h>
#include <math.h>
HINSTANCE hInst;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
hInst = hInstance;
WNDCLASS wls;
wls.cbClsExtra = 0;
wls.cbWndExtra = 0;
wls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wls.hCursor = LoadCursor(NULL, IDC_ARROW);
wls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wls.hInstance = hInst;
wls.lpfnWndProc = WindowProc;
wls.lpszClassName = _T("MainWnd");
wls.lpszMenuName = NULL;
wls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wls);
HWND hwnd = CreateWindow(_T("MainWnd"), _T("Test"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInst, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
#define PI 3.1415926
#define NUM 1000 //只画一个周期的信号波形
static double PMaxAmp, NMaxAmp;
static POINT apt[NUM];
static double Amp[NUM];
double fs;
RECT rect;
double Py, Ny;
switch (uMsg)
{
case WM_CREATE:
fs = 5000;
NMaxAmp = 0;
PMaxAmp = 0;
for (int i = 1; i < NUM; i++)
{
//下列模拟信号的最小正周期是1/5,即各个信号周期的最小公倍数
Amp[i] = 0.5*sin(30 * PI*i / fs) + 2 * sin(80 * PI*i / fs);
if (Amp[i] >= 0)
{
if (Amp[i] > PMaxAmp)
PMaxAmp = Amp[i];
}
else if (Amp[i] < 0)
{
if (Amp[i] < NMaxAmp)
NMaxAmp = Amp[i];
}
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
Ny = -NMaxAmp*rect.bottom / (PMaxAmp - NMaxAmp);
Py = rect.bottom - Ny;
//MoveToEx(hdc, 0, Py, NULL);
//LineTo(hdc, rect.right, Py);
for (int i = 0; i < NUM; i++)
{
apt[i].x = i*rect.right / NUM;
if (Amp[i] >= 0)
apt[i].y = Py*(1 - Amp[i] / PMaxAmp);
else
apt[i].y = Py + Ny*Amp[i] / NMaxAmp;
}
Polyline(hdc,apt,NUM);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}