peterzig

[PIU] WinAPI Paint

Oct 25th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.12 KB | None | 0 0
  1. #define SCREEN_WIDTH 600
  2. #define SCREEN_HEIGHT 600
  3. #define COLOURS 4
  4. #define RECTANGLE_SIZE 20
  5.  
  6. #include <Windows.h>
  7. #include <stdbool.h>
  8.  
  9. MSG msg;
  10. TCHAR className[] = TEXT("Simple Class");
  11. HWND hwnd;
  12.  
  13. unsigned int red = 0, green = 0, blue = 0;
  14. unsigned int pixelSize = 10;
  15. bool paintingLeft = false;
  16. bool paintingRight = false;
  17.  
  18. RECT rect[COLOURS];
  19.  
  20. void initColours() {
  21.     int squareSize = 50;
  22.     int y = 10;
  23.  
  24.     for (int i = 0; i < COLOURS; i++) {
  25.         int x = ((10 + squareSize) * i) + 5;
  26.  
  27.         rect[i].left = x;
  28.         rect[i].top = y;
  29.         rect[i].right = x + squareSize;
  30.         rect[i].bottom = y + squareSize;
  31.     }
  32. }
  33.  
  34. bool checkCollision(LPARAM lParam) {
  35.     int mouseX = LOWORD(lParam);
  36.     int mouseY = HIWORD(lParam);
  37.  
  38.     for (int i = 0; i < COLOURS; i++) {
  39.         if (mouseX > rect[i].left && mouseX < rect[i].right && mouseY < rect[i].bottom && mouseY > rect[i].top) {
  40.             switch (i) {
  41.             case 0:
  42.                 red = 255; green = blue = 0;
  43.                 break;
  44.  
  45.             case 1:
  46.                 red = blue = 0; green = 255;
  47.                 break;
  48.  
  49.             case 2:
  50.                 red = green = 0; blue = 255;
  51.                 break;
  52.  
  53.             case 3:
  54.                 red = green = blue = 0;
  55.                 break;
  56.             }
  57.  
  58.             return false;
  59.         }
  60.     }
  61.  
  62.     return true;
  63. }
  64.  
  65. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  66. {
  67.     PAINTSTRUCT ps;
  68.  
  69.     switch (msg)
  70.     {
  71.     case WM_PAINT: {
  72.         HDC hdc = BeginPaint(hwnd, &ps);
  73.  
  74.         HBRUSH pudelko, pedzel;
  75.         pedzel = CreateSolidBrush(RGB(255, 0, 0));
  76.         pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  77.  
  78.         Rectangle(hdc, rect[0].left, rect[0].top, rect[0].right, rect[0].bottom);
  79.         SelectObject(hdc, pudelko);
  80.  
  81.         /////////////////////////////////////////
  82.  
  83.         pedzel = CreateSolidBrush(RGB(0, 255, 0));
  84.         pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  85.  
  86.         Rectangle(hdc, rect[1].left, rect[1].top, rect[1].right, rect[1].bottom);
  87.         SelectObject(hdc, pudelko);
  88.  
  89.         /////////////////////////////////////////
  90.  
  91.         pedzel = CreateSolidBrush(RGB(0, 0, 255));
  92.         pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  93.  
  94.         Rectangle(hdc, rect[2].left, rect[2].top, rect[2].right, rect[2].bottom);
  95.         SelectObject(hdc, pudelko);
  96.  
  97.         /////////////////////////////////////////
  98.  
  99.         pedzel = CreateSolidBrush(RGB(0, 0, 0));
  100.         pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  101.  
  102.         Rectangle(hdc, rect[3].left, rect[3].top, rect[3].right, rect[3].bottom);
  103.         SelectObject(hdc, pudelko);
  104.  
  105.         EndPaint(hwnd, &ps);
  106.     }break;
  107.  
  108.     case WM_MOUSEMOVE:
  109.         if (checkCollision(lParam))
  110.             if (paintingLeft) {
  111.                 HDC hdc = GetDC(hwnd);
  112.  
  113.                 for (int i = 0; i < RECTANGLE_SIZE; i++)
  114.                     for (int j = 0; j < RECTANGLE_SIZE; j++)
  115.                         SetPixel(hdc, LOWORD(lParam) + j, HIWORD(lParam) + i, RGB(red, green, blue));
  116.  
  117.                 ReleaseDC(hwnd, hdc);
  118.             }
  119.             else if (paintingRight) {
  120.                 HDC hdc = GetDC(hwnd);
  121.  
  122.                 HBRUSH pudelko, pedzel;
  123.                 HPEN box, pen;
  124.                 pedzel = CreateSolidBrush(RGB(red, green, blue));
  125.                 pudelko = (HBRUSH)SelectObject(hdc, pedzel);
  126.  
  127.                 pen = CreatePen(PS_SOLID, 1, RGB(red, green, blue));
  128.                 box = (HPEN)SelectObject(hdc, pen);
  129.  
  130.                 POINT point[3];
  131.                 point[0].x = LOWORD(lParam); point[0].y = HIWORD(lParam);
  132.                 point[1].x = LOWORD(lParam) + (RECTANGLE_SIZE / 2); point[1].y = HIWORD(lParam) - RECTANGLE_SIZE;
  133.                 point[2].x = LOWORD(lParam) + RECTANGLE_SIZE; point[2].y = HIWORD(lParam);
  134.  
  135.                 Polygon(hdc, point, 3);
  136.                 SelectObject(hdc, pudelko);
  137.                 SelectObject(hdc, box);
  138.  
  139.                 ReleaseDC(hwnd, hdc);
  140.             }
  141.             break;
  142.  
  143.     case WM_LBUTTONDOWN: {
  144.         paintingLeft = true;
  145.  
  146.         SendMessage(hwnd, WM_MOUSEMOVE, wParam, lParam);
  147.     }break;
  148.  
  149.     case WM_LBUTTONUP:
  150.         paintingLeft = false;
  151.         break;
  152.  
  153.     case WM_RBUTTONUP:
  154.         paintingRight = false;
  155.         break;
  156.  
  157.     case WM_RBUTTONDOWN: {
  158.         paintingRight = true;
  159.  
  160.         SendMessage(hwnd, WM_MOUSEMOVE, wParam, lParam);
  161.     }break;
  162.  
  163.     case WM_DESTROY:
  164.         PostQuitMessage(0);
  165.         break;
  166.  
  167.     case WM_CLOSE:
  168.         DestroyWindow(hwnd);
  169.         break;
  170.  
  171.     default:
  172.         return DefWindowProc(hwnd, msg, wParam, lParam);
  173.     }
  174.  
  175.     return 0;
  176. }
  177.  
  178. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  179. {
  180.     WNDCLASSEX wc;
  181.     wc.cbSize = sizeof(WNDCLASSEX);
  182.     wc.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
  183.     wc.cbClsExtra = 0;
  184.     wc.cbWndExtra = 0;
  185.     wc.hInstance = hInstance;
  186.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  187.     wc.lpfnWndProc = WndProc;
  188.     wc.lpszClassName = className;
  189.     wc.lpszMenuName = NULL;
  190.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  191.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  192.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  193.  
  194.     initColours();
  195.  
  196.     if (!RegisterClassEx(&wc))
  197.     {
  198.         MessageBox(NULL, TEXT("Nie udało się zarejestrować klasy okna!"), TEXT("Błąd"), MB_OK | MB_ICONERROR);
  199.         return 1;
  200.     }
  201.  
  202.     hwnd = CreateWindowEx(0, className, TEXT("WinAPI Paint"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);
  203.     if (hwnd == NULL) {
  204.         MessageBox(NULL, TEXT("Nie udało się utworzyć okna 1!"), TEXT("CreateWindowEx"), MB_OK | MB_ICONERROR);
  205.         return 1;
  206.     }
  207.  
  208.     ShowWindow(hwnd, nCmdShow);
  209.     UpdateWindow(hwnd);
  210.  
  211.     while (GetMessage(&msg, NULL, 0, 0))
  212.     {
  213.         TranslateMessage(&msg);
  214.         DispatchMessage(&msg);
  215.     }
  216.  
  217.     UnregisterClass(className, hInstance);
  218.  
  219.     return msg.wParam;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment