#include #include #include #define NUM 1000 #define TWOPI (2 * 3.14159) LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevHinstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("SineWave"); 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("BOMB!"),"BOMB!", MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, TEXT("Sinewave using a POLYLINE"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); // shows the windows UpdateWindow(hwnd); // send a WM_PAINT message 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 offset = 0; HDC hdc; int i; PAINTSTRUCT ps; POINT apt[NUM]; RECT rect; TCHAR szBuf[100]; switch (message) { case WM_CREATE: SetTimer(hwnd, 1, 10, NULL); return 0; case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); return 0; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); wsprintf(szBuf, TEXT("Offset: %d"), offset); rect.top = (long) (cyClient / 2 * ( 1 - sin(TWOPI * (offset) / NUM))); DrawText(hdc,szBuf, -1, &rect, DT_SINGLELINE); wsprintf(szBuf, TEXT("Whee!!!")); rect.top = (long) (cyClient / 2 * ( 1 - sin(TWOPI * (-offset) / NUM))); DrawText(hdc,szBuf, -1, &rect, DT_SINGLELINE); MoveToEx(hdc, 0, cyClient / 2, NULL); LineTo(hdc, cxClient, cyClient / 2); for(i = 0 ; i < NUM ; i++ ) { apt[i].x = i * cxClient / NUM; apt[i].y = (int) (cyClient / 2 * ( 1 - sin(TWOPI * (i + offset) / NUM))); } Polyline(hdc, apt, NUM); for(i = 0 ; i < NUM ; i++ ) { apt[i].x = i * cxClient / NUM; apt[i].y = (int) (cyClient / 2 * ( 1 - sin(TWOPI * (i - offset) / NUM))); } Polyline(hdc, apt, NUM); EndPaint(hwnd, &ps); return 0; case WM_TIMER: offset++; InvalidateRect(hwnd,NULL,true); return 0; case WM_DESTROY: KillTimer(hwnd, 1); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }