WinAPI——模拟正弦波

时间:2021-08-13 12:05:50

WinAPI——模拟正弦波

 /***************************
*
* 程序名称 : 模拟正弦波
* 作 者 : doodle777
* 版 本 : 1.1
* 日 期 : 2012-10-19
* 说 明 : 1.1--解决了闪屏问题
*
***************************/ #include<Windows.h>
#include<math.h> #define NUM 1000
#define PI 3.14159 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ;
void Draw(HWND, float, float) ; static int cxClient, cyClient, xOffset, yOffset;
POINT apt[NUM] ;
static int A = ;
static float w = , g = ; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("模拟正弦波") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ; wndclass.cbClsExtra = NULL ;
wndclass.cbWndExtra = NULL ;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH) ;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW) ;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION) ;
wndclass.hInstance = hInstance ;
wndclass.lpfnWndProc = WndProc ;
wndclass.lpszClassName = szAppName ;
wndclass.lpszMenuName = NULL ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ; if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This Program Requires Windows NT !"), szAppName, MB_ICONERROR) ;
return ;
} hwnd = CreateWindow(szAppName, TEXT("模拟正弦波(上下左右控制)"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow(hwnd, iCmdShow) ;
UpdateWindow(hwnd) ; while(TRUE)
{
if(PeekMessage(&msg, NULL, , , PM_REMOVE))
{
if(msg.message == WM_QUIT)
break ;
TranslateMessage(&msg) ;
DispatchMessage(&msg) ;
}
else
{
Draw(hwnd, w, g) ;
// w += 1 ;
// g += PI / 6 ; }
}
return msg.wParam ;
} LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rect ; rect.bottom = cyClient ;
rect.left = ;
rect.right = cxClient ;
rect.top = ; switch(message)
{
case WM_SIZE :
cxClient = LOWORD(lParam) ;
cyClient = HIWORD(lParam) ;
xOffset = cxClient / ;
yOffset = cyClient / ;
A = cyClient / ; return ; case WM_KEYDOWN :
InvalidateRect(hwnd, &rect, TRUE) ;
switch(wParam)
{
case VK_UP :
g += PI / ;
break ;
case VK_DOWN :
g -= PI / ;
break ;
case VK_LEFT :
w -= ;
break ;
case VK_RIGHT :
w += ;
break ;
}
return ; case WM_DESTROY :
PostQuitMessage() ;
return ;
}
return DefWindowProc(hwnd, message, wParam, lParam) ;
} void Draw(HWND hwnd, float w, float g)
{
HDC hdc ;
PAINTSTRUCT ps ;
int t ; hdc = GetDC(hwnd) ; MoveToEx(hdc, , yOffset, NULL) ;
LineTo(hdc, cxClient, yOffset) ;
MoveToEx(hdc, xOffset, , NULL) ;
LineTo(hdc, xOffset, cyClient) ; apt[].x = ;
apt[].y = yOffset + A * sin(g) ;
MoveToEx(hdc, apt[].x, apt[].y, NULL) ;
for(t = ; t < NUM ; t++)
{
apt[t].x = cxClient * t / NUM ;
apt[t].y = yOffset + A * sin(w * t / NUM + g ) ;
LineTo(hdc, apt[t].x, apt[t].y) ;
MoveToEx(hdc, apt[t].x, apt[t].y, NULL) ;
} ReleaseDC(hwnd, hdc) ;
}