Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <string>
- #include <array>
- LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
- bool createWinClass(HINSTANCE&, TCHAR*);
- using namespace std;
- struct CRectColor
- {
- unsigned int x, y;
- COLORREF pcolor;
- };
- struct CRectShape
- {
- unsigned int x, y, shape;
- };
- class CPaint
- {
- public:
- CPaint();
- void drawPallet(const HWND);
- void startDraw(const WORD, const WORD);
- void endDraw(const HWND, const WORD, const WORD);
- void updatePosition(const RECT);
- bool palletArea(const WORD) const;
- void userClick(const HWND, const WORD, const WORD, const bool);
- unsigned int getXcoord() const;
- static const unsigned int colors_count = 16;
- static const unsigned int rectangle_size = 40;
- static const unsigned int pallet_size = 200;
- private:
- void fillPallet();
- RECT position;
- RECT draw_coords;
- vector<CRectColor> pallet;
- map<string, CRectShape> shapes;
- enum shapes { RECTANGLE, CIRCLE, TRIANGLE, TRIANGLE_FILL };
- unsigned short int current_shape;
- COLORREF current_brush_color;
- COLORREF current_pen_color;
- };
- CPaint::CPaint() : current_shape(RECTANGLE), current_brush_color(RGB(0, 0,0)), current_pen_color(RGB(0, 0,0))
- {
- fillPallet();
- }
- void CPaint::fillPallet()
- {
- array<COLORREF, colors_count> tab = {
- RGB(0, 0, 0), RGB(255, 0, 0),
- RGB(0, 255, 0), RGB(0, 0, 255),
- RGB(255, 255, 0), RGB(255, 0, 255),
- RGB(0, 255, 255), RGB(255, 255, 255),
- RGB(80, 80, 80), RGB(180, 20, 20),
- RGB(20, 180, 20), RGB(20, 20, 180),
- RGB(180, 180, 20), RGB(180, 20, 180),
- RGB(20, 180, 180), RGB(150, 150, 150)
- };
- for(array<COLORREF, colors_count>::iterator it = tab.begin(); it != tab.end(); it++)
- {
- CRectColor CRectColor;
- CRectColor.x = CRectColor.y = 0;
- CRectColor.pcolor = *it;
- this->pallet.push_back(CRectColor);
- }
- // shapes
- CRectShape defaultrect;
- defaultrect.x = defaultrect.y = 0;
- defaultrect.shape = RECTANGLE;
- shapes["rectangle"] = defaultrect;
- shapes["circle"] = defaultrect;
- shapes["triangle"] = defaultrect;
- shapes["trianglefill"] = defaultrect;
- shapes["circle"].shape = CIRCLE;
- shapes["triangle"].shape = TRIANGLE;
- shapes["trianglefill"].shape = TRIANGLE_FILL;
- draw_coords.bottom = draw_coords.left = draw_coords.top = draw_coords.right = 0;
- }
- void CPaint::updatePosition(const RECT position)
- {
- this->position = position;
- }
- void CPaint::drawPallet(const HWND hwnd)
- {
- PAINTSTRUCT paintdata;
- HDC hdc = BeginPaint(hwnd, &paintdata);
- HPEN emptyPen = (HPEN) SelectObject(hdc, GetStockObject(NULL_PEN));
- HBRUSH emptyBrush = (HBRUSH) SelectObject(hdc, GetStockObject(NULL_BRUSH));
- HPEN standardPen = CreatePen(PS_SOLID, 0, RGB(0, 0, 0));
- HPEN bluePen = CreatePen(PS_SOLID, 0, RGB(0, 0, 255));
- HPEN greyPen = CreatePen(PS_SOLID, 0, RGB(180, 180, 180));
- HBRUSH standardBrush = CreateSolidBrush(RGB(0, 0, 0));
- HBRUSH whiteBrush = CreateSolidBrush(RGB(255, 255, 255));
- HBRUSH greyBrush = CreateSolidBrush(RGB(180, 180, 180));
- SelectObject(hdc, greyBrush);
- SelectObject(hdc, bluePen);
- //draw grey palet
- Rectangle(hdc, position.right - CPaint::pallet_size,
- 0, position.right, position.bottom);
- //draw colors palet
- unsigned int i = 0, inc = 0;
- unsigned int margin_left = 45;
- unsigned int margin_top = 40;
- SelectObject(hdc, greyPen);
- for(vector<CRectColor>::iterator it = pallet.begin(); it != pallet.end(); it++, i++, inc++)
- {
- if(inc == 8) {
- margin_left += CPaint::rectangle_size + 25;
- i = 0;
- }
- unsigned int margin = i * (CPaint::rectangle_size + 5);
- (*it).x = position.right - CPaint::pallet_size + margin_left;
- (*it).y = position.top + margin_top + margin;
- HBRUSH rectBrush = CreateSolidBrush((*it).pcolor);
- SelectObject(hdc, rectBrush);
- Rectangle(hdc, (*it).x, (*it).y,
- (*it).x + CPaint::rectangle_size, (*it).y + CPaint::rectangle_size);
- DeleteObject(rectBrush);
- }
- //draw shapes pallet
- SelectObject(hdc, standardPen);
- SelectObject(hdc, whiteBrush);
- unsigned int margin_bottom = 80;
- margin_left = 45;
- //rectangle
- shapes["rectangle"].x = position.right - CPaint::pallet_size + margin_left;
- shapes["rectangle"].y = position.bottom - margin_bottom - CPaint::rectangle_size;
- Rectangle(hdc, shapes["rectangle"].x, shapes["rectangle"].y,
- shapes["rectangle"].x + CPaint::rectangle_size, shapes["rectangle"].y + CPaint::rectangle_size);
- SelectObject(hdc, standardBrush);
- Rectangle(hdc, shapes["rectangle"].x + 5, shapes["rectangle"].y + 5,
- shapes["rectangle"].x + CPaint::rectangle_size - 5, shapes["rectangle"].y + CPaint::rectangle_size - 5);
- //circle
- SelectObject(hdc, whiteBrush);
- shapes["circle"].x = shapes["rectangle"].x;
- shapes["circle"].y = position.bottom - margin_bottom + 5;
- Rectangle(hdc, shapes["circle"].x, shapes["circle"].y,
- shapes["circle"].x + CPaint::rectangle_size, shapes["circle"].y + CPaint::rectangle_size);
- SelectObject(hdc, standardBrush);
- Ellipse(hdc, shapes["circle"].x + 5, shapes["circle"].y + 5,
- shapes["circle"].x + CPaint::rectangle_size - 5, shapes["circle"].y + CPaint::rectangle_size - 5);
- //triangle
- SelectObject(hdc, whiteBrush);
- shapes["triangle"].x = shapes["rectangle"].x + CPaint::rectangle_size + 25;
- shapes["triangle"].y = shapes["rectangle"].y;
- Rectangle(hdc, shapes["triangle"].x, shapes["triangle"].y,
- shapes["triangle"].x + CPaint::rectangle_size, shapes["triangle"].y + CPaint::rectangle_size);
- POINT trianglePos[3];
- trianglePos[0].x = shapes["triangle"].x + 5;
- trianglePos[1].x = shapes["triangle"].x + CPaint::rectangle_size - 5;
- trianglePos[0].y = trianglePos[1].y = shapes["triangle"].y + 5;
- trianglePos[2].y = shapes["triangle"].y + CPaint::rectangle_size - 5;
- trianglePos[2].x = (trianglePos[0].x + trianglePos[1].x) / 2;
- Polygon(hdc, trianglePos, 3);
- //triangle fill
- SelectObject(hdc, whiteBrush);
- shapes["trianglefill"].x = shapes["triangle"].x;
- shapes["trianglefill"].y = shapes["circle"].y;
- Rectangle(hdc, shapes["trianglefill"].x, shapes["trianglefill"].y,
- shapes["trianglefill"].x + CPaint::rectangle_size, shapes["trianglefill"].y + CPaint::rectangle_size);
- SelectObject(hdc, standardBrush);
- trianglePos[0].x = shapes["trianglefill"].x + 5;
- trianglePos[1].x = shapes["trianglefill"].x + CPaint::rectangle_size - 5;
- trianglePos[0].y = trianglePos[1].y = shapes["trianglefill"].y + 5;
- trianglePos[2].y = shapes["trianglefill"].y + CPaint::rectangle_size - 5;
- trianglePos[2].x = (trianglePos[0].x + trianglePos[1].x) / 2;
- Polygon(hdc, trianglePos, 3);
- SelectObject(hdc, emptyPen);
- SelectObject(hdc, emptyBrush);
- EndPaint(hwnd, &paintdata);
- DeleteObject(standardPen);
- DeleteObject(bluePen);
- DeleteObject(greyPen);
- DeleteObject(standardBrush);
- DeleteObject(whiteBrush);
- DeleteObject(greyBrush);
- }
- unsigned int CPaint::getXcoord() const
- {
- return position.right;
- }
- void CPaint::startDraw(const WORD x, const WORD y)
- {
- // start draw on client area, remember points
- draw_coords.left = x;
- draw_coords.top = y;
- }
- void CPaint::endDraw(const HWND hwnd, const WORD x, const WORD y)
- {
- // end draw on client area
- draw_coords.right = x;
- draw_coords.bottom = y;
- HDC hdc = GetDC(hwnd);
- HPEN emptyPen = (HPEN) SelectObject(hdc, GetStockObject(NULL_PEN));
- HBRUSH emptyBrush = (HBRUSH) SelectObject(hdc, GetStockObject(NULL_BRUSH));
- HPEN userPen = CreatePen(PS_SOLID, 0, current_pen_color);
- HBRUSH userBrush = CreateSolidBrush(current_brush_color);
- SelectObject(hdc, userPen);
- SelectObject(hdc, userBrush);
- switch(current_shape)
- {
- case RECTANGLE:
- Rectangle(hdc, draw_coords.left, draw_coords.top,
- draw_coords.right, draw_coords.bottom);
- break;
- case CIRCLE:
- Ellipse(hdc, draw_coords.left, draw_coords.top,
- draw_coords.right, draw_coords.bottom);
- break;
- case TRIANGLE:
- {
- POINT trianglePos[4];
- trianglePos[0].x = draw_coords.left;
- trianglePos[1].x = draw_coords.right;
- trianglePos[0].y = trianglePos[1].y = draw_coords.top;
- trianglePos[2].y = draw_coords.bottom;
- trianglePos[2].x = (trianglePos[0].x + trianglePos[1].x) / 2;
- trianglePos[3] = trianglePos[0];
- Polyline(hdc, trianglePos, 4);
- break;
- }
- case TRIANGLE_FILL:
- {
- POINT trianglePos[3];
- trianglePos[0].x = draw_coords.left;
- trianglePos[1].x = draw_coords.right;
- trianglePos[0].y = trianglePos[1].y = draw_coords.top;
- trianglePos[2].y = draw_coords.bottom;
- trianglePos[2].x = (trianglePos[0].x + trianglePos[1].x) / 2;
- Polygon(hdc, trianglePos, 3);
- break;
- }
- }
- SelectObject(hdc, emptyPen);
- SelectObject(hdc, emptyBrush);
- DeleteObject(userPen);
- DeleteObject(userBrush);
- ReleaseDC(hwnd, hdc);
- }
- bool CPaint::palletArea(const WORD x) const
- {
- if(x < position.right - pallet_size) // no on grey pallet
- return false;
- return true;
- }
- void CPaint::userClick(const HWND hwnd, const WORD x, const WORD y, const bool left)
- {
- if(left) // LPM click
- {
- // color change?
- for(vector<CRectColor>::iterator it = pallet.begin(); it != pallet.end(); it++)
- {
- if(x >= (*it).x && x <= (*it).x + CPaint::rectangle_size)
- {
- if(y >= (*it).y && y <= (*it).y + CPaint::rectangle_size)
- {
- HDC con = GetDC(hwnd);
- current_brush_color = GetPixel(con, x, y);
- ReleaseDC(hwnd, con);
- break;
- }
- }
- }
- // shape change?
- unsigned int i = 0;
- for(map<string, CRectShape>::iterator it = shapes.begin(); it != shapes.end(); it++, i++)
- {
- if(x >= (*it).second.x && x <= (*it).second.x + CPaint::rectangle_size)
- {
- if(y >= (*it).second.y && y <= (*it).second.y + CPaint::rectangle_size)
- {
- current_shape = (*it).second.shape;
- break;
- }
- }
- }
- }
- else // RPM click
- {
- // color change?
- for(vector<CRectColor>::iterator it = pallet.begin(); it != pallet.end(); it++)
- {
- if(x >= (*it).x && x <= (*it).x + CPaint::rectangle_size)
- {
- if(y >= (*it).y && y <= (*it).y + CPaint::rectangle_size)
- {
- HDC con = GetDC(hwnd);
- current_pen_color = GetPixel(con, x, y);
- ReleaseDC(hwnd, con);
- break;
- }
- }
- }
- }
- }
- // END CLASS
- TCHAR APPName[] = TEXT("Paint application");
- TCHAR ClassName[] = TEXT("Class name");
- CPaint Paint;
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPevInstance, LPSTR lpCmdLine, int nShowCmd)
- {
- if(!createWinClass(hInstance, ClassName))
- return 1;
- HWND window = CreateWindowEx(0, ClassName, APPName, WS_OVERLAPPEDWINDOW, 200, 50, 800, 650, 0, 0, 0, 0);
- if(window)
- {
- MSG msg;
- ShowWindow(window, nShowCmd);
- while(GetMessage(&msg, 0, 0, 0) > 0)
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- else MessageBox(0, TEXT("Inicjalizacja okna nie powiodła się."), APPName, MB_OK | MB_ICONERROR);
- UnregisterClass(ClassName, hInstance);
- return 0;
- }
- static LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- static bool draw = false;
- switch(msg)
- {
- case WM_CLOSE:
- {
- if(MessageBox(hwnd, TEXT("Czy na pewno zamknąć aplikację?"), APPName, MB_YESNO) == IDYES)
- {
- DestroyWindow(hwnd);
- break;
- }
- }
- case WM_MOVE:
- case WM_SIZE:
- {
- RECT rectangle;
- InvalidateRect(hwnd, 0, true);
- GetClientRect(hwnd, &rectangle);
- Paint.updatePosition(rectangle);
- break;
- }
- case WM_GETMINMAXINFO:
- {
- MINMAXINFO* mmi = (MINMAXINFO*)lParam;
- mmi->ptMinTrackSize.x = CPaint::pallet_size;
- mmi->ptMinTrackSize.y = 580;
- return 0;
- }
- case WM_PAINT:
- {
- Paint.drawPallet(hwnd);
- break;
- }
- case WM_LBUTTONDOWN:
- {
- if(Paint.palletArea(LOWORD(lParam)))
- Paint.userClick(hwnd, LOWORD(lParam), HIWORD(lParam), true); // brush / shape choice
- else
- {
- draw = true;
- Paint.startDraw(LOWORD(lParam), HIWORD(lParam));
- }
- break;
- }
- case WM_LBUTTONUP:
- {
- if(!Paint.palletArea(LOWORD(lParam)) && draw) // end draw
- Paint.endDraw(hwnd, LOWORD(lParam), HIWORD(lParam));
- draw = false;
- break;
- }
- case WM_RBUTTONDOWN:
- {
- if(Paint.palletArea(LOWORD(lParam))) // pen color choice
- Paint.userClick(hwnd, LOWORD(lParam), HIWORD(lParam), false);
- break;
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
- static bool createWinClass(HINSTANCE& hInstance, TCHAR* ClassName)
- {
- WNDCLASSEX wc;
- wc.cbClsExtra = wc.cbWndExtra = 0;
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wc.hCursor = LoadCursor(0, IDC_ARROW);
- wc.hIconSm = wc.hIcon = LoadIcon(0, IDI_APPLICATION);
- wc.hInstance = hInstance;
- wc.lpfnWndProc = WinProc;
- wc.lpszClassName = ClassName;
- wc.lpszMenuName = 0;
- wc.style = 0;
- return (RegisterClassEx(&wc) != 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement