YauhenMardan

WinAPI_2_2_Sinus_v2

Apr 6th, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma warning(disable:4996)
  2. #undef UNICODE
  3. #include <windows.h>
  4. #include "KWnd.h"
  5.  
  6. #define _USE_MATH_DEFINES
  7. #include <cmath>
  8. const int WIDTH = 314;
  9. const int HEIGHT = 200;
  10.  
  11. void updateRegion(HRGN& hRgn, int left, int top, int right, int bottom);
  12.  
  13. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  14.  
  15. const int INTERVAL = 50;
  16.  
  17. //bool isOdd(int i);
  18.  
  19. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  20. {
  21.     MSG msg;
  22.     KWnd mainWnd("MyWin", hInstance, nCmdShow, WndProc);
  23.     while (GetMessage(&msg, NULL, 0, 0))
  24.     {
  25.         TranslateMessage(&msg);
  26.         DispatchMessage(&msg);
  27.     }
  28.     return (msg.wParam);
  29. }
  30.  
  31. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  32. {
  33.     PAINTSTRUCT ps;
  34.     HDC hDC;
  35.     //coordinates
  36.     static int sx, sy;
  37.     static int x_scr, y_scr;
  38.     double x;
  39.     //ball coord
  40.     //timer t
  41.     static bool paused = false;
  42.     static int t = 0;
  43.     //pens
  44.     static HPEN hPenAxis, hPenGraphic;
  45.     //brush
  46.     static HBRUSH hBrushYellow;
  47.     //regions
  48.     static HRGN ellipse;
  49.     //angle for ball
  50.     static double ballAngle = -M_PI;
  51.     //direction
  52.     static bool forward = true;
  53.  
  54.     switch (message) {
  55.     case WM_CREATE:
  56.         //create pens
  57.         hPenAxis = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
  58.         hPenGraphic = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  59.         //create brush
  60.         hBrushYellow = CreateSolidBrush(RGB(255, 165, 0));
  61.         //create timer
  62.         SetTimer(hWnd, 1, INTERVAL, NULL);
  63.         //create rgn
  64.         ellipse = CreateEllipticRgn(sx, sy, sx, sy);
  65.         break;
  66.     case WM_SIZE:
  67.         sx = LOWORD(lParam);
  68.         sy = HIWORD(lParam);
  69.         break;
  70.     case WM_PAINT:
  71.         //begin paint
  72.         hDC = BeginPaint(hWnd, &ps);
  73.         //set modes
  74.         SetMapMode(hDC, MM_ANISOTROPIC);
  75.         SetWindowExtEx(hDC, 2 * WIDTH, -2 * HEIGHT, NULL);
  76.         SetViewportExtEx(hDC, 3 * sx / 4, 3 * sy / 4, NULL);
  77.         SetViewportOrgEx(hDC, sx / 2, sy / 2, NULL);
  78.         //draw axis
  79.         SelectObject(hDC, hPenAxis);
  80.         MoveToEx(hDC, -WIDTH, 0, NULL);
  81.         LineTo(hDC, WIDTH, 0);
  82.         MoveToEx(hDC, 0, HEIGHT, NULL);
  83.         LineTo(hDC, 0, -HEIGHT);
  84.         //draw graphic
  85.         SelectObject(hDC, hPenGraphic);
  86.         MoveToEx(hDC, -WIDTH, 0, NULL);
  87.         for (x = -M_PI, x_scr = -WIDTH; x < M_PI; x += 0.02, x_scr += 2)
  88.         {
  89.             y_scr = HEIGHT * sin(x);
  90.             LineTo(hDC, x_scr, y_scr);
  91.         }
  92.         //draw ball
  93.         SelectObject(hDC, hBrushYellow);
  94.         x_scr = t - WIDTH;
  95.         y_scr = HEIGHT * sin(ballAngle);
  96.         updateRegion(ellipse, x_scr - 15, y_scr - 15, x_scr + 15, y_scr + 15);
  97.         FillRgn(hDC, ellipse, hBrushYellow);
  98.         EndPaint(hWnd, &ps);
  99.         break;
  100.     case WM_RBUTTONDOWN:
  101.         paused = !paused;
  102.         break;
  103.     case WM_TIMER:
  104.         if (!paused)
  105.         {
  106.             if (forward)
  107.             {
  108.                 t += 3;
  109.                 ballAngle += 0.03;
  110.             }
  111.             else if (!forward)
  112.             {
  113.                 t -= 3;
  114.                 ballAngle -= 0.03;
  115.             }
  116.             if (t >= 2 * WIDTH) {
  117.                 forward = false;
  118.             }
  119.             else if (t <= 0) {
  120.                 forward = true;
  121.             }
  122.             InvalidateRgn(hWnd, NULL, true);
  123.         }
  124.             break;
  125.         case WM_DESTROY:
  126.             //delete pens
  127.             DeleteObject(hPenAxis);
  128.             DeleteObject(hPenGraphic);
  129.             //delete brush
  130.             DeleteObject(hBrushYellow);
  131.             //delete timer
  132.             KillTimer(hWnd, 1);
  133.             //delete rgn
  134.             DeleteObject(ellipse);
  135.             PostQuitMessage(0);
  136.             break;
  137.         default: return DefWindowProc(hWnd, message, wParam, lParam);
  138.     }
  139.     return 0;
  140. }
  141.  
  142. void updateRegion(HRGN& hRgn, int left, int top, int right, int bottom) {
  143.     DeleteObject(hRgn);
  144.     hRgn = CreateEllipticRgn(left, top, right, bottom);
  145. }
Add Comment
Please, Sign In to add comment