Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <iostream>
- #include <fstream>
- #include <vector>
- #define width 1280
- #define height 720
- std::fstream File;
- bool chekParams = false;
- bool isSquare = false;
- bool isRhombus = false;
- float angle = 0;
- float scale = 1;
- HDC hdc;
- HWND hWnd, hWndPaint;
- COLORREF cRefPen, cRefBrush;
- HBRUSH hBrush;
- HPEN hPen;
- const int ID_butttonSquare = 1, ID_buttonRhombus = 2, ID_TEXTBOX = 3, ID_Brush = 4, ID_Pen = 5, ID_saveFile = 6, ID_getFileOne = 7, ID_getFileAll = 8;
- void readFromFile(char *filename, bool all);
- void reloadPaint(int count);
- float NormaVector(POINT *vect);
- POINT centerFigure(POINT *p);
- bool Сollinearity(POINT *vect1, POINT *vect2);
- POINT VectorFromPoints(POINT *p1, POINT *p2);
- bool isSquarePoints(POINT *p);
- bool isRhombusPoints(POINT *p);
- POINT *scaleFigure(POINT *pSource, POINT center, float scale);
- POINT scalePoint(POINT pSource, POINT center, float scale);
- POINT *rotateFigureToAngle(POINT *pSource, POINT center, float alpha);
- POINT rotatePointToAngle(POINT pSource, POINT center, float alpha);
- void getPen();
- COLORREF chooseColor();
- LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
- LRESULT CALLBACK PaintProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
- POINT *PointSquare(int x, int y)
- {
- POINT *p = new POINT[4];
- p[0].x = x; p[0].y = y;
- p[1].x = x; p[1].y = y + 100;
- p[2].x = x + 100; p[2].y = y + 100;
- p[3].x = x + 100; p[3].y = y;
- return p;
- }
- POINT *PointRhombus(int x, int y)
- {
- POINT *p = new POINT[4];
- p[0].x = x; p[0].y = y;
- p[1].x = x + 50; p[1].y = y + 80;
- p[2].x = x + 100; p[2].y = y;
- p[3].x = x + 50; p[3].y = y - 80;
- return p;
- }
- struct Elem
- {
- POINT center;
- POINT *p;
- HBRUSH hB;
- HPEN hP;
- };
- Elem nowFigure;
- std::vector<Elem> Container;
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- MSG uMsg;
- WNDCLASSEX windowsClass;
- memset(&windowsClass, 0, sizeof(WNDCLASSEXW));
- windowsClass.cbSize = sizeof(WNDCLASSEX);
- windowsClass.hbrBackground = (HBRUSH)GetStockObject(1);
- windowsClass.hCursor = LoadCursor(NULL, IDC_ARROW);
- windowsClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- windowsClass.hInstance = hInstance;
- windowsClass.lpfnWndProc = WindowProc;
- windowsClass.lpszClassName = "Simple WIndow";
- RegisterClassEx(&windowsClass);
- hWnd = CreateWindow(windowsClass.lpszClassName, "DrawWindow", WS_OVERLAPPED |WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
- (GetSystemMetrics(SM_CXSCREEN) - width) / 2, (GetSystemMetrics(SM_CYSCREEN) - height) / 2, width, height, NULL, NULL, NULL, NULL);
- CreateWindow("static", " Толщина границы:", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 5, 5, 165, 90, hWnd, NULL, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
- CreateWindow("EDIT", "3", WS_BORDER|WS_VISIBLE|WS_CHILD|ES_LEFT|ES_MULTILINE, 135, 7, 30, 17, hWnd, (HMENU)ID_TEXTBOX, NULL, NULL);
- CreateWindowEx(0, "button", "Цвет кисти", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 30, 100, 25, hWnd, (HMENU)ID_Brush, NULL, NULL);
- CreateWindowEx(0, "button", "Цвет пера", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 60, 100, 25, hWnd, (HMENU)ID_Pen, NULL, NULL);
- CreateWindow("static", " Фигуры", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 5, 100, 165, 90, hWnd, NULL, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
- CreateWindowEx(0, "button", "Квадрат", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 125, 100, 25, hWnd, (HMENU)ID_butttonSquare, NULL, NULL);
- CreateWindowEx(0, "button", "Ромб", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 155, 100, 25, hWnd, (HMENU)ID_buttonRhombus, NULL, NULL);
- CreateWindow("static", " Файлы", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 5, 195, 165, 120, hWnd, NULL, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
- CreateWindowEx(0, "button", "Считать", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 220, 100, 25, hWnd, (HMENU)ID_getFileOne, NULL, NULL);
- CreateWindowEx(0, "button", "Считать все", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 250, 100, 25, hWnd, (HMENU)ID_getFileAll, NULL, NULL);
- CreateWindowEx(0, "button", "Сохранить", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 280, 100, 25, hWnd, (HMENU)ID_saveFile, NULL, NULL);
- ShowWindow(hWnd, nCmdShow);
- WNDCLASS windowsClass2;
- memset(&windowsClass2,0,sizeof(WNDCLASS));
- windowsClass2.lpfnWndProc = PaintProc;
- windowsClass2.hInstance = hInstance;
- windowsClass2.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- windowsClass2.lpszClassName = "ChildWClass";
- windowsClass2.hCursor=LoadCursor(NULL,IDC_CROSS);
- RegisterClass(&windowsClass2);
- hWndPaint=CreateWindowEx(0,"ChildWClass",(LPCTSTR) NULL, WS_CHILD | WS_BORDER | WS_VISIBLE, 180, 0, 1084, 681, hWnd, NULL, hInstance, NULL);
- ShowWindow(hWndPaint, SW_NORMAL);
- UpdateWindow(hWndPaint);
- SetActiveWindow(hWnd);
- cRefPen = COLORREF(RGB(0,0,0));
- cRefBrush = COLORREF(RGB(255,0,0));
- hdc = GetDC(hWndPaint);
- while (GetMessage(&uMsg, hWnd, NULL, NULL))
- {
- TranslateMessage(&uMsg);
- DispatchMessage(&uMsg);
- }
- return uMsg.wParam;
- }
- LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_MOUSEWHEEL:
- if(chekParams)
- {
- int m = Container.size() - 1;
- nowFigure = Container[m];
- if (GET_WHEEL_DELTA_WPARAM(wParam) > 0)
- if (GetAsyncKeyState(VK_SHIFT))
- angle += 0.05f;
- else
- scale += 0.05f;
- else
- if (GetAsyncKeyState(VK_SHIFT))
- angle = angle - 0.05f;
- else
- if (scale > 0.05f) scale -= 0.05f;
- reloadPaint(Container.size() - 1);
- SelectObject(hdc, nowFigure.hP);
- SelectObject(hdc, nowFigure.hB);
- nowFigure.p = rotateFigureToAngle(nowFigure.p, nowFigure.center, angle);
- nowFigure.p = scaleFigure(nowFigure.p, nowFigure.center, scale);
- Polygon(hdc, nowFigure.p, 4);
- }
- break;
- case WM_CLOSE:
- ExitProcess(0);
- break;
- case WM_COMMAND:
- chekParams = false;
- int m;
- m = Container.size();
- if (m)
- Container[m - 1] = nowFigure;
- switch (LOWORD(wParam))
- {
- case ID_butttonSquare:
- //SetWindowText(hLabelWnd, "квадрат");
- isSquare = true;
- break;
- case ID_buttonRhombus:
- isRhombus = true;
- break;
- case ID_Brush:
- cRefBrush = chooseColor();
- break;
- case ID_Pen:
- cRefPen = chooseColor();
- break;
- case ID_getFileOne:
- readFromFile("Фигуры.txt", 0);
- break;
- case ID_getFileAll:
- readFromFile("Фигуры.txt", 1);
- chekParams = false;
- break;
- case ID_saveFile:
- {
- static HDC hdcEMF;
- hdcEMF = CreateEnhMetaFile(NULL, "рисунок.EMF", NULL, "CreateMetafile\0Pict1\0");
- int count = Container.size();
- for (int i = 0; i < count; i++)
- {
- SelectObject(hdcEMF, Container[i].hP);
- SelectObject(hdcEMF, Container[i].hB);
- Polygon(hdcEMF, Container[i].p, 4);
- }
- HENHMETAFILE hEMF = CloseEnhMetaFile(hdcEMF);
- DeleteEnhMetaFile(hEMF);
- }
- break;
- default:
- break;
- }
- default:
- break;
- }
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- LRESULT CALLBACK PaintProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_LBUTTONDOWN:
- chekParams = false;
- if (isSquare || isRhombus)
- {
- int m = Container.size();
- if (m)
- Container[m - 1] = nowFigure;
- getPen(); //
- hBrush = CreateSolidBrush(cRefBrush);
- hdc = GetDC(hWndPaint);
- SelectObject(hdc, hPen);
- SelectObject(hdc, hBrush);
- int x, y;
- x = LOWORD(lParam);
- y = HIWORD(lParam);
- if (isSquare)
- {
- nowFigure.center.x = x + 50;
- nowFigure.center.y = y + 50;
- nowFigure.p = PointSquare(x, y);
- isSquare = false;
- }
- else
- {
- nowFigure.center.x = x + 50;
- nowFigure.center.y = y;
- nowFigure.p = PointRhombus(x, y);
- isRhombus = false;
- }
- nowFigure.hB = hBrush;
- nowFigure.hP = hPen;
- Polygon(hdc, nowFigure.p, 4);
- Container.push_back(nowFigure);
- chekParams = true;
- angle = 0;
- scale = 1;
- }
- break;
- default:
- break;
- }
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- void readFromFile(char *filename, bool all)
- {
- try
- {
- if (!File.is_open())
- File.open(filename, std::ios::in);
- static int i = 0, n;
- int type, j, Wd;
- int *color = new int [3];
- if (!i) File >> n;
- for (; i < n; i++)
- {
- File >> type;
- POINT *p = new POINT[4];
- for(j = 0; j < 4; j++)
- File >> p[j].x >> p[j].y;
- nowFigure.p = p;
- for (j = 0; j < 3; j++) File >> color[j];
- nowFigure.hB = CreateSolidBrush(RGB(color[0], color[1], color[2]));
- for (j = 0; j < 3; j++) File >> color[j];
- File >> Wd;
- nowFigure.hP = CreatePen(PS_SOLID, Wd, RGB(color[0], color[1], color[2]));
- nowFigure.center = centerFigure(nowFigure.p);
- if (type == 100001) // квадрат
- {
- if (isSquarePoints(nowFigure.p))
- {
- Container.push_back(nowFigure);
- chekParams = true;
- SelectObject(hdc, nowFigure.hP);
- SelectObject(hdc, nowFigure.hB);
- Polygon(hdc, nowFigure.p, 4);
- }
- else
- MessageBox(hWnd, TEXT("Некорректные входные данные"), TEXT("Ошибка"), NULL);
- }
- else // ромб
- {
- if (isRhombusPoints(nowFigure.p))
- {
- Container.push_back(nowFigure);
- chekParams = true;
- SelectObject(hdc, nowFigure.hP);
- SelectObject(hdc, nowFigure.hB);
- Polygon(hdc, nowFigure.p, 4);
- }
- else
- MessageBox(hWnd, TEXT("Некорректные входные данные"), TEXT("Ошибка"), NULL);
- }
- if (!all)
- {
- i++;
- break;
- }
- }
- if (i == n)
- {
- i = 0;
- File.close();
- }
- }
- catch (int correct)
- {
- MessageBox(hWnd, TEXT("Не удалось выполнить чтение из файла"), TEXT("Ошибка"), NULL);
- }
- }
- void reloadPaint(int count)
- {
- InvalidateRect(hWndPaint, 0, 1);
- ReleaseDC(hWndPaint, hdc);
- UpdateWindow(hWndPaint);
- hdc = GetDC(hWndPaint);
- for (int i = 0; i < count; i++)
- {
- SelectObject(hdc, Container[i].hP);
- SelectObject(hdc, Container[i].hB);
- Polygon(hdc, Container[i].p, 4);
- }
- }
- COLORREF chooseColor()
- {
- CHOOSECOLOR cc;
- static COLORREF acrCustClr[16];
- static DWORD rgbCurrent;
- ZeroMemory(&cc, sizeof(cc));
- cc.lStructSize = sizeof(cc);
- cc.hwndOwner = hWnd;
- cc.lpCustColors = (LPDWORD) acrCustClr;
- cc.rgbResult = rgbCurrent;
- cc.Flags = CC_FULLOPEN | CC_RGBINIT;
- if (ChooseColor(&cc)==TRUE)
- return cc.rgbResult;
- return NULL;
- }
- void getPen()
- {
- char str[3];
- GetDlgItemText(hWnd, ID_TEXTBOX, str, sizeof(str));
- int penWidth;
- penWidth = atoi(str);
- if (penWidth > 0)
- hPen = CreatePen(PS_SOLID, penWidth, cRefPen);
- else
- hPen = CreatePen(PS_SOLID, 3, cRefPen);
- }
- POINT *scaleFigure(POINT *pSource, POINT center, float scale)
- {
- POINT *p = new POINT[4];
- for(int i = 0; i < 4; i++)
- p[i] = scalePoint(pSource[i], center, scale);
- return p;
- }
- POINT scalePoint(POINT pSource, POINT center, float scale)
- {
- POINT p;
- p.x = LONG((pSource.x - center.x) * scale + center.x);
- p.y = LONG((pSource.y - center.y) * scale + center.y);
- return p;
- }
- POINT *rotateFigureToAngle(POINT *pSource, POINT center, float alpha)
- {
- POINT *p = new POINT[4];
- for(int i = 0; i < 4; i++)
- p[i] = rotatePointToAngle(pSource[i], center, alpha);
- return p;
- }
- POINT rotatePointToAngle(POINT pSource, POINT center, float alpha)
- {
- POINT p;
- int x = pSource.x, x0 = center.x, y = pSource.y, y0 = center.y;
- p.x = LONG(x0 + (x - x0) * cos(alpha) - (y - y0) * sin(alpha));
- p.y = LONG(y0 + (y - y0) * cos(alpha) + (x - x0) * sin(alpha));
- return p;
- }
- float NormaVector(POINT *vect)
- {
- return (float)sqrt(vect->x * vect->x + vect->y * vect->y);
- }
- bool Сollinearity(POINT *vect1, POINT *vect2)
- {
- if (vect1->x * vect2->y == vect2->x * vect1->y) return true;
- return false;
- }
- POINT VectorFromPoints(POINT *p1, POINT *p2)
- {
- POINT vect;
- vect.x = p2->x - p1->x;
- vect.y = p2->y - p1->y;
- return vect;
- }
- bool isSquarePoints(POINT *p)
- {
- POINT vector1, vector2;
- vector1 = VectorFromPoints(&p[0], &p[1]);
- vector2 = VectorFromPoints(&p[2], &p[3]);
- if (Сollinearity(&vector1, &vector2))
- {
- vector1 = VectorFromPoints(&p[1], &p[2]);
- if (abs( NormaVector(&vector1) - NormaVector(&vector2)) < 0.1)
- {
- return true;
- }
- }
- return false;
- }
- bool isRhombusPoints(POINT *p)
- {
- POINT vector1, vector2;
- vector1 = VectorFromPoints(&p[0], &p[1]);
- vector2 = VectorFromPoints(&p[2], &p[3]);
- if (Сollinearity(&vector1, &vector2))
- {
- vector1 = VectorFromPoints(&p[0], &p[2]);
- vector1 = VectorFromPoints(&p[1], &p[3]);
- if (abs( NormaVector(&vector1) - NormaVector(&vector2)) < 0.1)
- {
- return true;
- }
- }
- return false;
- }
- POINT centerFigure(POINT *p)
- {
- POINT center, vector1;
- vector1 = VectorFromPoints(&p[0], &p[2]);
- center.x = p[0].x + vector1.x / 2;
- center.y = p[0].y + vector1.y / 2;
- return center;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement