Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define COLOURS 16
- #include <windows.h>
- LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
- HINSTANCE hInstance;
- RECT r;
- RECT przybornik;
- RECT figures[4];
- HDC hdc;
- bool rysuj = false;
- COLORREF color = RGB(0, 0, 0);
- enum FIGURE { TRIANGLE, FILL_TRIANGLE, CIRCLE, SQUARE };
- FIGURE figure = SQUARE;
- struct Colours {
- RECT rect;
- COLORREF color;
- };
- Colours colours[16];
- bool static StworzOkno(WNDCLASSEX &window)
- {
- window.cbSize = sizeof(WNDCLASSEX);
- window.style = CS_VREDRAW | CS_HREDRAW;
- window.lpfnWndProc = ProceduraOkna;
- window.cbClsExtra = 0;
- window.cbWndExtra = 0;
- window.hInstance = hInstance;
- window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- window.hCursor = LoadCursor(NULL, IDC_ARROW);
- window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- window.lpszMenuName = NULL;
- window.lpszClassName = L"StandardoweOkno";
- window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- if (!RegisterClassEx(&window))
- {
- MessageBox(NULL, L"Problem z rejestracją okna", L"Rejestracja okna", MB_ICONERROR | MB_OK);
- return true;
- }
- }
- void JakieKolory() {
- colours[0].color = RGB(0, 0, 0);
- colours[1].color = RGB(255, 255, 255);
- colours[2].color = RGB(128, 0, 0);
- colours[3].color = RGB(0, 128, 255);
- colours[4].color = RGB(0, 64, 0);
- colours[5].color = RGB(255, 64, 128);
- colours[6].color = RGB(0, 32, 128);
- colours[7].color = RGB(128, 0, 255);
- colours[8].color = RGB(32, 0, 32);
- colours[9].color = RGB(64, 64, 32);
- colours[10].color = RGB(128, 64, 32);
- colours[11].color = RGB(16, 255, 255);
- colours[12].color = RGB(128, 32, 128);
- colours[13].color = RGB(32, 32, 32);
- colours[14].color = RGB(64, 255, 64);
- colours[15].color = RGB(32, 32, 16);
- colours[16].color = RGB(128, 255, 128);
- }
- void Okna(HWND hWnd) {
- GetClientRect(hWnd, &r);
- Rectangle(hdc, r.left, r.top, r.right, r.bottom);
- przybornik.left = r.right - 140;
- przybornik.right = r.right;
- przybornik.top = r.top;
- przybornik.bottom = r.bottom;
- HBRUSH PrzybornikBrush, OldBrush;
- HPEN PrzybornikPen, OldPen;
- PrzybornikPen = (HPEN)CreateSolidBrush(0xC0C0C0);
- PrzybornikBrush = (HBRUSH)CreateSolidBrush(0x000000);
- OldBrush = (HBRUSH)SelectObject(hdc, PrzybornikBrush);
- OldPen = (HPEN)SelectObject(hdc, PrzybornikPen);
- Rectangle(hdc, przybornik.left, przybornik.top, przybornik.right, przybornik.bottom);
- SelectObject(hdc, OldPen);
- SelectObject(hdc, OldBrush);
- DeleteObject(OldBrush);
- DeleteObject(OldPen);
- DeleteObject(PrzybornikBrush);
- DeleteObject(PrzybornikPen);
- }
- void UstawKolory() {
- int count = 0 ,x , y = 15;
- for (int i = 0; i < 16; i++)
- {
- x = (55 * count) + przybornik.left + 20;
- colours[i].rect.left = x;
- colours[i].rect.top = y;
- colours[i].rect.bottom = y + 45;
- colours[i].rect.right = x + 45;
- count++;
- if (count % 2 == 0) {
- y += 50;
- count = 0;
- }
- }
- }
- void UstawFigury() {
- int count = 0;
- int y = 20;
- for (int i = 0; i < 4; i++)
- {
- figures[i].left = colours[14 + count].rect.left;
- figures[i].top = colours[15].rect.top + 45 + y;
- figures[i].bottom = colours[15].rect.bottom + 45 + y;
- figures[i].right = colours[14 + count].rect.right;
- count++;
- if (count % 2 == 0)
- {
- count = 0;
- y += 50;
- }
- }
- }
- void Rysuj(HWND hWnd) {
- HBRUSH Brush, OldBrush;
- PAINTSTRUCT paint;
- hdc = BeginPaint(hWnd, &paint);
- JakieKolory();
- Okna(hWnd);
- UstawKolory();
- UstawFigury();
- for (int i = 0; i < COLOURS; i++)
- {
- Brush = CreateSolidBrush(colours[i].color);
- OldBrush = (HBRUSH)SelectObject(hdc, Brush);
- Rectangle(hdc, colours[i].rect.left, colours[i].rect.top, colours[i].rect.right, colours[i].rect.bottom);
- SelectObject(hdc, OldBrush);
- DeleteObject(Brush);
- DeleteObject(OldBrush);
- }
- for (int i = 0; i < 4; i++)
- {
- Brush = CreateSolidBrush(colours[1].color);
- OldBrush = (HBRUSH)SelectObject(hdc, Brush);
- Rectangle(hdc, figures[i].left, figures[i].top, figures[i].right, figures[i].bottom);
- SelectObject(hdc, OldBrush);
- DeleteObject(Brush);
- DeleteObject(OldBrush);
- }
- Brush = CreateSolidBrush(colours[0].color);
- OldBrush = (HBRUSH)SelectObject(hdc, Brush);
- POINT point[3];
- point[0].x = figures[0].left; point[0].y = figures[0].top;
- point[1].x = figures[0].left + (45 / 2); point[1].y = figures[0].top + 45;
- point[2].x = figures[0].left + 45; point[2].y = figures[0].top;
- Polygon(hdc, point, 3);
- SelectObject(hdc, OldBrush);
- DeleteObject(Brush);
- DeleteObject(OldBrush);
- Brush = CreateSolidBrush(0xFFFFFF);
- OldBrush = (HBRUSH)SelectObject(hdc, Brush);
- point[0].x = figures[1].left; point[0].y = figures[1].top;
- point[1].x = figures[1].left + (45 / 2); point[1].y = figures[1].top + 45;
- point[2].x = figures[1].left + 45; point[2].y = figures[1].top;
- Polygon(hdc, point, 3);
- SelectObject(hdc, OldBrush);
- DeleteObject(Brush);
- DeleteObject(OldBrush);
- Brush = CreateSolidBrush(0x000000);
- OldBrush = (HBRUSH)SelectObject(hdc, Brush);
- Rectangle(hdc, figures[3].left + 5, figures[3].top + 5, figures[3].right - 5, figures[3].bottom - 5);
- SelectObject(hdc, OldBrush);
- DeleteObject(Brush);
- DeleteObject(OldBrush);
- Brush = CreateSolidBrush(0x000000);
- OldBrush = (HBRUSH)SelectObject(hdc, Brush);
- Ellipse(hdc, figures[2].left + 40, figures[2].top + 5, figures[2].right - 40, figures[3].bottom - 5);
- SelectObject(hdc, OldBrush);
- DeleteObject(Brush);
- DeleteObject(OldBrush);
- EndPaint(hWnd, &paint);
- }
- bool KolizjaPrzybornik(RECT rect, LPARAM lParam)
- {
- DWORD mouseX = LOWORD(lParam);
- DWORD mouseY = HIWORD(lParam);
- if (mouseX > rect.left && mouseX < rect.right && mouseY < rect.bottom && mouseY > rect.top)
- return true;
- return false;
- }
- bool KolizjaKwadraty(RECT rect, LPARAM lParam) {
- DWORD mouseX = LOWORD(lParam);
- DWORD mouseY = HIWORD(lParam);
- if (mouseX > rect.left && mouseX < rect.right && mouseY < rect.bottom && mouseY > rect.top)
- return true;
- return false;
- }
- void Ruszaj(HWND hWnd ,LPARAM lParam) {
- if (!KolizjaPrzybornik(przybornik, lParam))
- {
- if (rysuj)
- {
- HDC hdc = GetDC(hWnd);
- HBRUSH Brush, OldBrush;
- HPEN Pen, OldPen;
- Brush = CreateSolidBrush(color);
- Pen = CreatePen(PS_SOLID, 1, color);
- OldBrush = (HBRUSH)SelectObject(hdc, Brush);
- OldPen = (HPEN)SelectObject(hdc, Pen);
- switch (figure)
- {
- case TRIANGLE:
- {
- Brush = CreateSolidBrush(0xFFFFFF);
- OldBrush = (HBRUSH)SelectObject(hdc, Brush);
- POINT point[3];
- point[0].x = LOWORD(lParam); point[0].y = HIWORD(lParam);
- point[1].x = LOWORD(lParam) + (10 / 2); point[1].y = HIWORD(lParam) - 10;
- point[2].x = LOWORD(lParam) + 10; point[2].y = HIWORD(lParam);
- Polygon(hdc, point, 3);
- }
- break;
- case FILL_TRIANGLE:
- {
- POINT point[3];
- point[0].x = LOWORD(lParam); point[0].y = HIWORD(lParam);
- point[1].x = LOWORD(lParam) + (10 / 2); point[1].y = HIWORD(lParam) - 10;
- point[2].x = LOWORD(lParam) + 10; point[2].y = HIWORD(lParam);
- Polygon(hdc, point, 3);
- }
- break;
- case SQUARE:
- Rectangle(hdc, LOWORD(lParam), HIWORD(lParam), LOWORD(lParam) + 10, HIWORD(lParam) + 10);
- break;
- case CIRCLE:
- Ellipse(hdc, LOWORD(lParam), HIWORD(lParam), LOWORD(lParam) + 10, HIWORD(lParam) + 10);
- break;
- }
- SelectObject(hdc, OldBrush);
- SelectObject(hdc, OldPen);
- DeleteObject(Brush);
- DeleteObject(OldBrush);
- DeleteObject(Pen);
- DeleteObject(OldPen);
- ReleaseDC(hWnd, hdc);
- }
- }
- else
- {
- if (rysuj)
- {
- for(int i = 0; i < 16; i++)
- if (KolizjaKwadraty(colours[i].rect, lParam))
- {
- color = colours[i].color;
- break;
- }
- for(int i = 0; i < 4; i++)
- if (KolizjaKwadraty(figures[i], lParam))
- {
- switch (i)
- {
- case 0:
- figure = FILL_TRIANGLE;
- break;
- case 1:
- figure = TRIANGLE;
- break;
- case 2:
- figure = CIRCLE;
- break;
- case 3:
- figure = SQUARE;
- break;
- }
- break;
- }
- }
- }
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCMD)
- {
- WNDCLASSEX window;
- if (!StworzOkno(window))
- return 0;
- HWND uchwytOkna = 0;
- uchwytOkna = CreateWindowEx(
- WS_EX_WINDOWEDGE,
- L"StandardoweOkno",
- L"Pierwsze okno",
- WS_OVERLAPPEDWINDOW | WS_VISIBLE,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- 1024,
- 768,
- NULL,
- NULL,
- hInstance,
- NULL);
- ShowWindow(uchwytOkna, SW_NORMAL);
- UpdateWindow(uchwytOkna);
- MSG msg;
- for (; ;)
- {
- if (0 != GetMessage(&msg, 0, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- if (msg.message == WM_QUIT) break;
- }
- return (int)msg.wParam;
- }
- LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_CLOSE:
- if (MessageBox(hWnd, L"Czy chcesz zakonczyc dzialanie programu?", L"Dzialanie programu", MB_YESNO | MB_ICONERROR) == IDNO)
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_PAINT:
- Rysuj(hWnd);
- break;
- case WM_MOUSEMOVE:
- Ruszaj(hWnd ,lParam);
- break;
- case WM_LBUTTONDOWN:
- rysuj = true;
- SendMessage(hWnd, WM_MOUSEMOVE, wParam, lParam);
- break;
- case WM_LBUTTONUP:
- rysuj = false;
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement