#include <Windows.h>
LPSTR className = TEXT("WindowClass");
MSG msg;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
HBRUSH colors[16];
HDC hdc;
struct ToolLocation
{
POINT position;
POINT size;
};
ToolLocation colorsLocation[16];
ToolLocation toolsLocation[4];
void drawRect(int x, int y, int width, int height, HBRUSH color);
void drawCircle(int x, int y, int radius, HBRUSH color);
void drawTriangle(int x, int y, int width, int height, HBRUSH color);
void prepareColors();
void drawColors();
void drawTools();
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(255, 255, 255));
wc.hInstance = hInstance;
wc.lpszClassName = className;
wc.lpszMenuName = NULL;
wc.lpfnWndProc = WndProc;
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 hwnd = CreateWindowEx(0, className, TEXT("Paint"), WS_MINIMIZEBOX | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, TEXT("Nie udało się utworzyć okna"), TEXT("Błąd"), MB_OK | MB_ICONERROR);
return 1;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
hdc = GetDC(hwnd);
HPEN bluePen = CreatePen(PS_SOLID, 2, RGB(100, 100, 255));
HBRUSH toolBG = CreateSolidBrush(RGB(150, 150, 150));
HBRUSH box = (HBRUSH)SelectObject(hdc, toolBG);
HPEN pio = (HPEN) SelectObject(hdc, bluePen);
drawRect(570, 3, 210, 555, toolBG);
prepareColors();
drawColors();
drawTools();
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
ReleaseDC(hwnd, hdc);
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static bool clicked = false;
static bool rClicked = false;
static HBRUSH selectedColor = CreateSolidBrush(RGB(0, 0, 0));
static int tool = 0;
HPEN nullPen = CreatePen(PS_NULL, 0, RGB(0, 0, 0));
HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
int x = LOWORD(lParam);
int y = HIWORD(lParam);
switch (msg)
{
case WM_CLOSE:
{
DestroyWindow(hwnd);
}break;
case WM_DESTROY:
{
PostQuitMessage(0);
}break;
case WM_LBUTTONDOWN:
{
clicked = true;
}break;
case WM_LBUTTONUP:
{
clicked = false;
}break;
case WM_MOUSEMOVE:
{
if (clicked && x < 570 - 30)
{
SelectObject(hdc, nullPen);
switch (tool)
{
case 0:
{
drawRect(x, y, 30, 30, selectedColor);
}break;
case 1:
{
drawCircle(x, y, 30, selectedColor);
}break;
case 2:
{
drawTriangle(x, y, 30, 30, selectedColor);
}break;
case 3:
{
//SelectObject(hdc, blackPen);
drawTriangle(x, y, 30, 30, colors[15]);
}break;
}
}
}break;
case WM_RBUTTONDOWN:
{
rClicked = true;
for (int i = 0; i < 16; i++)
{
if (x > colorsLocation[i].position.x && x < colorsLocation[i].position.x + colorsLocation[i].size.x &&
y > colorsLocation[i].position.y && y < colorsLocation[i].position.y + colorsLocation[i].size.y && rClicked && y < 400 )
{
selectedColor = colors[i];
}
}
for (int i = 0; i < 4; i++)
{
if (x > toolsLocation[i].position.x && x < toolsLocation[i].position.x + toolsLocation[i].size.x &&
y > toolsLocation[i].position.y && y < toolsLocation[i].position.y + toolsLocation[i].size.y && rClicked)
{
tool = i;
}
}
}break;
case WM_RBUTTONUP:
{
rClicked = false;
}break;
default:
{
return DefWindowProc(hwnd, msg, wParam, lParam);
}break;
}
return 0;
}
void drawRect(int x, int y, int width, int height, HBRUSH color)
{
SelectObject(hdc, color);
Rectangle(hdc, x, y, x + width, y + height);
}
void drawCircle(int x, int y, int radius, HBRUSH color)
{
SelectObject(hdc, color);
Ellipse(hdc, x, y, x + radius, y + radius);
}
void drawTriangle(int x, int y, int width, int height, HBRUSH color)
{
POINT vertex[3];
vertex[0].x = x;
vertex[0].y = y;
vertex[1].x = x + width / 2;
vertex[1].y = y + height;
vertex[2].x = x + width;
vertex[2].y = y;
SelectObject(hdc, color);
Polygon(hdc, vertex, 3);
}
void prepareColors()
{
colors[0] = CreateSolidBrush(RGB(0, 0, 0));
colors[1] = CreateSolidBrush(RGB(200, 200, 200));
colors[2] = CreateSolidBrush(RGB(255, 100, 100));
colors[3] = CreateSolidBrush(RGB(255, 0, 0));
colors[4] = CreateSolidBrush(RGB(150, 150, 0));
colors[5] = CreateSolidBrush(RGB(255, 255, 0));
colors[6] = CreateSolidBrush(RGB(100, 255, 100));
colors[7] = CreateSolidBrush(RGB(0, 255, 0));
colors[8] = CreateSolidBrush(RGB(100, 100, 255));
colors[9] = CreateSolidBrush(RGB(0, 0, 255));
colors[10] = CreateSolidBrush(RGB(150, 0, 150));
colors[11] = CreateSolidBrush(RGB(255, 0, 255));
colors[12] = CreateSolidBrush(RGB(0, 150, 150));
colors[13] = CreateSolidBrush(RGB(0, 255, 255));
colors[14] = CreateSolidBrush(RGB(120, 120, 120));
colors[15] = CreateSolidBrush(RGB(255, 255, 255));
}
int x = 630;
int y = 10;
int width = 41;
int height = 41;
int spaceX = 20;
int spaceY = 10;
void drawColors()
{
for (int i = 1; i <= 16; i++)
{
colorsLocation[i - 1].position.x = x;
colorsLocation[i - 1].position.y = y;
colorsLocation[i - 1].size.x = x + width;
colorsLocation[i - 1].size.y = y + height;
drawRect(x, y, width, height, colors[i - 1]);
x += width + spaceX;
if (i % 2 == 0)
{
y += height + spaceY;
x = 630;
}
}
}
void drawTools()
{
y += 40;
for (int i = 1; i <= 4; i++)
{
toolsLocation[i - 1].position.x = x;
toolsLocation[i - 1].position.y = y;
toolsLocation[i - 1].size.x = x + width;
toolsLocation[i - 1].size.y = y + height;
drawRect(x, y, width, height, CreateSolidBrush(RGB(255, 255, 255)));
x += width + spaceX;
if (i % 2 == 0)
{
y += height + spaceY;
x = 630;
}
}
drawRect(toolsLocation[0].position.x + 5, toolsLocation[0].position.y + 5, width - 10, height - 10, colors[0]);
drawCircle(toolsLocation[1].position.x + 5, toolsLocation[1].position.y + 5, width - 10, colors[0]);
drawTriangle(toolsLocation[2].position.x + 5, toolsLocation[2].position.y + 5, width - 10, height - 10, colors[0]);
HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
SelectObject(hdc, blackPen);
drawTriangle(toolsLocation[3].position.x + 5, toolsLocation[3].position.y + 5, width - 10, height - 10, colors[15]);
}