Advertisement
Guest User

Untitled

a guest
Nov 13th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>   
  2.  
  3. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  4. static void OnWM_PAINT(const HWND hwnd);
  5. static void OnWM_LBUTTONDOWN(const HWND hwnd, LPARAM lParam, WPARAM wParam);
  6.  
  7. RECT rSize;
  8. TCHAR className[] = TEXT("Paint v1");
  9.  
  10. int colors[] = {
  11.     0x000000,
  12.     0xFFFFFF,
  13.     0x7F7F7F,
  14.     0xFF0000,
  15.     0xFF7F00,
  16.     0xFF7F7F,
  17.     0xFF007F,
  18.     0xFF00FF,
  19.     0XFFFF00,
  20.     0x7F0000,
  21.     0x7F7F00,
  22.     0x7F007F,
  23.     0x7FFF00,
  24.     0x7F00FF,
  25.     0x007F00,
  26.     0x00FFFF,
  27.     0x00FF00,
  28. };
  29.  
  30. enum Type {
  31.     color,
  32.     rectangle,
  33.     elipse,
  34.     triangle
  35. };
  36.  
  37. class Button {
  38.     POINT leftTop;
  39.     POINT rightBottom;
  40.     Type type;
  41.  
  42.     int nColor;
  43.  
  44. public:
  45.     Button() {
  46.         leftTop.x = 0;
  47.         leftTop.y = 0;
  48.         rightBottom.x = 0;
  49.         rightBottom.y = 0;
  50.         type = color;
  51.         nColor = colors[0];
  52.     }
  53.  
  54.     Button(int nXL, int nYL, int nXR, int nYR, Type type, int nColor) {
  55.         this->leftTop.x = nXL;
  56.         this->leftTop.y = nYL;
  57.         this->rightBottom.x = nXR;
  58.         this->rightBottom.y = nYR;
  59.         this->type = type;
  60.         this->nColor = nColor;
  61.     }
  62.  
  63.     inline const Type returnType() const { return type; }
  64.     inline const int returnColor() const { return nColor; }
  65.     bool posComare(const int nX, const int nY) const {
  66.         if (nX <= leftTop.x && nX <= rightBottom.x &&
  67.             nY >= leftTop.y && nY <= rightBottom.y)
  68.             return true;
  69.  
  70.         return false;
  71.     }
  72.     void drawButton(HDC hdc) {
  73.         HBRUSH brush = CreateSolidBrush(this->nColor);
  74.         HBRUSH defaultBrush = (HBRUSH)SelectObject(hdc, brush);
  75.         Rectangle(hdc, leftTop.x, leftTop.y, rightBottom.x, rightBottom.y);
  76.         SelectObject(hdc, defaultBrush);
  77.         DeleteObject(brush);
  78.     }
  79. } buttons[16];
  80.  
  81. int WINAPI WinMain(HINSTANCE hInstance,
  82.     HINSTANCE hPrevInstance,
  83.     LPSTR lpCmdLine,
  84.     int nCmdShow)
  85. {
  86.     WNDCLASSEX wc;
  87.  
  88.     wc.cbClsExtra = 0;
  89.     wc.cbSize = sizeof(WNDCLASSEX);
  90.     wc.cbWndExtra = 0;
  91.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  92.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  93.     wc.hIcon = 0;
  94.     wc.hIconSm = 0;
  95.     wc.hInstance = hInstance;
  96.     wc.lpfnWndProc = WndProc;
  97.     wc.lpszClassName = className;
  98.     wc.lpszMenuName = 0;
  99.     wc.style = 0;
  100.  
  101.     if (!RegisterClassEx(&wc)) {
  102.         MessageBox(NULL, TEXT("Błąd przy rejestracji okna!"), TEXT("Błąd!"), MB_OK | MB_ICONERROR);
  103.         return 1;
  104.     }
  105.  
  106.     HWND hwnd = CreateWindowEx(0, className, TEXT("Paint"), WS_OVERLAPPEDWINDOW, 50, 50, 500, 500, 0, 0, hInstance, 0);
  107.  
  108.     ShowWindow(hwnd, SW_SHOW);
  109.     UpdateWindow(hwnd);
  110.  
  111.     MSG msg;
  112.  
  113.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  114.         TranslateMessage(&msg);
  115.         DispatchMessage(&msg);
  116.     }
  117.  
  118.     UnregisterClass(className, hInstance);
  119.  
  120.     return msg.wParam;
  121. }
  122.  
  123. static void OnWM_LBUTTONDOWN(const HWND hwnd, LPARAM lParam, WPARAM wParam) {
  124.     POINT pos;
  125.  
  126.     pos.x = LOWORD(lParam);
  127.     pos.y = HIWORD(lParam);
  128.  
  129.     int actualColor;
  130.  
  131.     for (int i = 0; i < 16; ++i) {
  132.         if (buttons[i].posComare(pos.x, pos.y) == true)
  133.             actualColor = buttons[i].returnColor();
  134.     }
  135.  
  136.     if (pos.x < rSize.right - 150) {
  137.         HDC hdc = GetDC(hwnd);
  138.         HBRUSH brush = CreateSolidBrush(actualColor);
  139.         HBRUSH defaultBrush = (HBRUSH)SelectObject(hdc, brush);
  140.  
  141.         Rectangle(hdc, pos.x, pos.y, pos.x + 20, pos.y + 20);
  142.  
  143.         SelectObject(hdc, defaultBrush);
  144.         DeleteObject(brush);
  145.         ReleaseDC(hwnd, hdc);
  146.     }
  147.  
  148.    
  149.        
  150. }
  151.  
  152. static void OnWM_PAINT(const HWND hwnd) {
  153.     PAINTSTRUCT ps;
  154.     HDC hdc;
  155.  
  156.     HBRUSH brush1 = CreateSolidBrush(RGB(54, 69, 79));
  157.     hdc = BeginPaint(hwnd, &ps);
  158.  
  159.     HBRUSH defaultBrush = (HBRUSH)SelectObject(hdc, brush1);
  160.  
  161.     GetClientRect(hwnd, &rSize);
  162.     Rectangle(hdc, rSize.right - 150, rSize.top, rSize.right, rSize.bottom);
  163.  
  164.     for (int i = 0; i < 16; ++i) {
  165.         if (i % 2 == 1)
  166.             buttons[i] = Button(rSize.right - 95, rSize.top + (i * 20), rSize.right - 125, rSize.top + 30 + (i * 20), color, colors[i]);
  167.         else
  168.             buttons[i] = Button(rSize.right - 30, rSize.top + 20 + (i * 20), rSize.right - 60, rSize.top + 50 + (i * 20), color, colors[i]);
  169.     }
  170.  
  171.     for (int i = 0; i < 16; ++i)
  172.         buttons[i].drawButton(hdc);
  173.  
  174.     SelectObject(hdc, defaultBrush);
  175.     EndPaint(hwnd, &ps);
  176.  
  177.     DeleteObject(brush1);
  178.  
  179. }
  180.  
  181. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  182.     switch (msg) {
  183.         case WM_PAINT:
  184.             OnWM_PAINT(hwnd); break;
  185.  
  186.         case WM_LBUTTONDOWN:
  187.             OnWM_LBUTTONDOWN(hwnd, lParam, wParam); break;
  188.            
  189.  
  190.         case WM_DESTROY:
  191.             PostQuitMessage(0); break;
  192.  
  193.         default:
  194.             return DefWindowProc(hwnd, msg, wParam, lParam);
  195.     }
  196.  
  197.     return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement