Advertisement
peterzig

[PIU] Paint v2

Oct 26th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.65 KB | None | 0 0
  1. #define SCREEN_WIDTH 800
  2. #define SCREEN_HEIGHT 600
  3. #define COLOURS 24
  4. #define RECTANGLE_SIZE 30
  5. #define SQUARE_SIZE 50
  6.  
  7. #include <Windows.h>
  8. #include <tchar.h>
  9. #include <stdbool.h>
  10.  
  11. MSG msg;
  12. TCHAR className[] = TEXT("Simple Class");
  13. HWND hwnd;
  14. COLORREF color = RGB(0, 0, 0);
  15. RECT drawPanelRect;
  16.  
  17. unsigned int pixelSize = 10;
  18. bool paintingLeft = false;
  19.  
  20. enum FIGURE { TRIANGLE, FILL_TRIANGLE, CIRCLE, SQUARE };
  21. FIGURE figure = SQUARE;
  22.  
  23. RECT figures[4];
  24.  
  25. struct Colours {
  26.     RECT rect;
  27.     COLORREF color;
  28. };
  29.  
  30. Colours colours[COLOURS];
  31.  
  32. void initDrawPanelRect() {
  33.     float x = SCREEN_WIDTH * 0.7f;
  34.  
  35.     RECT windowRect;
  36.     GetClientRect(hwnd, &windowRect);
  37.  
  38.     drawPanelRect.left = SCREEN_WIDTH * 0.7f;
  39.     drawPanelRect.top = 0;
  40.     drawPanelRect.right = windowRect.right;
  41.     drawPanelRect.bottom = windowRect.bottom;
  42. }
  43.  
  44. void initFigures() {
  45.     for (int i = 0; i < 4; i++) {
  46.         figures[i].left = colours[20 + i].rect.left;
  47.         figures[i].top = colours[23].rect.top + SQUARE_SIZE + 20;
  48.         figures[i].right = colours[20 + i].rect.right;
  49.         figures[i].bottom = colours[23].rect.bottom + SQUARE_SIZE + 20;
  50.     }
  51. }
  52.  
  53. void initColours() {
  54.     int y = 10;
  55.     int count = 0;
  56.  
  57.     for (int i = 0; i < COLOURS; i++) {
  58.         int x = ((5 + SQUARE_SIZE) * count) + drawPanelRect.left + 5;
  59.  
  60.         colours[i].rect.left = x;
  61.         colours[i].rect.top = y;
  62.         colours[i].rect.right = x + SQUARE_SIZE;
  63.         colours[i].rect.bottom = y + SQUARE_SIZE;
  64.  
  65.         count++;
  66.  
  67.         if (count % 4 == 0) {
  68.             count = 0;
  69.             y += (SQUARE_SIZE + 5);
  70.         }
  71.     }
  72.  
  73.     colours[0].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  74.     colours[1].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  75.     colours[2].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  76.     colours[3].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  77.     colours[4].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  78.     colours[5].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  79.     colours[6].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  80.     colours[7].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  81.     colours[8].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  82.     colours[9].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  83.     colours[10].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  84.     colours[11].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  85.     colours[12].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  86.     colours[13].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  87.     colours[14].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  88.     colours[15].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  89.     colours[16].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  90.     colours[17].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  91.     colours[18].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  92.     colours[19].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  93.     colours[20].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  94.     colours[21].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  95.     colours[22].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  96.     colours[23].color = RGB(rand() % 255, rand() % 255, rand() % 255);
  97. }
  98.  
  99. bool checkCollision(RECT rect, LPARAM lParam) {
  100.     float mouseX = LOWORD(lParam);
  101.     float mouseY = HIWORD(lParam);
  102.  
  103.     if (mouseX > rect.left && mouseX < rect.right && mouseY < rect.bottom && mouseY > rect.top)
  104.         return true;
  105.  
  106.     return false;
  107. }
  108.  
  109. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  110. {
  111.     PAINTSTRUCT ps;
  112.  
  113.     switch (msg)
  114.     {
  115.     case WM_PAINT: {
  116.         HDC hdc = BeginPaint(hwnd, &ps);
  117.  
  118.         HBRUSH pudelko, pedzel;
  119.  
  120.         pedzel = CreateSolidBrush(RGB(128, 128, 128));
  121.         pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  122.  
  123.         Rectangle(hdc, drawPanelRect.left, drawPanelRect.top, drawPanelRect.right, drawPanelRect.bottom);
  124.  
  125.         DeleteObject(pudelko);
  126.         DeleteObject(pedzel);
  127.  
  128.         for (int i = 0; i < COLOURS; i++) {
  129.             pedzel = CreateSolidBrush(colours[i].color);
  130.             pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  131.  
  132.             Rectangle(hdc, colours[i].rect.left, colours[i].rect.top, colours[i].rect.right, colours[i].rect.bottom);
  133.             SelectObject(hdc, pudelko);
  134.  
  135.             DeleteObject(pudelko);
  136.             DeleteObject(pedzel);
  137.         }
  138.  
  139.         for (int i = 0; i < 4; i++) {
  140.             pedzel = CreateSolidBrush(RGB(255, 255, 255));
  141.             pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  142.  
  143.             Rectangle(hdc, figures[i].left, figures[i].top, figures[i].right, figures[i].bottom);
  144.             SelectObject(hdc, pudelko);
  145.  
  146.             DeleteObject(pudelko);
  147.             DeleteObject(pedzel);
  148.         }
  149.  
  150.         pedzel = CreateSolidBrush(RGB(0, 0, 0));
  151.         pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  152.  
  153.         POINT point[3];
  154.         point[0].x = figures[0].left + 10; point[0].y = figures[0].top + 10;
  155.         point[1].x = figures[0].left + 10 + (RECTANGLE_SIZE / 2); point[1].y = figures[0].top + RECTANGLE_SIZE + 10;
  156.         point[2].x = figures[0].left + 10 + RECTANGLE_SIZE; point[2].y = figures[0].top + 10;
  157.         Polygon(hdc, point, 3);
  158.         SelectObject(hdc, pudelko);
  159.  
  160.         pedzel = CreateSolidBrush(RGB(255, 255, 255));
  161.         pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  162.  
  163.         point[0].x = figures[1].left + 10; point[0].y = figures[1].top + 10;
  164.         point[1].x = figures[1].left + 10 + (RECTANGLE_SIZE / 2); point[1].y = figures[1].top + RECTANGLE_SIZE + 10;
  165.         point[2].x = figures[1].left + 10 + RECTANGLE_SIZE; point[2].y = figures[1].top + 10;
  166.         Polygon(hdc, point, 3);
  167.         SelectObject(hdc, pudelko);
  168.  
  169.         pedzel = CreateSolidBrush(RGB(0, 0, 0));
  170.         pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  171.         Ellipse(hdc, figures[2].left + 10, figures[2].top + 10, figures[2].left + 40, figures[2].top + 40);
  172.         SelectObject(hdc, pudelko);
  173.  
  174.         Rectangle(hdc, figures[3].left + 10, figures[3].top + 10, figures[3].left + 40, figures[3].top + 40);
  175.  
  176.         EndPaint(hwnd, &ps);
  177.     }break;
  178.  
  179.     case WM_MOUSEMOVE:
  180.         if (!checkCollision(drawPanelRect, lParam)) {
  181.             if (paintingLeft) {
  182.                 HDC hdc = GetDC(hwnd);
  183.  
  184.                 HBRUSH pudelko, pedzel;
  185.                 HPEN box, pen;
  186.  
  187.                 pedzel = CreateSolidBrush(color);
  188.                 pen = CreatePen(PS_SOLID, 1, color);
  189.  
  190.                 pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  191.                 box = (HPEN)SelectObject(hdc, pen);
  192.  
  193.                 switch (figure)
  194.                 {
  195.                 case TRIANGLE: {
  196.                     pedzel = CreateSolidBrush(RGB(255, 255, 255));
  197.                     pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  198.  
  199.                     POINT point[3];
  200.                     point[0].x = LOWORD(lParam); point[0].y = HIWORD(lParam);
  201.                     point[1].x = LOWORD(lParam) + (RECTANGLE_SIZE / 2); point[1].y = HIWORD(lParam) - RECTANGLE_SIZE;
  202.                     point[2].x = LOWORD(lParam) + RECTANGLE_SIZE; point[2].y = HIWORD(lParam);
  203.  
  204.                     Polygon(hdc, point, 3);
  205.                 }break;
  206.  
  207.                 case FILL_TRIANGLE: {
  208.                     POINT point[3];
  209.                     point[0].x = LOWORD(lParam); point[0].y = HIWORD(lParam);
  210.                     point[1].x = LOWORD(lParam) + (RECTANGLE_SIZE / 2); point[1].y = HIWORD(lParam) - RECTANGLE_SIZE;
  211.                     point[2].x = LOWORD(lParam) + RECTANGLE_SIZE; point[2].y = HIWORD(lParam);
  212.  
  213.                     Polygon(hdc, point, 3);
  214.                 }break;
  215.  
  216.                 case CIRCLE:
  217.                     Ellipse(hdc, LOWORD(lParam), HIWORD(lParam), LOWORD(lParam) + SQUARE_SIZE, HIWORD(lParam) + SQUARE_SIZE);
  218.                     break;
  219.  
  220.                 case SQUARE:
  221.                     Rectangle(hdc, LOWORD(lParam), HIWORD(lParam), LOWORD(lParam) + SQUARE_SIZE, HIWORD(lParam) + SQUARE_SIZE);
  222.                     break;
  223.                 }
  224.  
  225.                 SelectObject(hdc, pudelko);
  226.                 SelectObject(hdc, box);
  227.  
  228.                 DeleteObject(pen);
  229.                 DeleteObject(pedzel);
  230.                 DeleteObject(pudelko);
  231.                 DeleteObject(box);
  232.  
  233.                 ReleaseDC(hwnd, hdc);
  234.             }
  235.         }
  236.         else {
  237.             if (paintingLeft) {
  238.                 for (int i = 0; i < COLOURS; i++)
  239.                     if (checkCollision(colours[i].rect, lParam)) {
  240.                         color = colours[i].color;
  241.  
  242.                         break;
  243.                     }
  244.  
  245.                 for (int i = 0; i < 4; i++)
  246.                     if (checkCollision(figures[i], lParam)) {
  247.                         switch (i) {
  248.                         case 0:
  249.                             figure = FILL_TRIANGLE;
  250.                             break;
  251.  
  252.                         case 1:
  253.                             figure = TRIANGLE;
  254.                             break;
  255.  
  256.                         case 2:
  257.                             figure = CIRCLE;
  258.                             break;
  259.  
  260.                         case 3:
  261.                             figure = SQUARE;
  262.                             break;
  263.                         }
  264.  
  265.                         break;
  266.                     }
  267.             }
  268.         }
  269.         break;
  270.  
  271.     case WM_LBUTTONDOWN: {
  272.         paintingLeft = true;
  273.  
  274.         SendMessage(hwnd, WM_MOUSEMOVE, wParam, lParam);
  275.     }break;
  276.  
  277.     case WM_LBUTTONUP:
  278.         paintingLeft = false;
  279.         break;
  280.  
  281.     case WM_KEYDOWN:
  282.         switch ((int)wParam)
  283.         {
  284.         case 0x43:
  285.             InvalidateRect(hwnd, NULL, 1);
  286.             break;
  287.         }
  288.         break;
  289.  
  290.     case WM_DESTROY:
  291.         PostQuitMessage(0);
  292.         break;
  293.  
  294.     case WM_CLOSE:
  295.         DestroyWindow(hwnd);
  296.         break;
  297.  
  298.     default:
  299.         return DefWindowProc(hwnd, msg, wParam, lParam);
  300.     }
  301.  
  302.     return 0;
  303. }
  304.  
  305. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  306. {
  307.     WNDCLASSEX wc;
  308.     wc.cbSize = sizeof(WNDCLASSEX);
  309.     wc.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
  310.     wc.cbClsExtra = 0;
  311.     wc.cbWndExtra = 0;
  312.     wc.hInstance = hInstance;
  313.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  314.     wc.lpfnWndProc = WndProc;
  315.     wc.lpszClassName = className;
  316.     wc.lpszMenuName = NULL;
  317.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  318.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  319.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  320.  
  321.     if (!RegisterClassEx(&wc))
  322.     {
  323.         MessageBox(NULL, TEXT("Nie udało się zarejestrować klasy okna!"), TEXT("Błąd"), MB_OK | MB_ICONERROR);
  324.         return 1;
  325.     }
  326.  
  327.     hwnd = CreateWindowEx(0, className, TEXT("TU RYSUJ XD"), WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);
  328.     if (hwnd == NULL) {
  329.         MessageBox(NULL, TEXT("Nie udało się utworzyć okna 1!"), TEXT("CreateWindowEx"), MB_OK | MB_ICONERROR);
  330.         return 1;
  331.     }
  332.  
  333.     initDrawPanelRect();
  334.     initColours();
  335.     initFigures();
  336.  
  337.     ShowWindow(hwnd, nCmdShow);
  338.     UpdateWindow(hwnd);
  339.  
  340.     while (GetMessage(&msg, NULL, 0, 0))
  341.     {
  342.         TranslateMessage(&msg);
  343.         DispatchMessage(&msg);
  344.     }
  345.  
  346.     UnregisterClass(className, hInstance);
  347.  
  348.     return msg.wParam;
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement