Advertisement
Guest User

пцкцп

a guest
Dec 21st, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.83 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5.  
  6. #define width 1280
  7. #define height 720
  8.  
  9. std::fstream File;
  10.  
  11. bool chekParams = false;
  12. bool isSquare = false;
  13. bool isRhombus = false;
  14. float angle = 0;
  15. float scale = 1;
  16.  
  17. HDC hdc;
  18. HWND hWnd, hWndPaint;
  19. COLORREF cRefPen, cRefBrush;
  20. HBRUSH hBrush;
  21. HPEN hPen;
  22. 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;
  23.  
  24. void readFromFile(char *filename, bool all);
  25. void reloadPaint(int count);
  26. float NormaVector(POINT *vect);
  27. POINT centerFigure(POINT *p);
  28. bool Сollinearity(POINT *vect1, POINT *vect2);
  29. POINT VectorFromPoints(POINT *p1, POINT *p2);
  30. bool isSquarePoints(POINT *p);
  31. bool isRhombusPoints(POINT *p);
  32. POINT *scaleFigure(POINT *pSource, POINT center, float scale);
  33. POINT scalePoint(POINT pSource, POINT center, float scale);
  34. POINT *rotateFigureToAngle(POINT *pSource, POINT center, float alpha);
  35. POINT rotatePointToAngle(POINT pSource, POINT center, float alpha);
  36. void getPen();
  37. COLORREF chooseColor();
  38. LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  39. LRESULT CALLBACK PaintProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  40.  
  41. POINT *PointSquare(int x, int y)
  42. {
  43.     POINT *p = new POINT[4];
  44.     p[0].x = x; p[0].y = y;
  45.     p[1].x = x; p[1].y = y + 100;
  46.     p[2].x = x + 100; p[2].y = y + 100;
  47.     p[3].x = x + 100; p[3].y = y;
  48.     return p;
  49. }
  50. POINT *PointRhombus(int x, int y)
  51. {
  52.     POINT *p = new POINT[4];
  53.     p[0].x = x; p[0].y = y;
  54.     p[1].x = x + 50; p[1].y = y + 80;
  55.     p[2].x = x + 100; p[2].y = y;
  56.     p[3].x = x + 50; p[3].y = y - 80;
  57.     return p;
  58. }
  59. struct Elem
  60. {
  61.     POINT center;
  62.     POINT *p;
  63.     HBRUSH hB;
  64.     HPEN hP;
  65. };
  66. Elem nowFigure;
  67. std::vector<Elem> Container;
  68.  
  69. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  70. {
  71.     MSG uMsg;
  72.     WNDCLASSEX windowsClass;
  73.     memset(&windowsClass, 0, sizeof(WNDCLASSEXW));
  74.     windowsClass.cbSize = sizeof(WNDCLASSEX);
  75.     windowsClass.hbrBackground = (HBRUSH)GetStockObject(1);
  76.     windowsClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  77.     windowsClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  78.     windowsClass.hInstance = hInstance;
  79.     windowsClass.lpfnWndProc = WindowProc;
  80.     windowsClass.lpszClassName = "Simple WIndow";
  81.     RegisterClassEx(&windowsClass);
  82.     hWnd = CreateWindow(windowsClass.lpszClassName, "DrawWindow", WS_OVERLAPPED |WS_CAPTION | WS_SYSMENU  | WS_MINIMIZEBOX,
  83.         (GetSystemMetrics(SM_CXSCREEN) - width) / 2, (GetSystemMetrics(SM_CYSCREEN) - height) / 2, width, height, NULL, NULL, NULL, NULL);
  84.     CreateWindow("static", " Толщина границы:", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 5, 5, 165, 90, hWnd, NULL, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
  85.     CreateWindow("EDIT", "3", WS_BORDER|WS_VISIBLE|WS_CHILD|ES_LEFT|ES_MULTILINE, 135, 7, 30, 17, hWnd, (HMENU)ID_TEXTBOX, NULL, NULL);
  86.     CreateWindowEx(0, "button", "Цвет кисти", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 30, 100, 25, hWnd, (HMENU)ID_Brush, NULL, NULL);
  87.     CreateWindowEx(0, "button", "Цвет пера", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 60, 100, 25, hWnd, (HMENU)ID_Pen, NULL, NULL);
  88.     CreateWindow("static", " Фигуры", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 5, 100, 165, 90, hWnd, NULL, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
  89.     CreateWindowEx(0, "button", "Квадрат", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 125, 100, 25, hWnd, (HMENU)ID_butttonSquare, NULL, NULL);
  90.     CreateWindowEx(0, "button", "Ромб", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 155, 100, 25, hWnd, (HMENU)ID_buttonRhombus, NULL, NULL);
  91.     CreateWindow("static", " Файлы", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 5, 195, 165, 120, hWnd, NULL, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
  92.     CreateWindowEx(0, "button", "Считать", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 220, 100, 25, hWnd, (HMENU)ID_getFileOne, NULL, NULL);
  93.     CreateWindowEx(0, "button", "Считать все", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 250, 100, 25, hWnd, (HMENU)ID_getFileAll, NULL, NULL);
  94.     CreateWindowEx(0, "button", "Сохранить", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 35, 280, 100, 25, hWnd, (HMENU)ID_saveFile, NULL, NULL);  
  95.     ShowWindow(hWnd, nCmdShow);
  96.     WNDCLASS windowsClass2;
  97.     memset(&windowsClass2,0,sizeof(WNDCLASS));
  98.     windowsClass2.lpfnWndProc = PaintProc;
  99.     windowsClass2.hInstance = hInstance;
  100.     windowsClass2.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  101.     windowsClass2.lpszClassName = "ChildWClass";
  102.     windowsClass2.hCursor=LoadCursor(NULL,IDC_CROSS);
  103.     RegisterClass(&windowsClass2);
  104.     hWndPaint=CreateWindowEx(0,"ChildWClass",(LPCTSTR) NULL, WS_CHILD | WS_BORDER | WS_VISIBLE, 180, 0, 1084, 681, hWnd, NULL, hInstance, NULL);
  105.     ShowWindow(hWndPaint, SW_NORMAL);
  106.     UpdateWindow(hWndPaint);
  107.     SetActiveWindow(hWnd);
  108.     cRefPen = COLORREF(RGB(0,0,0));
  109.     cRefBrush = COLORREF(RGB(255,0,0));
  110.     hdc = GetDC(hWndPaint);
  111.     while (GetMessage(&uMsg, hWnd, NULL, NULL))
  112.     {
  113.         TranslateMessage(&uMsg);
  114.         DispatchMessage(&uMsg);
  115.     }
  116.     return uMsg.wParam;
  117. }
  118.  
  119. LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  120. {
  121.     switch (uMsg)
  122.     {      
  123.     case WM_MOUSEWHEEL:
  124.         if(chekParams)
  125.         {
  126.             int m = Container.size() - 1;
  127.                 nowFigure = Container[m];
  128.             if (GET_WHEEL_DELTA_WPARAM(wParam) > 0)
  129.                 if (GetAsyncKeyState(VK_SHIFT))
  130.                     angle += 0.05f;
  131.                 else
  132.                     scale += 0.05f;
  133.             else
  134.                 if (GetAsyncKeyState(VK_SHIFT))
  135.                     angle = angle - 0.05f;
  136.                 else
  137.                     if (scale > 0.05f) scale -= 0.05f;
  138.             reloadPaint(Container.size() - 1);
  139.             SelectObject(hdc, nowFigure.hP);
  140.             SelectObject(hdc, nowFigure.hB);
  141.             nowFigure.p = rotateFigureToAngle(nowFigure.p, nowFigure.center, angle);
  142.             nowFigure.p = scaleFigure(nowFigure.p, nowFigure.center, scale);
  143.             Polygon(hdc, nowFigure.p, 4);
  144.         }
  145.         break;
  146.  
  147.     case WM_CLOSE:
  148.         ExitProcess(0);
  149.         break;
  150.  
  151.     case WM_COMMAND:
  152.         chekParams = false;
  153.         int m;
  154.         m = Container.size();
  155.         if (m)
  156.             Container[m - 1] = nowFigure;
  157.         switch (LOWORD(wParam))
  158.         {
  159.         case ID_butttonSquare:
  160.             //SetWindowText(hLabelWnd, "квадрат");
  161.             isSquare = true;
  162.             break;
  163.  
  164.         case ID_buttonRhombus:
  165.             isRhombus = true;
  166.             break;
  167.  
  168.         case ID_Brush:
  169.             cRefBrush = chooseColor();
  170.             break;
  171.            
  172.         case ID_Pen:
  173.             cRefPen = chooseColor();
  174.             break;
  175.            
  176.         case ID_getFileOne:
  177.             readFromFile("Фигуры.txt", 0);
  178.             break;
  179.  
  180.         case ID_getFileAll:
  181.             readFromFile("Фигуры.txt", 1);
  182.             chekParams = false;
  183.             break;
  184.  
  185.         case ID_saveFile:
  186.             {
  187.             static HDC hdcEMF;
  188.             hdcEMF = CreateEnhMetaFile(NULL, "рисунок.EMF", NULL, "CreateMetafile\0Pict1\0");
  189.             int count = Container.size();
  190.             for (int i = 0; i < count; i++)
  191.             {
  192.                 SelectObject(hdcEMF, Container[i].hP);
  193.                 SelectObject(hdcEMF, Container[i].hB);
  194.                 Polygon(hdcEMF, Container[i].p, 4);
  195.             }
  196.             HENHMETAFILE hEMF = CloseEnhMetaFile(hdcEMF);
  197.             DeleteEnhMetaFile(hEMF);
  198.             }
  199.             break;
  200.  
  201.         default:
  202.             break;
  203.         }
  204.  
  205.     default:
  206.         break;
  207.     }
  208.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  209. }
  210. LRESULT CALLBACK PaintProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  211. {
  212.     switch (uMsg)
  213.     {
  214.         case WM_LBUTTONDOWN:
  215.             chekParams = false;
  216.             if (isSquare || isRhombus)
  217.             {
  218.                 int m = Container.size();
  219.                 if (m)
  220.                     Container[m - 1] = nowFigure;
  221.                 getPen(); //
  222.                 hBrush = CreateSolidBrush(cRefBrush);
  223.                 hdc = GetDC(hWndPaint);
  224.                 SelectObject(hdc, hPen);
  225.                 SelectObject(hdc, hBrush);
  226.                 int x, y;
  227.                 x = LOWORD(lParam);
  228.                 y = HIWORD(lParam);
  229.                 if (isSquare)
  230.                 {
  231.                     nowFigure.center.x = x + 50;
  232.                     nowFigure.center.y = y + 50;
  233.                     nowFigure.p = PointSquare(x, y);
  234.                     isSquare = false;
  235.                 }
  236.                 else
  237.                 {
  238.                     nowFigure.center.x = x + 50;
  239.                     nowFigure.center.y = y;
  240.                     nowFigure.p = PointRhombus(x, y);
  241.                     isRhombus = false;
  242.                 }
  243.                 nowFigure.hB = hBrush;
  244.                 nowFigure.hP = hPen;
  245.                 Polygon(hdc, nowFigure.p, 4);
  246.                 Container.push_back(nowFigure);
  247.                 chekParams = true;
  248.                 angle = 0;
  249.                 scale = 1;
  250.             }
  251.         break;
  252.  
  253.         default:
  254.             break;
  255.     }
  256.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  257. }
  258.  
  259. void readFromFile(char *filename, bool all)
  260. {
  261.     try
  262.     {
  263.         if (!File.is_open())
  264.             File.open(filename, std::ios::in);
  265.         static int i = 0, n;
  266.         int type, j, Wd;
  267.         int *color = new int [3];
  268.         if (!i) File >> n;
  269.         for (; i < n; i++)
  270.         {
  271.             File >> type;
  272.             POINT *p = new POINT[4];
  273.             for(j = 0; j < 4; j++)
  274.                 File >> p[j].x >> p[j].y;
  275.             nowFigure.p = p;
  276.             for (j = 0; j < 3; j++) File >> color[j];
  277.             nowFigure.hB = CreateSolidBrush(RGB(color[0], color[1], color[2]));
  278.             for (j = 0; j < 3; j++) File >> color[j];
  279.             File >> Wd;
  280.             nowFigure.hP = CreatePen(PS_SOLID, Wd, RGB(color[0], color[1], color[2]));
  281.             nowFigure.center = centerFigure(nowFigure.p);
  282.             if (type == 100001) // квадрат
  283.             {
  284.                 if (isSquarePoints(nowFigure.p))
  285.                 {
  286.                     Container.push_back(nowFigure);
  287.                     chekParams = true;
  288.                     SelectObject(hdc, nowFigure.hP);
  289.                     SelectObject(hdc, nowFigure.hB);
  290.                     Polygon(hdc, nowFigure.p, 4);
  291.                 }
  292.                 else
  293.                     MessageBox(hWnd, TEXT("Некорректные входные данные"), TEXT("Ошибка"), NULL);
  294.             }
  295.             else // ромб
  296.             {
  297.                 if (isRhombusPoints(nowFigure.p))
  298.                 {
  299.                     Container.push_back(nowFigure);
  300.                     chekParams = true;
  301.                     SelectObject(hdc, nowFigure.hP);
  302.                     SelectObject(hdc, nowFigure.hB);
  303.                     Polygon(hdc, nowFigure.p, 4);
  304.                 }
  305.                 else
  306.                     MessageBox(hWnd, TEXT("Некорректные входные данные"), TEXT("Ошибка"), NULL);
  307.             }
  308.             if (!all)
  309.             {
  310.                 i++;
  311.                 break;
  312.             }
  313.         }
  314.         if (i == n)
  315.         {
  316.             i = 0;
  317.             File.close();
  318.         }
  319.     }
  320.     catch (int correct)
  321.     {
  322.         MessageBox(hWnd, TEXT("Не удалось выполнить чтение из файла"), TEXT("Ошибка"), NULL);
  323.     }
  324. }
  325. void reloadPaint(int count)
  326. {
  327.     InvalidateRect(hWndPaint, 0, 1);
  328.     ReleaseDC(hWndPaint, hdc);
  329.     UpdateWindow(hWndPaint);
  330.     hdc = GetDC(hWndPaint);
  331.     for (int i = 0; i < count; i++)
  332.     {
  333.         SelectObject(hdc, Container[i].hP);
  334.         SelectObject(hdc, Container[i].hB);
  335.         Polygon(hdc, Container[i].p, 4);
  336.     }
  337. }
  338. COLORREF chooseColor()
  339. {
  340.     CHOOSECOLOR cc;
  341.     static COLORREF acrCustClr[16];
  342.     static DWORD rgbCurrent;
  343.  
  344.     ZeroMemory(&cc, sizeof(cc));
  345.     cc.lStructSize = sizeof(cc);
  346.     cc.hwndOwner = hWnd;
  347.     cc.lpCustColors = (LPDWORD) acrCustClr;
  348.     cc.rgbResult = rgbCurrent;
  349.     cc.Flags = CC_FULLOPEN | CC_RGBINIT;
  350.  
  351.     if (ChooseColor(&cc)==TRUE)
  352.         return cc.rgbResult;
  353.     return NULL;
  354. }
  355. void getPen()
  356. {
  357.     char str[3];
  358.     GetDlgItemText(hWnd, ID_TEXTBOX, str, sizeof(str));
  359.     int penWidth;
  360.     penWidth = atoi(str);
  361.     if (penWidth > 0)
  362.         hPen = CreatePen(PS_SOLID, penWidth, cRefPen);
  363.     else
  364.         hPen = CreatePen(PS_SOLID, 3, cRefPen);
  365. }
  366. POINT *scaleFigure(POINT *pSource, POINT center, float scale)
  367. {
  368.     POINT *p = new POINT[4];
  369.     for(int i = 0; i < 4; i++)
  370.         p[i] = scalePoint(pSource[i], center, scale);
  371.     return p;
  372. }
  373. POINT scalePoint(POINT pSource, POINT center, float scale)
  374. {
  375.     POINT p;
  376.     p.x = LONG((pSource.x - center.x) * scale + center.x);
  377.     p.y = LONG((pSource.y - center.y) * scale + center.y);
  378.     return p;
  379. }
  380. POINT *rotateFigureToAngle(POINT *pSource, POINT center, float alpha)
  381. {
  382.     POINT *p = new POINT[4];
  383.     for(int i = 0; i < 4; i++)
  384.         p[i] = rotatePointToAngle(pSource[i], center, alpha);
  385.     return p;
  386. }
  387. POINT rotatePointToAngle(POINT pSource, POINT center, float alpha)
  388. {
  389.     POINT p;
  390.     int x = pSource.x, x0 = center.x, y = pSource.y, y0 = center.y;
  391.     p.x = LONG(x0 + (x - x0) * cos(alpha) - (y - y0) * sin(alpha));
  392.     p.y = LONG(y0 + (y - y0) * cos(alpha) + (x - x0) * sin(alpha));
  393.     return p;
  394. }
  395. float NormaVector(POINT *vect)
  396. {
  397.     return (float)sqrt(vect->x * vect->x + vect->y * vect->y);
  398. }
  399. bool Сollinearity(POINT *vect1, POINT *vect2)
  400. {
  401.     if (vect1->x * vect2->y == vect2->x * vect1->y) return true;
  402.     return false;
  403. }
  404. POINT VectorFromPoints(POINT *p1, POINT *p2)
  405. {
  406.     POINT vect;
  407.     vect.x = p2->x - p1->x;
  408.     vect.y = p2->y - p1->y;
  409.     return vect;
  410. }
  411. bool isSquarePoints(POINT *p)
  412. {
  413.     POINT vector1, vector2;
  414.     vector1 = VectorFromPoints(&p[0], &p[1]);
  415.     vector2 = VectorFromPoints(&p[2], &p[3]);
  416.     if (Сollinearity(&vector1, &vector2))
  417.     {
  418.         vector1 = VectorFromPoints(&p[1], &p[2]);
  419.         if (abs( NormaVector(&vector1) - NormaVector(&vector2)) < 0.1)
  420.         {
  421.             return true;
  422.         }
  423.     }
  424.     return false;
  425. }
  426. bool isRhombusPoints(POINT *p)
  427. {
  428.     POINT vector1, vector2;
  429.     vector1 = VectorFromPoints(&p[0], &p[1]);
  430.     vector2 = VectorFromPoints(&p[2], &p[3]);
  431.     if (Сollinearity(&vector1, &vector2))
  432.     {
  433.         vector1 = VectorFromPoints(&p[0], &p[2]);
  434.         vector1 = VectorFromPoints(&p[1], &p[3]);
  435.         if (abs( NormaVector(&vector1) - NormaVector(&vector2)) < 0.1)
  436.         {
  437.             return true;
  438.         }
  439.     }
  440.     return false;
  441. }
  442. POINT centerFigure(POINT *p)
  443. {
  444.     POINT center, vector1;
  445.     vector1 = VectorFromPoints(&p[0], &p[2]);
  446.     center.x = p[0].x + vector1.x / 2;
  447.     center.y = p[0].y + vector1.y / 2;
  448.     return center;
  449. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement