Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define SCREEN_WIDTH 800
- #define SCREEN_HEIGHT 600
- #define COLOURS 24
- #define RECTANGLE_SIZE 30
- #define SQUARE_SIZE 50
- #include <Windows.h>
- #include <tchar.h>
- #include <stdbool.h>
- MSG msg;
- TCHAR className[] = TEXT("Simple Class");
- HWND hwnd;
- COLORREF color = RGB(0, 0, 0);
- RECT drawPanelRect;
- unsigned int pixelSize = 10;
- bool paintingLeft = false;
- enum FIGURE { TRIANGLE, FILL_TRIANGLE, CIRCLE, SQUARE };
- FIGURE figure = SQUARE;
- RECT figures[4];
- struct Colours {
- RECT rect;
- COLORREF color;
- };
- Colours colours[COLOURS];
- void initDrawPanelRect() {
- float x = SCREEN_WIDTH * 0.7f;
- RECT windowRect;
- GetClientRect(hwnd, &windowRect);
- drawPanelRect.left = SCREEN_WIDTH * 0.7f;
- drawPanelRect.top = 0;
- drawPanelRect.right = windowRect.right;
- drawPanelRect.bottom = windowRect.bottom;
- }
- void initFigures() {
- for (int i = 0; i < 4; i++) {
- figures[i].left = colours[20 + i].rect.left;
- figures[i].top = colours[23].rect.top + SQUARE_SIZE + 20;
- figures[i].right = colours[20 + i].rect.right;
- figures[i].bottom = colours[23].rect.bottom + SQUARE_SIZE + 20;
- }
- }
- void initColours() {
- int y = 10;
- int count = 0;
- for (int i = 0; i < COLOURS; i++) {
- int x = ((5 + SQUARE_SIZE) * count) + drawPanelRect.left + 5;
- colours[i].rect.left = x;
- colours[i].rect.top = y;
- colours[i].rect.right = x + SQUARE_SIZE;
- colours[i].rect.bottom = y + SQUARE_SIZE;
- count++;
- if (count % 4 == 0) {
- count = 0;
- y += (SQUARE_SIZE + 5);
- }
- }
- colours[0].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[1].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[2].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[3].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[4].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[5].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[6].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[7].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[8].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[9].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[10].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[11].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[12].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[13].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[14].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[15].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[16].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[17].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[18].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[19].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[20].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[21].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[22].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- colours[23].color = RGB(rand() % 255, rand() % 255, rand() % 255);
- }
- bool checkCollision(RECT rect, LPARAM lParam) {
- float mouseX = LOWORD(lParam);
- float mouseY = HIWORD(lParam);
- if (mouseX > rect.left && mouseX < rect.right && mouseY < rect.bottom && mouseY > rect.top)
- return true;
- return false;
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- PAINTSTRUCT ps;
- switch (msg)
- {
- case WM_PAINT: {
- HDC hdc = BeginPaint(hwnd, &ps);
- HBRUSH pudelko, pedzel;
- pedzel = CreateSolidBrush(RGB(128, 128, 128));
- pudelko = (HBRUSH)SelectObject(hdc, pedzel);
- Rectangle(hdc, drawPanelRect.left, drawPanelRect.top, drawPanelRect.right, drawPanelRect.bottom);
- DeleteObject(pudelko);
- DeleteObject(pedzel);
- for (int i = 0; i < COLOURS; i++) {
- pedzel = CreateSolidBrush(colours[i].color);
- pudelko = (HBRUSH)SelectObject(hdc, pedzel);
- Rectangle(hdc, colours[i].rect.left, colours[i].rect.top, colours[i].rect.right, colours[i].rect.bottom);
- SelectObject(hdc, pudelko);
- DeleteObject(pudelko);
- DeleteObject(pedzel);
- }
- for (int i = 0; i < 4; i++) {
- pedzel = CreateSolidBrush(RGB(255, 255, 255));
- pudelko = (HBRUSH)SelectObject(hdc, pedzel);
- Rectangle(hdc, figures[i].left, figures[i].top, figures[i].right, figures[i].bottom);
- SelectObject(hdc, pudelko);
- DeleteObject(pudelko);
- DeleteObject(pedzel);
- }
- pedzel = CreateSolidBrush(RGB(0, 0, 0));
- pudelko = (HBRUSH)SelectObject(hdc, pedzel);
- POINT point[3];
- point[0].x = figures[0].left + 10; point[0].y = figures[0].top + 10;
- point[1].x = figures[0].left + 10 + (RECTANGLE_SIZE / 2); point[1].y = figures[0].top + RECTANGLE_SIZE + 10;
- point[2].x = figures[0].left + 10 + RECTANGLE_SIZE; point[2].y = figures[0].top + 10;
- Polygon(hdc, point, 3);
- SelectObject(hdc, pudelko);
- pedzel = CreateSolidBrush(RGB(255, 255, 255));
- pudelko = (HBRUSH)SelectObject(hdc, pedzel);
- point[0].x = figures[1].left + 10; point[0].y = figures[1].top + 10;
- point[1].x = figures[1].left + 10 + (RECTANGLE_SIZE / 2); point[1].y = figures[1].top + RECTANGLE_SIZE + 10;
- point[2].x = figures[1].left + 10 + RECTANGLE_SIZE; point[2].y = figures[1].top + 10;
- Polygon(hdc, point, 3);
- SelectObject(hdc, pudelko);
- pedzel = CreateSolidBrush(RGB(0, 0, 0));
- pudelko = (HBRUSH)SelectObject(hdc, pedzel);
- Ellipse(hdc, figures[2].left + 10, figures[2].top + 10, figures[2].left + 40, figures[2].top + 40);
- SelectObject(hdc, pudelko);
- Rectangle(hdc, figures[3].left + 10, figures[3].top + 10, figures[3].left + 40, figures[3].top + 40);
- EndPaint(hwnd, &ps);
- }break;
- case WM_MOUSEMOVE:
- if (!checkCollision(drawPanelRect, lParam)) {
- if (paintingLeft) {
- HDC hdc = GetDC(hwnd);
- HBRUSH pudelko, pedzel;
- HPEN box, pen;
- pedzel = CreateSolidBrush(color);
- pen = CreatePen(PS_SOLID, 1, color);
- pudelko = (HBRUSH)SelectObject(hdc, pedzel);
- box = (HPEN)SelectObject(hdc, pen);
- switch (figure)
- {
- case TRIANGLE: {
- pedzel = CreateSolidBrush(RGB(255, 255, 255));
- pudelko = (HBRUSH)SelectObject(hdc, pedzel);
- POINT point[3];
- point[0].x = LOWORD(lParam); point[0].y = HIWORD(lParam);
- point[1].x = LOWORD(lParam) + (RECTANGLE_SIZE / 2); point[1].y = HIWORD(lParam) - RECTANGLE_SIZE;
- point[2].x = LOWORD(lParam) + RECTANGLE_SIZE; point[2].y = HIWORD(lParam);
- Polygon(hdc, point, 3);
- }break;
- case FILL_TRIANGLE: {
- POINT point[3];
- point[0].x = LOWORD(lParam); point[0].y = HIWORD(lParam);
- point[1].x = LOWORD(lParam) + (RECTANGLE_SIZE / 2); point[1].y = HIWORD(lParam) - RECTANGLE_SIZE;
- point[2].x = LOWORD(lParam) + RECTANGLE_SIZE; point[2].y = HIWORD(lParam);
- Polygon(hdc, point, 3);
- }break;
- case CIRCLE:
- Ellipse(hdc, LOWORD(lParam), HIWORD(lParam), LOWORD(lParam) + SQUARE_SIZE, HIWORD(lParam) + SQUARE_SIZE);
- break;
- case SQUARE:
- Rectangle(hdc, LOWORD(lParam), HIWORD(lParam), LOWORD(lParam) + SQUARE_SIZE, HIWORD(lParam) + SQUARE_SIZE);
- break;
- }
- SelectObject(hdc, pudelko);
- SelectObject(hdc, box);
- DeleteObject(pen);
- DeleteObject(pedzel);
- DeleteObject(pudelko);
- DeleteObject(box);
- ReleaseDC(hwnd, hdc);
- }
- }
- else {
- if (paintingLeft) {
- for (int i = 0; i < COLOURS; i++)
- if (checkCollision(colours[i].rect, lParam)) {
- color = colours[i].color;
- break;
- }
- for (int i = 0; i < 4; i++)
- if (checkCollision(figures[i], lParam)) {
- switch (i) {
- case 0:
- figure = FILL_TRIANGLE;
- break;
- case 1:
- figure = TRIANGLE;
- break;
- case 2:
- figure = CIRCLE;
- break;
- case 3:
- figure = SQUARE;
- break;
- }
- break;
- }
- }
- }
- break;
- case WM_LBUTTONDOWN: {
- paintingLeft = true;
- SendMessage(hwnd, WM_MOUSEMOVE, wParam, lParam);
- }break;
- case WM_LBUTTONUP:
- paintingLeft = false;
- break;
- case WM_KEYDOWN:
- switch ((int)wParam)
- {
- case 0x43:
- InvalidateRect(hwnd, NULL, 1);
- break;
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_CLOSE:
- DestroyWindow(hwnd);
- break;
- default:
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- WNDCLASSEX wc;
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wc.lpfnWndProc = WndProc;
- wc.lpszClassName = className;
- wc.lpszMenuName = NULL;
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- if (!RegisterClassEx(&wc))
- {
- MessageBox(NULL, TEXT("Nie udało się zarejestrować klasy okna!"), TEXT("Błąd"), MB_OK | MB_ICONERROR);
- return 1;
- }
- 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);
- if (hwnd == NULL) {
- MessageBox(NULL, TEXT("Nie udało się utworzyć okna 1!"), TEXT("CreateWindowEx"), MB_OK | MB_ICONERROR);
- return 1;
- }
- initDrawPanelRect();
- initColours();
- initFigures();
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- UnregisterClass(className, hInstance);
- return msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement