Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <stdio.h>
- #include <math.h>
- #include <list>
- #include <time.h>
- using namespace std;
- const TCHAR NazwaKlasy[] = TEXT("OKNO");
- class Gwiazda
- {
- public:
- Gwiazda(const unsigned int x, const unsigned int y, const unsigned int rozmiar, const COLORREF kolorRamki, const COLORREF kolorWypelnienia)
- : x(x), y(y), rozmiar(rozmiar), kolorRamki(kolorRamki), kolorWypelnienia(kolorWypelnienia)
- {
- const int bok = 2 * rozmiar / sqrt(3.0);
- const int h1_3 = rozmiar / 3;
- const int h2_3 = 2 * h1_3;
- // gwiazda rysowana na zasadzie 2 trojkatow rownobocznych, jak gwiazda dawida, 6ramienna
- Triangle1[0].x = x;
- Triangle1[0].y = y - h2_3;
- Triangle1[1].x = x - bok / 2;
- Triangle1[1].y = y + h1_3;
- Triangle1[2].x = x + bok / 2;
- Triangle1[2].y = y + h1_3;
- Triangle2[0].x = x;
- Triangle2[0].y = y + h2_3;
- Triangle2[1].x = x - bok / 2;
- Triangle2[1].y = y - h1_3;
- Triangle2[2].x = x + bok / 2;
- Triangle2[2].y = y - h1_3;
- }
- void Rysuj(const HDC hdc)
- {
- const HWND hwnd = WindowFromDC(hdc);
- RECT ClientRect;
- GetClientRect(hwnd, &ClientRect);
- SetDCBrushColor(hdc, kolorWypelnienia);
- SetDCPenColor(hdc, kolorRamki);
- SelectObject(hdc, (HBRUSH)GetStockObject(DC_BRUSH));
- SelectObject(hdc, (HPEN)GetStockObject(DC_PEN));
- Polygon(hdc, Triangle1, 3);
- Polygon(hdc, Triangle2, 3);
- }
- void Przesun(const HDC hdc, const int Dx, const int Dy)
- {
- const HWND hwnd = WindowFromDC(hdc);
- RECT ClientRect;
- GetClientRect(hwnd, &ClientRect);
- int R = rozmiar * 2 / 3;
- if ((x + Dx - R) > ClientRect.left && (x + Dx + R) < ClientRect.right)
- {
- Triangle1[0].x += Dx; Triangle1[1].x += Dx; Triangle1[2].x += Dx;
- Triangle2[0].x += Dx; Triangle2[1].x += Dx; Triangle2[2].x += Dx;
- x += Dx;
- }
- if ((y + Dy - R) > ClientRect.top && (y + Dy + R) < ClientRect.bottom)
- {
- Triangle1[0].y += Dy; Triangle1[1].y += Dy; Triangle1[2].y += Dy;
- Triangle2[0].y += Dy; Triangle2[1].y += Dy; Triangle2[2].y += Dy;
- y += Dy;
- }
- }
- private:
- int x, y;
- COLORREF kolorRamki, kolorWypelnienia;
- int rozmiar; // za rozmiar przyjąłem wysokość trójkąta
- POINT Triangle1[3], Triangle2[3];
- };
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- srand(time(0));
- static RECT ClientRect;
- GetClientRect(hwnd, &ClientRect);
- static list<Gwiazda> Gwiazdy;
- static const unsigned int lGwiazdek = 10;
- switch (msg)
- {
- case WM_CREATE:
- for (int i = 0; i < lGwiazdek; i++)
- {
- int rozmiar = (rand() % 71) + 30;
- int x = (rand() % (ClientRect.right - 2 * rozmiar)) + rozmiar;
- int y = (rand() % (ClientRect.bottom - 2 * rozmiar)) + rozmiar;
- 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)));
- }
- SetTimer(hwnd, 1, 100, NULL);
- break;
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- BeginPaint(hwnd, &ps);
- SetDCBrushColor(ps.hdc, RGB(0, 0, 50));
- FillRect(ps.hdc, &ps.rcPaint, (HBRUSH)GetStockObject(DC_BRUSH));
- _List_iterator<_List_val<_List_simple_types<Gwiazda>>> it = Gwiazdy.begin();
- for (int i = 0; i < lGwiazdek; i++, it++)
- {
- it->Rysuj(ps.hdc);
- }
- EndPaint(hwnd, &ps);
- break;
- }
- case WM_TIMER:
- {
- _List_iterator<_List_val<_List_simple_types<Gwiazda>>> it = Gwiazdy.begin();
- for (int i = 0; i < lGwiazdek; i++, it++)
- {
- it->Przesun(GetDC(hwnd), (rand() % 11) - 5, (rand() % 11) - 5);
- }
- InvalidateRect(hwnd, &ClientRect, true);
- break;
- }
- case WM_CLOSE:
- if ((MessageBox(hwnd, TEXT("Czy chcesz wyłączyć okno ?"), TEXT("Pytanie"), MB_YESNO)) == IDYES)
- DestroyWindow(hwnd);
- else break;
- case WM_DESTROY:
- KillTimer(hwnd, 1);
- Gwiazdy.erase(Gwiazdy.begin(), Gwiazdy.end());
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProcW(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- WNDCLASSEX window;
- window.cbClsExtra = NULL;
- window.cbSize = sizeof(WNDCLASSEX);
- window.cbWndExtra = NULL;
- window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- window.hCursor = LoadCursor(NULL, IDC_ARROW);
- window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- window.hIconSm = NULL;
- window.hInstance = hInstance;
- window.lpfnWndProc = WndProc;
- window.lpszClassName = NazwaKlasy;
- window.lpszMenuName = 0;
- window.style = CS_HREDRAW | CS_VREDRAW;
- if (!RegisterClassEx(&window))
- {
- MessageBox(NULL, TEXT("Zarejestrowanie klasy nieudane!"), TEXT("WARNING"), MB_OK | MB_ICONWARNING);
- return 1;
- }
- HWND okno = CreateWindowEx(WS_EX_WINDOWEDGE, NazwaKlasy, TEXT("Okno"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
- if (okno == NULL)
- {
- MessageBox(NULL, TEXT("Stworzenie okna nieudane!"), TEXT("WARNING"), MB_OK | MB_ICONWARNING);
- return 2;
- }
- ShowWindow(okno, nCmdShow);
- UpdateWindow(okno);
- MSG Komunikat;
- while (GetMessage(&Komunikat, 0, 0, 0) > 0)
- {
- TranslateMessage(&Komunikat);
- DispatchMessage(&Komunikat);
- }
- UnregisterClass(NazwaKlasy, hInstance);
- return Komunikat.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement