Advertisement
Guest User

WinAPI - Grafika Wektorowa (Gwiazdki)

a guest
Nov 16th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <list>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8. const TCHAR NazwaKlasy[] = TEXT("OKNO");
  9.  
  10. class Gwiazda
  11. {
  12. public:
  13.     Gwiazda(const unsigned int x, const unsigned int y, const unsigned int rozmiar, const COLORREF kolorRamki, const COLORREF kolorWypelnienia)
  14.     : x(x), y(y), rozmiar(rozmiar), kolorRamki(kolorRamki), kolorWypelnienia(kolorWypelnienia)
  15.     {
  16.         const int bok = 2 * rozmiar / sqrt(3.0);
  17.         const int h1_3 = rozmiar / 3;
  18.         const int h2_3 = 2 * h1_3;
  19.         // gwiazda rysowana na zasadzie 2 trojkatow rownobocznych, jak gwiazda dawida, 6ramienna
  20.         Triangle1[0].x = x;
  21.         Triangle1[0].y = y - h2_3;
  22.         Triangle1[1].x = x - bok / 2;
  23.         Triangle1[1].y = y + h1_3;
  24.         Triangle1[2].x = x + bok / 2;
  25.         Triangle1[2].y = y + h1_3;
  26.  
  27.         Triangle2[0].x = x;
  28.         Triangle2[0].y = y + h2_3;
  29.         Triangle2[1].x = x - bok / 2;
  30.         Triangle2[1].y = y - h1_3;
  31.         Triangle2[2].x = x + bok / 2;
  32.         Triangle2[2].y = y - h1_3;
  33.     }
  34.  
  35.     void Rysuj(const HDC hdc)
  36.     {
  37.         const HWND hwnd = WindowFromDC(hdc);
  38.         RECT ClientRect;
  39.         GetClientRect(hwnd, &ClientRect);
  40.  
  41.         SetDCBrushColor(hdc, kolorWypelnienia);
  42.         SetDCPenColor(hdc, kolorRamki);
  43.         SelectObject(hdc, (HBRUSH)GetStockObject(DC_BRUSH));
  44.         SelectObject(hdc, (HPEN)GetStockObject(DC_PEN));
  45.  
  46.         Polygon(hdc, Triangle1, 3);
  47.         Polygon(hdc, Triangle2, 3);
  48.     }
  49.  
  50.     void Przesun(const HDC hdc, const int Dx, const int Dy)
  51.     {
  52.         const HWND hwnd = WindowFromDC(hdc);
  53.         RECT ClientRect;
  54.         GetClientRect(hwnd, &ClientRect);
  55.         int R = rozmiar * 2 / 3;
  56.        
  57.         if ((x + Dx - R) > ClientRect.left && (x + Dx + R) < ClientRect.right)
  58.         {
  59.             Triangle1[0].x += Dx; Triangle1[1].x += Dx; Triangle1[2].x += Dx;
  60.             Triangle2[0].x += Dx; Triangle2[1].x += Dx; Triangle2[2].x += Dx;
  61.             x += Dx;
  62.         }
  63.        
  64.         if ((y + Dy - R) > ClientRect.top && (y + Dy + R) < ClientRect.bottom)
  65.         {
  66.             Triangle1[0].y += Dy; Triangle1[1].y += Dy; Triangle1[2].y += Dy;
  67.             Triangle2[0].y += Dy; Triangle2[1].y += Dy; Triangle2[2].y += Dy;
  68.             y += Dy;
  69.         }
  70.     }
  71. private:
  72.     int x, y;
  73.     COLORREF kolorRamki, kolorWypelnienia;
  74.     int rozmiar;    // za rozmiar przyjąłem wysokość trójkąta
  75.     POINT Triangle1[3], Triangle2[3];
  76. };
  77.  
  78. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  79. {
  80.     srand(time(0));
  81.     static RECT ClientRect;
  82.     GetClientRect(hwnd, &ClientRect);
  83.     static list<Gwiazda> Gwiazdy;
  84.     static const unsigned int lGwiazdek = 10;
  85.     switch (msg)
  86.     {
  87.         case WM_CREATE:
  88.             for (int i = 0; i < lGwiazdek; i++)
  89.             {
  90.                 int rozmiar = (rand() % 71) + 30;
  91.                 int x = (rand() % (ClientRect.right - 2 * rozmiar)) + rozmiar;
  92.                 int y = (rand() % (ClientRect.bottom - 2 * rozmiar)) + rozmiar;
  93.                 Gwiazdy.push_back(Gwiazda(x, y, rozmiar, RGB(rand() % 254 + 1, rand() % 254 + 1, rand() % 254 + 1), RGB(rand() % 254 + 1, rand() % 254 + 1, rand() % 254 + 1)));
  94.             }
  95.             SetTimer(hwnd, 1, 100, NULL);
  96.             break;
  97.         case WM_PAINT:
  98.         {
  99.             PAINTSTRUCT ps;
  100.             BeginPaint(hwnd, &ps);
  101.             SetDCBrushColor(ps.hdc, RGB(0, 0, 50));
  102.             FillRect(ps.hdc, &ps.rcPaint, (HBRUSH)GetStockObject(DC_BRUSH));
  103.  
  104.             _List_iterator<_List_val<_List_simple_types<Gwiazda>>> it = Gwiazdy.begin();
  105.  
  106.             for (int i = 0; i < lGwiazdek; i++, it++)
  107.             {
  108.                 it->Rysuj(ps.hdc);
  109.             }
  110.            
  111.             EndPaint(hwnd, &ps);
  112.             break;
  113.         }
  114.         case WM_TIMER:
  115.         {
  116.             _List_iterator<_List_val<_List_simple_types<Gwiazda>>> it = Gwiazdy.begin();
  117.  
  118.             for (int i = 0; i < lGwiazdek; i++, it++)
  119.             {
  120.                 it->Przesun(GetDC(hwnd), (rand() % 11) - 5, (rand() % 11) - 5);
  121.             }
  122.             InvalidateRect(hwnd, &ClientRect, true);
  123.             break;
  124.         }
  125.         case WM_CLOSE:
  126.             if ((MessageBox(hwnd, TEXT("Czy chcesz wyłączyć okno ?"), TEXT("Pytanie"), MB_YESNO)) == IDYES)
  127.                 DestroyWindow(hwnd);
  128.             else break;
  129.  
  130.         case WM_DESTROY:
  131.             KillTimer(hwnd, 1);
  132.             Gwiazdy.erase(Gwiazdy.begin(), Gwiazdy.end());
  133.             PostQuitMessage(0);
  134.             break;
  135.         default:
  136.             return DefWindowProcW(hwnd, msg, wParam, lParam);
  137.     }
  138.  
  139.     return 0;
  140. }
  141.  
  142. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  143. {
  144.     WNDCLASSEX window;
  145.     window.cbClsExtra = NULL;
  146.     window.cbSize = sizeof(WNDCLASSEX);
  147.     window.cbWndExtra = NULL;
  148.     window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  149.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  150.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  151.     window.hIconSm = NULL;
  152.     window.hInstance = hInstance;
  153.     window.lpfnWndProc = WndProc;
  154.     window.lpszClassName = NazwaKlasy;
  155.     window.lpszMenuName = 0;
  156.     window.style = CS_HREDRAW | CS_VREDRAW;
  157.  
  158.     if (!RegisterClassEx(&window))
  159.     {
  160.         MessageBox(NULL, TEXT("Zarejestrowanie klasy nieudane!"), TEXT("WARNING"), MB_OK | MB_ICONWARNING);
  161.         return 1;
  162.     }
  163.  
  164.     HWND okno = CreateWindowEx(WS_EX_WINDOWEDGE, NazwaKlasy, TEXT("Okno"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
  165.     if (okno == NULL)
  166.     {
  167.         MessageBox(NULL, TEXT("Stworzenie okna nieudane!"), TEXT("WARNING"), MB_OK | MB_ICONWARNING);
  168.         return 2;
  169.     }
  170.  
  171.     ShowWindow(okno, nCmdShow);
  172.     UpdateWindow(okno);
  173.     MSG Komunikat;
  174.     while (GetMessage(&Komunikat, 0, 0, 0) > 0)
  175.     {
  176.         TranslateMessage(&Komunikat);
  177.         DispatchMessage(&Komunikat);
  178.     }
  179.     UnregisterClass(NazwaKlasy, hInstance);
  180.     return Komunikat.wParam;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement