Facebook
From Miczeq, 7 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 271
  1. #include <Windows.h>
  2.  
  3. LPSTR className = TEXT("WindowClass");
  4.  
  5. MSG msg;
  6.  
  7. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  8.  
  9. HBRUSH colors[16];
  10.  
  11. HDC hdc;
  12.  
  13. struct ToolLocation
  14. {
  15.         POINT position;
  16.         POINT size;
  17. };
  18.  
  19. ToolLocation colorsLocation[16];
  20.  
  21. ToolLocation toolsLocation[4];
  22.  
  23. void drawRect(int x, int y, int width, int height, HBRUSH color);
  24. void drawCircle(int x, int y, int radius, HBRUSH color);
  25. void drawTriangle(int x, int y, int width, int height, HBRUSH color);
  26.  
  27. void prepareColors();
  28.  
  29. void drawColors();
  30.  
  31. void drawTools();
  32.  
  33. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  34. {
  35.         WNDCLASSEX wc;
  36.  
  37.         wc.cbSize = sizeof(WNDCLASSEX);
  38.         wc.style = 0;
  39.         wc.cbClsExtra = 0;
  40.         wc.cbWndExtra = 0;
  41.         wc.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(255, 255, 255));
  42.         wc.hInstance = hInstance;
  43.         wc.lpszClassName = className;
  44.         wc.lpszMenuName = NULL;
  45.         wc.lpfnWndProc = WndProc;
  46.         wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  47.         wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  48.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  49.  
  50.         if (!RegisterClassEx(&wc))
  51.         {
  52.                 MessageBox(NULL, TEXT("Nie udało się zarejestrować klasy okna"), TEXT("Błąd"), MB_OK | MB_ICONERROR);
  53.                 return 1;
  54.         }
  55.  
  56.         HWND hwnd = CreateWindowEx(0, className, TEXT("Paint"), WS_MINIMIZEBOX | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
  57.                 NULL, NULL, hInstance, NULL);
  58.  
  59.         if (hwnd == NULL)
  60.         {
  61.                 MessageBox(NULL, TEXT("Nie udało się utworzyć okna"), TEXT("Błąd"), MB_OK | MB_ICONERROR);
  62.                 return 1;
  63.         }
  64.  
  65.         ShowWindow(hwnd, nCmdShow);
  66.         UpdateWindow(hwnd);
  67.  
  68.         hdc = GetDC(hwnd);
  69.  
  70.         HPEN bluePen = CreatePen(PS_SOLID, 2, RGB(100, 100, 255));
  71.  
  72.         HBRUSH toolBG = CreateSolidBrush(RGB(150, 150, 150));
  73.         HBRUSH box = (HBRUSH)SelectObject(hdc, toolBG);
  74.         HPEN pio = (HPEN) SelectObject(hdc, bluePen);
  75.  
  76.         drawRect(570, 3, 210, 555, toolBG);
  77.  
  78.         prepareColors();
  79.         drawColors();
  80.  
  81.         drawTools();
  82.  
  83.         while (GetMessage(&msg, NULL, 0, 0))
  84.         {
  85.                 TranslateMessage(&msg);
  86.                 DispatchMessage(&msg);
  87.         }
  88.  
  89.         ReleaseDC(hwnd, hdc);
  90.  
  91.         return msg.wParam;
  92. }
  93.  
  94. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  95. {
  96.         static bool clicked = false;
  97.         static bool rClicked = false;
  98.         static HBRUSH selectedColor = CreateSolidBrush(RGB(0, 0, 0));
  99.         static int tool = 0;
  100.  
  101.         HPEN nullPen = CreatePen(PS_NULL, 0, RGB(0, 0, 0));
  102.         HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
  103.  
  104.         int x = LOWORD(lParam);
  105.         int y = HIWORD(lParam);
  106.  
  107.         switch (msg)
  108.         {
  109.                 case WM_CLOSE:
  110.                 {
  111.                         DestroyWindow(hwnd);
  112.                 }break;
  113.  
  114.                 case WM_DESTROY:
  115.                 {
  116.                         PostQuitMessage(0);
  117.                 }break;
  118.  
  119.                 case WM_LBUTTONDOWN:
  120.                 {
  121.                         clicked = true;
  122.                 }break;
  123.  
  124.                 case WM_LBUTTONUP:
  125.                 {
  126.                         clicked = false;
  127.                 }break;
  128.  
  129.                 case WM_MOUSEMOVE:
  130.                 {
  131.                         if (clicked && x < 570 - 30)
  132.                         {
  133.                                 SelectObject(hdc, nullPen);
  134.                                 switch (tool)
  135.                                 {
  136.                                         case 0:
  137.                                         {
  138.                                                 drawRect(x, y, 30, 30, selectedColor);
  139.                                         }break;
  140.  
  141.                                         case 1:
  142.                                         {
  143.                                                 drawCircle(x, y, 30, selectedColor);
  144.                                         }break;
  145.  
  146.                                         case 2:
  147.                                         {
  148.                                                 drawTriangle(x, y, 30, 30, selectedColor);
  149.                                         }break;
  150.  
  151.                                         case 3:
  152.                                         {
  153.                                                 //SelectObject(hdc, blackPen);
  154.                                                 drawTriangle(x, y, 30, 30, colors[15]);
  155.                                         }break;
  156.                                 }
  157.                         }
  158.                 }break;
  159.  
  160.                 case WM_RBUTTONDOWN:
  161.                 {
  162.                         rClicked = true;
  163.                         for (int i = 0; i < 16; i++)
  164.                         {
  165.                                 if (x > colorsLocation[i].position.x && x < colorsLocation[i].position.x + colorsLocation[i].size.x &&
  166.                                         y > colorsLocation[i].position.y && y < colorsLocation[i].position.y + colorsLocation[i].size.y && rClicked && y < 400 )
  167.                                 {
  168.                                         selectedColor = colors[i];
  169.                                 }
  170.                         }
  171.  
  172.                         for (int i = 0; i < 4; i++)
  173.                         {
  174.                                 if (x > toolsLocation[i].position.x && x < toolsLocation[i].position.x + toolsLocation[i].size.x &&
  175.                                         y > toolsLocation[i].position.y && y < toolsLocation[i].position.y + toolsLocation[i].size.y && rClicked)
  176.                                 {
  177.                                         tool = i;
  178.                                 }
  179.                         }
  180.                 }break;
  181.  
  182.                 case WM_RBUTTONUP:
  183.                 {
  184.                         rClicked = false;
  185.                 }break;
  186.  
  187.                 default:
  188.                 {
  189.                         return DefWindowProc(hwnd, msg, wParam, lParam);
  190.                 }break;
  191.         }
  192.  
  193.         return 0;
  194. }
  195.  
  196. void drawRect(int x, int y, int width, int height, HBRUSH color)
  197. {
  198.         SelectObject(hdc, color);
  199.         Rectangle(hdc, x, y, x + width, y + height);
  200. }
  201.  
  202. void drawCircle(int x, int y, int radius, HBRUSH color)
  203. {
  204.         SelectObject(hdc, color);
  205.         Ellipse(hdc, x, y, x + radius, y + radius);
  206. }
  207.  
  208. void drawTriangle(int x, int y, int width, int height, HBRUSH color)
  209. {
  210.         POINT vertex[3];
  211.  
  212.         vertex[0].x = x;
  213.         vertex[0].y = y;
  214.         vertex[1].x = x + width / 2;
  215.         vertex[1].y = y + height;
  216.         vertex[2].x = x + width;
  217.         vertex[2].y = y;
  218.  
  219.         SelectObject(hdc, color);
  220.         Polygon(hdc, vertex, 3);
  221. }
  222.  
  223. void prepareColors()
  224. {
  225.         colors[0] = CreateSolidBrush(RGB(0, 0, 0));
  226.         colors[1] = CreateSolidBrush(RGB(200, 200, 200));
  227.         colors[2] = CreateSolidBrush(RGB(255, 100, 100));
  228.         colors[3] = CreateSolidBrush(RGB(255, 0, 0));
  229.         colors[4] = CreateSolidBrush(RGB(150, 150, 0));
  230.         colors[5] = CreateSolidBrush(RGB(255, 255, 0));
  231.         colors[6] = CreateSolidBrush(RGB(100, 255, 100));
  232.         colors[7] = CreateSolidBrush(RGB(0, 255, 0));
  233.         colors[8] = CreateSolidBrush(RGB(100, 100, 255));
  234.         colors[9] = CreateSolidBrush(RGB(0, 0, 255));
  235.         colors[10] = CreateSolidBrush(RGB(150, 0, 150));
  236.         colors[11] = CreateSolidBrush(RGB(255, 0, 255));
  237.         colors[12] = CreateSolidBrush(RGB(0, 150, 150));
  238.         colors[13] = CreateSolidBrush(RGB(0, 255, 255));
  239.         colors[14] = CreateSolidBrush(RGB(120, 120, 120));
  240.         colors[15] = CreateSolidBrush(RGB(255, 255, 255));
  241. }
  242.  
  243. int x = 630;
  244. int y = 10;
  245. int width = 41;
  246. int height = 41;
  247. int spaceX = 20;
  248. int spaceY = 10;
  249.  
  250. void drawColors()
  251. {
  252.         for (int i = 1; i <= 16; i++)
  253.         {
  254.                 colorsLocation[i - 1].position.x = x;
  255.                 colorsLocation[i - 1].position.y = y;
  256.                 colorsLocation[i - 1].size.x = x + width;
  257.                 colorsLocation[i - 1].size.y = y + height;
  258.  
  259.                 drawRect(x, y, width, height, colors[i - 1]);
  260.  
  261.                 x += width + spaceX;
  262.  
  263.                 if (i % 2 == 0)
  264.                 {
  265.                         y += height + spaceY;
  266.                         x = 630;
  267.                 }
  268.         }
  269. }
  270.  
  271. void drawTools()
  272. {
  273.         y += 40;
  274.  
  275.         for (int i = 1; i <= 4; i++)
  276.         {
  277.                 toolsLocation[i - 1].position.x = x;
  278.                 toolsLocation[i - 1].position.y = y;
  279.                 toolsLocation[i - 1].size.x = x + width;
  280.                 toolsLocation[i - 1].size.y = y + height;
  281.  
  282.                 drawRect(x, y, width, height, CreateSolidBrush(RGB(255, 255, 255)));
  283.  
  284.                 x += width + spaceX;
  285.  
  286.                 if (i % 2 == 0)
  287.                 {
  288.                         y += height + spaceY;
  289.                         x = 630;
  290.                 }
  291.         }
  292.  
  293.         drawRect(toolsLocation[0].position.x + 5, toolsLocation[0].position.y + 5, width - 10, height - 10, colors[0]);
  294.         drawCircle(toolsLocation[1].position.x + 5, toolsLocation[1].position.y + 5, width - 10, colors[0]);
  295.         drawTriangle(toolsLocation[2].position.x + 5, toolsLocation[2].position.y + 5, width - 10, height - 10, colors[0]);
  296.         HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
  297.         SelectObject(hdc, blackPen);
  298.         drawTriangle(toolsLocation[3].position.x + 5, toolsLocation[3].position.y + 5, width - 10, height - 10, colors[15]);
  299. }