Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning(disable:4996)
- #undef UNICODE
- #include <windows.h>
- #include "KWnd.h"
- #define _USE_MATH_DEFINES
- #include <cmath>
- const int WIDTH = 314;
- const int HEIGHT = 200;
- void updateRegion(HRGN& hRgn, int left, int top, int right, int bottom);
- LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
- const int INTERVAL = 50;
- //bool isOdd(int i);
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- MSG msg;
- KWnd mainWnd("MyWin", hInstance, nCmdShow, WndProc);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (msg.wParam);
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hDC;
- //coordinates
- static int sx, sy;
- static int x_scr, y_scr;
- double x;
- //ball coord
- //timer t
- static bool paused = false;
- static int t = 0;
- //pens
- static HPEN hPenAxis, hPenGraphic;
- //brush
- static HBRUSH hBrushYellow;
- //regions
- static HRGN ellipse;
- //angle for ball
- static double ballAngle = -M_PI;
- //direction
- static bool forward = true;
- switch (message) {
- case WM_CREATE:
- //create pens
- hPenAxis = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
- hPenGraphic = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
- //create brush
- hBrushYellow = CreateSolidBrush(RGB(255, 165, 0));
- //create timer
- SetTimer(hWnd, 1, INTERVAL, NULL);
- //create rgn
- ellipse = CreateEllipticRgn(sx, sy, sx, sy);
- break;
- case WM_SIZE:
- sx = LOWORD(lParam);
- sy = HIWORD(lParam);
- break;
- case WM_PAINT:
- //begin paint
- hDC = BeginPaint(hWnd, &ps);
- //set modes
- SetMapMode(hDC, MM_ANISOTROPIC);
- SetWindowExtEx(hDC, 2 * WIDTH, -2 * HEIGHT, NULL);
- SetViewportExtEx(hDC, 3 * sx / 4, 3 * sy / 4, NULL);
- SetViewportOrgEx(hDC, sx / 2, sy / 2, NULL);
- //draw axis
- SelectObject(hDC, hPenAxis);
- MoveToEx(hDC, -WIDTH, 0, NULL);
- LineTo(hDC, WIDTH, 0);
- MoveToEx(hDC, 0, HEIGHT, NULL);
- LineTo(hDC, 0, -HEIGHT);
- //draw graphic
- SelectObject(hDC, hPenGraphic);
- MoveToEx(hDC, -WIDTH, 0, NULL);
- for (x = -M_PI, x_scr = -WIDTH; x < M_PI; x += 0.02, x_scr += 2)
- {
- y_scr = HEIGHT * sin(x);
- LineTo(hDC, x_scr, y_scr);
- }
- //draw ball
- SelectObject(hDC, hBrushYellow);
- x_scr = t - WIDTH;
- y_scr = HEIGHT * sin(ballAngle);
- updateRegion(ellipse, x_scr - 15, y_scr - 15, x_scr + 15, y_scr + 15);
- FillRgn(hDC, ellipse, hBrushYellow);
- EndPaint(hWnd, &ps);
- break;
- case WM_RBUTTONDOWN:
- paused = !paused;
- break;
- case WM_TIMER:
- if (!paused)
- {
- if (forward)
- {
- t += 3;
- ballAngle += 0.03;
- }
- else if (!forward)
- {
- t -= 3;
- ballAngle -= 0.03;
- }
- if (t >= 2 * WIDTH) {
- forward = false;
- }
- else if (t <= 0) {
- forward = true;
- }
- InvalidateRgn(hWnd, NULL, true);
- }
- break;
- case WM_DESTROY:
- //delete pens
- DeleteObject(hPenAxis);
- DeleteObject(hPenGraphic);
- //delete brush
- DeleteObject(hBrushYellow);
- //delete timer
- KillTimer(hWnd, 1);
- //delete rgn
- DeleteObject(ellipse);
- PostQuitMessage(0);
- break;
- default: return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- void updateRegion(HRGN& hRgn, int left, int top, int right, int bottom) {
- DeleteObject(hRgn);
- hRgn = CreateEllipticRgn(left, top, right, bottom);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement