Advertisement
Cieslin

PIU_PAINT

Nov 14th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.28 KB | None | 0 0
  1. #define COLOURS 16
  2.  
  3. #include <windows.h>
  4.  
  5. LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  6.  
  7. HINSTANCE hInstance;
  8. RECT r;
  9. RECT przybornik;
  10. RECT figures[4];
  11. HDC hdc;
  12. bool rysuj = false;
  13. COLORREF color = RGB(0, 0, 0);
  14. enum FIGURE { TRIANGLE, FILL_TRIANGLE, CIRCLE, SQUARE };
  15. FIGURE figure = SQUARE;
  16.  
  17.  
  18. struct Colours {
  19.  
  20.     RECT rect;
  21.     COLORREF color;
  22. };
  23.  
  24. Colours colours[16];
  25.  
  26.  
  27. bool static StworzOkno(WNDCLASSEX &window)
  28. {
  29.     window.cbSize = sizeof(WNDCLASSEX);
  30.     window.style = CS_VREDRAW | CS_HREDRAW;
  31.     window.lpfnWndProc = ProceduraOkna;
  32.     window.cbClsExtra = 0;
  33.     window.cbWndExtra = 0;
  34.     window.hInstance = hInstance;
  35.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  36.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  37.     window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  38.     window.lpszMenuName = NULL;
  39.     window.lpszClassName = L"StandardoweOkno";
  40.     window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  41.  
  42.     if (!RegisterClassEx(&window))
  43.     {
  44.         MessageBox(NULL, L"Problem z rejestracją okna", L"Rejestracja okna", MB_ICONERROR | MB_OK);
  45.         return true;
  46.     }
  47. }
  48.  
  49. void JakieKolory() {
  50.     colours[0].color = RGB(0, 0, 0);
  51.     colours[1].color = RGB(255, 255, 255);
  52.     colours[2].color = RGB(128, 0, 0);
  53.     colours[3].color = RGB(0, 128, 255);
  54.     colours[4].color = RGB(0, 64, 0);
  55.     colours[5].color = RGB(255, 64, 128);
  56.     colours[6].color = RGB(0, 32, 128);
  57.     colours[7].color = RGB(128, 0, 255);
  58.     colours[8].color = RGB(32, 0, 32);
  59.     colours[9].color = RGB(64, 64, 32);
  60.     colours[10].color = RGB(128, 64, 32);
  61.     colours[11].color = RGB(16, 255, 255);
  62.     colours[12].color = RGB(128, 32, 128);
  63.     colours[13].color = RGB(32, 32, 32);
  64.     colours[14].color = RGB(64, 255, 64);
  65.     colours[15].color = RGB(32, 32, 16);
  66.     colours[16].color = RGB(128, 255, 128);
  67. }
  68.  
  69. void Okna(HWND hWnd) {
  70.     GetClientRect(hWnd, &r);
  71.     Rectangle(hdc, r.left, r.top, r.right, r.bottom);
  72.     przybornik.left = r.right - 140;
  73.     przybornik.right = r.right;
  74.     przybornik.top = r.top;
  75.     przybornik.bottom = r.bottom;
  76.     HBRUSH PrzybornikBrush, OldBrush;
  77.     HPEN PrzybornikPen, OldPen;
  78.     PrzybornikPen = (HPEN)CreateSolidBrush(0xC0C0C0);
  79.     PrzybornikBrush = (HBRUSH)CreateSolidBrush(0x000000);
  80.     OldBrush = (HBRUSH)SelectObject(hdc, PrzybornikBrush);
  81.     OldPen = (HPEN)SelectObject(hdc, PrzybornikPen);
  82.     Rectangle(hdc, przybornik.left, przybornik.top, przybornik.right, przybornik.bottom);
  83.     SelectObject(hdc, OldPen);
  84.     SelectObject(hdc, OldBrush);
  85.     DeleteObject(OldBrush);
  86.     DeleteObject(OldPen);
  87.     DeleteObject(PrzybornikBrush);
  88.     DeleteObject(PrzybornikPen);
  89. }
  90.  
  91. void UstawKolory() {
  92.     int count = 0 ,x , y = 15;
  93.  
  94.    
  95.     for (int i = 0; i < 16; i++)
  96.     {
  97.         x = (55 * count) + przybornik.left + 20;
  98.  
  99.         colours[i].rect.left = x;
  100.         colours[i].rect.top = y;
  101.         colours[i].rect.bottom = y + 45;
  102.         colours[i].rect.right = x + 45;
  103.  
  104.         count++;
  105.  
  106.         if (count % 2 == 0) {
  107.             y += 50;
  108.             count = 0;
  109.         }
  110.            
  111.     }
  112. }
  113.  
  114.  
  115. void UstawFigury() {
  116.     int count = 0;
  117.     int y = 20;
  118.     for (int i = 0; i < 4; i++)
  119.     {
  120.         figures[i].left = colours[14 + count].rect.left;
  121.         figures[i].top = colours[15].rect.top + 45 + y;
  122.         figures[i].bottom = colours[15].rect.bottom + 45 + y;
  123.         figures[i].right = colours[14 + count].rect.right;
  124.  
  125.         count++;
  126.         if (count % 2 == 0)
  127.         {
  128.             count = 0;
  129.             y += 50;
  130.         }
  131.     }
  132. }
  133.  
  134.  
  135. void Rysuj(HWND hWnd) {
  136.     HBRUSH Brush, OldBrush;
  137.     PAINTSTRUCT paint;
  138.     hdc = BeginPaint(hWnd, &paint);
  139.     JakieKolory();
  140.     Okna(hWnd);
  141.     UstawKolory();
  142.     UstawFigury();
  143.  
  144.  
  145.     for (int i = 0; i < COLOURS; i++)
  146.     {
  147.         Brush = CreateSolidBrush(colours[i].color);
  148.         OldBrush = (HBRUSH)SelectObject(hdc, Brush);
  149.  
  150.         Rectangle(hdc, colours[i].rect.left, colours[i].rect.top, colours[i].rect.right, colours[i].rect.bottom);
  151.         SelectObject(hdc, OldBrush);
  152.         DeleteObject(Brush);
  153.         DeleteObject(OldBrush);
  154.     }
  155.  
  156.  
  157.     for (int i = 0; i < 4; i++)
  158.     {
  159.         Brush = CreateSolidBrush(colours[1].color);
  160.         OldBrush = (HBRUSH)SelectObject(hdc, Brush);
  161.  
  162.         Rectangle(hdc, figures[i].left, figures[i].top, figures[i].right, figures[i].bottom);
  163.         SelectObject(hdc, OldBrush);
  164.         DeleteObject(Brush);
  165.         DeleteObject(OldBrush);
  166.     }
  167.  
  168.  
  169.     Brush = CreateSolidBrush(colours[0].color);
  170.     OldBrush = (HBRUSH)SelectObject(hdc, Brush);
  171.  
  172.     POINT point[3];
  173.     point[0].x = figures[0].left; point[0].y = figures[0].top;
  174.     point[1].x = figures[0].left + (45 / 2); point[1].y = figures[0].top + 45;
  175.     point[2].x = figures[0].left + 45; point[2].y = figures[0].top;
  176.     Polygon(hdc, point, 3);
  177.     SelectObject(hdc, OldBrush);
  178.     DeleteObject(Brush);
  179.     DeleteObject(OldBrush);
  180.  
  181.     Brush = CreateSolidBrush(0xFFFFFF);
  182.     OldBrush = (HBRUSH)SelectObject(hdc, Brush);
  183.  
  184.     point[0].x = figures[1].left; point[0].y = figures[1].top;
  185.     point[1].x = figures[1].left + (45 / 2); point[1].y = figures[1].top + 45;
  186.     point[2].x = figures[1].left + 45; point[2].y = figures[1].top;
  187.     Polygon(hdc, point, 3);
  188.     SelectObject(hdc, OldBrush);
  189.     DeleteObject(Brush);
  190.     DeleteObject(OldBrush);
  191.  
  192.     Brush = CreateSolidBrush(0x000000);
  193.     OldBrush = (HBRUSH)SelectObject(hdc, Brush);
  194.  
  195.     Rectangle(hdc, figures[3].left + 5, figures[3].top + 5, figures[3].right - 5, figures[3].bottom - 5);
  196.     SelectObject(hdc, OldBrush);
  197.     DeleteObject(Brush);
  198.     DeleteObject(OldBrush);
  199.  
  200.     Brush = CreateSolidBrush(0x000000);
  201.     OldBrush = (HBRUSH)SelectObject(hdc, Brush);
  202.    
  203.     Ellipse(hdc, figures[2].left + 40, figures[2].top + 5, figures[2].right - 40, figures[3].bottom - 5);
  204.     SelectObject(hdc, OldBrush);
  205.     DeleteObject(Brush);
  206.     DeleteObject(OldBrush);
  207.     EndPaint(hWnd, &paint);
  208. }
  209.  
  210. bool KolizjaPrzybornik(RECT rect, LPARAM lParam)
  211. {
  212.     DWORD mouseX = LOWORD(lParam);
  213.     DWORD mouseY = HIWORD(lParam);
  214.  
  215.     if (mouseX > rect.left && mouseX < rect.right && mouseY < rect.bottom && mouseY > rect.top)
  216.         return true;
  217.  
  218.     return false;
  219. }
  220.  
  221. bool KolizjaKwadraty(RECT rect, LPARAM lParam) {
  222.     DWORD mouseX = LOWORD(lParam);
  223.     DWORD mouseY = HIWORD(lParam);
  224.  
  225.     if (mouseX > rect.left  && mouseX < rect.right && mouseY < rect.bottom && mouseY > rect.top)
  226.         return true;
  227.  
  228.     return false;
  229. }
  230.  
  231. void Ruszaj(HWND hWnd ,LPARAM lParam) {
  232.     if (!KolizjaPrzybornik(przybornik, lParam))
  233.     {
  234.         if (rysuj)
  235.         {
  236.             HDC hdc = GetDC(hWnd);
  237.  
  238.             HBRUSH Brush, OldBrush;
  239.             HPEN Pen, OldPen;
  240.  
  241.             Brush = CreateSolidBrush(color);
  242.             Pen = CreatePen(PS_SOLID, 1, color);
  243.  
  244.             OldBrush = (HBRUSH)SelectObject(hdc, Brush);
  245.             OldPen = (HPEN)SelectObject(hdc, Pen);
  246.  
  247.             switch (figure)
  248.             {
  249.             case TRIANGLE:
  250.             {
  251.                 Brush = CreateSolidBrush(0xFFFFFF);
  252.                 OldBrush = (HBRUSH)SelectObject(hdc, Brush);
  253.  
  254.                 POINT point[3];
  255.                 point[0].x = LOWORD(lParam); point[0].y = HIWORD(lParam);
  256.                 point[1].x = LOWORD(lParam) + (10 / 2); point[1].y = HIWORD(lParam) - 10;
  257.                 point[2].x = LOWORD(lParam) + 10; point[2].y = HIWORD(lParam);
  258.                 Polygon(hdc, point, 3);
  259.             }
  260.             break;
  261.             case FILL_TRIANGLE:
  262.             {
  263.                 POINT point[3];
  264.                 point[0].x = LOWORD(lParam); point[0].y = HIWORD(lParam);
  265.                 point[1].x = LOWORD(lParam) + (10 / 2); point[1].y = HIWORD(lParam) - 10;
  266.                 point[2].x = LOWORD(lParam) + 10; point[2].y = HIWORD(lParam);
  267.                 Polygon(hdc, point, 3);
  268.             }
  269.             break;
  270.             case SQUARE:
  271.                 Rectangle(hdc, LOWORD(lParam), HIWORD(lParam), LOWORD(lParam) + 10, HIWORD(lParam) + 10);
  272.                 break;
  273.             case CIRCLE:
  274.                 Ellipse(hdc, LOWORD(lParam), HIWORD(lParam), LOWORD(lParam) + 10, HIWORD(lParam) + 10);
  275.                 break;
  276.             }
  277.  
  278.             SelectObject(hdc, OldBrush);
  279.             SelectObject(hdc, OldPen);
  280.  
  281.             DeleteObject(Brush);
  282.             DeleteObject(OldBrush);
  283.             DeleteObject(Pen);
  284.             DeleteObject(OldPen);
  285.  
  286.             ReleaseDC(hWnd, hdc);
  287.         }
  288.     }
  289.     else
  290.     {
  291.         if (rysuj)
  292.         {
  293.             for(int i = 0; i < 16; i++)
  294.                 if (KolizjaKwadraty(colours[i].rect, lParam))
  295.                 {
  296.                     color = colours[i].color;
  297.                     break;
  298.                 }
  299.             for(int i = 0; i < 4; i++)
  300.                 if (KolizjaKwadraty(figures[i], lParam))
  301.                 {
  302.                     switch (i)
  303.                     {
  304.                     case 0:
  305.                         figure = FILL_TRIANGLE;
  306.                         break;
  307.                     case 1:
  308.                         figure = TRIANGLE;
  309.                         break;
  310.                     case 2:
  311.                         figure = CIRCLE;
  312.                         break;
  313.                     case 3:
  314.                         figure = SQUARE;
  315.                         break;
  316.                     }
  317.                     break;
  318.                 }
  319.         }
  320.     }
  321. }
  322.  
  323.  
  324.  
  325.  
  326. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCMD)
  327. {
  328.     WNDCLASSEX window;
  329.  
  330.     if (!StworzOkno(window))
  331.         return 0;
  332.  
  333.     HWND uchwytOkna = 0;
  334.  
  335.     uchwytOkna = CreateWindowEx(
  336.         WS_EX_WINDOWEDGE,
  337.         L"StandardoweOkno",
  338.         L"Pierwsze okno",
  339.         WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  340.         CW_USEDEFAULT,
  341.         CW_USEDEFAULT,
  342.         1024,
  343.         768,
  344.         NULL,
  345.         NULL,
  346.         hInstance,
  347.         NULL);
  348.  
  349.     ShowWindow(uchwytOkna, SW_NORMAL);
  350.     UpdateWindow(uchwytOkna);
  351.  
  352.     MSG msg;
  353.  
  354.     for (; ;)
  355.     {
  356.         if (0 != GetMessage(&msg, 0, 0, 0))
  357.         {
  358.             TranslateMessage(&msg);
  359.             DispatchMessage(&msg);
  360.         }
  361.         if (msg.message == WM_QUIT) break;
  362.     }
  363.  
  364.     return (int)msg.wParam;
  365. }
  366.  
  367. LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  368. {
  369.     switch (message)
  370.     {
  371.     case WM_CLOSE:
  372.         if (MessageBox(hWnd, L"Czy chcesz zakonczyc dzialanie programu?", L"Dzialanie programu", MB_YESNO | MB_ICONERROR) == IDNO)
  373.             break;
  374.     case WM_DESTROY:
  375.         PostQuitMessage(0);
  376.         break;
  377.     case WM_PAINT:
  378.         Rysuj(hWnd);
  379.         break;
  380.     case WM_MOUSEMOVE:
  381.         Ruszaj(hWnd ,lParam);
  382.         break;
  383.     case WM_LBUTTONDOWN:
  384.         rysuj = true;
  385.         SendMessage(hWnd, WM_MOUSEMOVE, wParam, lParam);
  386.         break;
  387.     case WM_LBUTTONUP:
  388.         rysuj = false;
  389.         break;
  390.     default:
  391.         return DefWindowProc(hWnd, message, wParam, lParam);
  392.     }
  393.     return 0;
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement