Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
- static void OnWM_PAINT(const HWND hwnd);
- static void OnWM_LBUTTONDOWN(const HWND hwnd, LPARAM lParam, WPARAM wParam);
- RECT rSize;
- TCHAR className[] = TEXT("Paint v1");
- int colors[] = {
- 0x000000,
- 0xFFFFFF,
- 0x7F7F7F,
- 0xFF0000,
- 0xFF7F00,
- 0xFF7F7F,
- 0xFF007F,
- 0xFF00FF,
- 0XFFFF00,
- 0x7F0000,
- 0x7F7F00,
- 0x7F007F,
- 0x7FFF00,
- 0x7F00FF,
- 0x007F00,
- 0x00FFFF,
- 0x00FF00,
- };
- enum Type {
- color,
- rectangle,
- elipse,
- triangle
- };
- class Button {
- POINT leftTop;
- POINT rightBottom;
- Type type;
- int nColor;
- public:
- Button() {
- leftTop.x = 0;
- leftTop.y = 0;
- rightBottom.x = 0;
- rightBottom.y = 0;
- type = color;
- nColor = colors[0];
- }
- Button(int nXL, int nYL, int nXR, int nYR, Type type, int nColor) {
- this->leftTop.x = nXL;
- this->leftTop.y = nYL;
- this->rightBottom.x = nXR;
- this->rightBottom.y = nYR;
- this->type = type;
- this->nColor = nColor;
- }
- inline const Type returnType() const { return type; }
- inline const int returnColor() const { return nColor; }
- bool posComare(const int nX, const int nY) const {
- if (nX <= leftTop.x && nX <= rightBottom.x &&
- nY >= leftTop.y && nY <= rightBottom.y)
- return true;
- return false;
- }
- void drawButton(HDC hdc) {
- HBRUSH brush = CreateSolidBrush(this->nColor);
- HBRUSH defaultBrush = (HBRUSH)SelectObject(hdc, brush);
- Rectangle(hdc, leftTop.x, leftTop.y, rightBottom.x, rightBottom.y);
- SelectObject(hdc, defaultBrush);
- DeleteObject(brush);
- }
- } buttons[16];
- int WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- WNDCLASSEX wc;
- wc.cbClsExtra = 0;
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.cbWndExtra = 0;
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hIcon = 0;
- wc.hIconSm = 0;
- wc.hInstance = hInstance;
- wc.lpfnWndProc = WndProc;
- wc.lpszClassName = className;
- wc.lpszMenuName = 0;
- wc.style = 0;
- if (!RegisterClassEx(&wc)) {
- MessageBox(NULL, TEXT("Błąd przy rejestracji okna!"), TEXT("Błąd!"), MB_OK | MB_ICONERROR);
- return 1;
- }
- HWND hwnd = CreateWindowEx(0, className, TEXT("Paint"), WS_OVERLAPPEDWINDOW, 50, 50, 500, 500, 0, 0, hInstance, 0);
- ShowWindow(hwnd, SW_SHOW);
- UpdateWindow(hwnd);
- MSG msg;
- while (GetMessage(&msg, NULL, NULL, NULL)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- UnregisterClass(className, hInstance);
- return msg.wParam;
- }
- static void OnWM_LBUTTONDOWN(const HWND hwnd, LPARAM lParam, WPARAM wParam) {
- POINT pos;
- pos.x = LOWORD(lParam);
- pos.y = HIWORD(lParam);
- int actualColor;
- for (int i = 0; i < 16; ++i) {
- if (buttons[i].posComare(pos.x, pos.y) == true)
- actualColor = buttons[i].returnColor();
- }
- if (pos.x < rSize.right - 150) {
- HDC hdc = GetDC(hwnd);
- HBRUSH brush = CreateSolidBrush(actualColor);
- HBRUSH defaultBrush = (HBRUSH)SelectObject(hdc, brush);
- Rectangle(hdc, pos.x, pos.y, pos.x + 20, pos.y + 20);
- SelectObject(hdc, defaultBrush);
- DeleteObject(brush);
- ReleaseDC(hwnd, hdc);
- }
- }
- static void OnWM_PAINT(const HWND hwnd) {
- PAINTSTRUCT ps;
- HDC hdc;
- HBRUSH brush1 = CreateSolidBrush(RGB(54, 69, 79));
- hdc = BeginPaint(hwnd, &ps);
- HBRUSH defaultBrush = (HBRUSH)SelectObject(hdc, brush1);
- GetClientRect(hwnd, &rSize);
- Rectangle(hdc, rSize.right - 150, rSize.top, rSize.right, rSize.bottom);
- for (int i = 0; i < 16; ++i) {
- if (i % 2 == 1)
- buttons[i] = Button(rSize.right - 95, rSize.top + (i * 20), rSize.right - 125, rSize.top + 30 + (i * 20), color, colors[i]);
- else
- buttons[i] = Button(rSize.right - 30, rSize.top + 20 + (i * 20), rSize.right - 60, rSize.top + 50 + (i * 20), color, colors[i]);
- }
- for (int i = 0; i < 16; ++i)
- buttons[i].drawButton(hdc);
- SelectObject(hdc, defaultBrush);
- EndPaint(hwnd, &ps);
- DeleteObject(brush1);
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
- switch (msg) {
- case WM_PAINT:
- OnWM_PAINT(hwnd); break;
- case WM_LBUTTONDOWN:
- OnWM_LBUTTONDOWN(hwnd, lParam, wParam); break;
- case WM_DESTROY:
- PostQuitMessage(0); break;
- default:
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement