Advertisement
Combreal

ImgFractionCalc

Feb 20th, 2017 (edited)
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.33 KB | None | 0 0
  1. #include <windows.h>
  2. #include <string>
  3. #include <algorithm>
  4. #include <commctrl.h>
  5. #include <vector>
  6. #include <fstream>
  7. //#include "resource.h"
  8.  
  9. typedef struct
  10. {
  11.     POINT smallestCoords;
  12.     POINT bigestCoords;
  13. }Coords;
  14.  
  15.  
  16. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  17. std::wstring OpenDialog(HWND hwnd);
  18. void ResetWindow(HWND hwnd);
  19. void DrawLines(HWND hwnd);
  20. void ReDrawCross(HWND hwnd);
  21. void DrawCrossOnClick(HWND hwnd);
  22. void DrawRect(HWND hwnd);
  23. void DrawLine(HWND hwnd);
  24. void saveCoords(POINT smallestCoords, POINT bigestCoords);
  25. void DrawAllRect(HWND hwnd);
  26. void DrawAllLine(HWND hwnd);
  27. bool isMouseInrect();
  28. int getRect();
  29. double getSwFract(int mouseX);
  30. double getShFract(int mouseY);
  31. #define ID_BUTTON 1
  32. #define ID_DROPLIST 2
  33. HWND hwndButton, hwndComboBox;
  34. TCHAR NPath[MAX_PATH];
  35. LPCWSTR Path;
  36. bool FSon;
  37. bool drawLine;
  38. bool delLastCross;
  39. bool drawRect;
  40. bool savePhase;
  41. bool drawAllRect;
  42. bool crossIsDrawing;
  43. bool lineMod = false;
  44. bool rectangleMod = false;
  45. bool loadCoords = false;
  46. int crossCounter;
  47. std::vector<int> xCoords;
  48. std::vector<int> yCoords;
  49. std::vector<Coords> CoordsList;
  50.  
  51. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow)
  52. {
  53.     crossCounter = -1;
  54.     FSon = false;
  55.     drawLine = false;
  56.     delLastCross = false;
  57.     drawRect = false;
  58.     savePhase = false;
  59.     drawAllRect = false;
  60.     crossIsDrawing = false;
  61.     MSG  msg;
  62.     HWND hwnd;
  63.     WNDCLASSEXW wc;
  64.     wc.cbSize = sizeof(WNDCLASSEX);
  65.     wc.style = 0;
  66.     wc.lpfnWndProc = WndProc;
  67.     wc.cbClsExtra = 0;
  68.     wc.cbWndExtra = 0;
  69.     wc.hInstance = hInstance;  
  70.     wc.hIcon = NULL; //wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  71.     wc.hCursor = NULL;
  72.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //(HBRUSH)(COLOR_WINDOW+1);
  73.     wc.lpszMenuName = NULL;
  74.     wc.lpszClassName = L"ImgFractionCalc";
  75.     wc.hIconSm = NULL; //wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
  76.     if (!RegisterClassExW(&wc))
  77.     {
  78.         MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
  79.         return 0;
  80.     }
  81.     hwnd = CreateWindowExW(NULL, L"ImgFractionCalc", L"ImgFrac", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME, GetSystemMetrics(SM_CXSCREEN) / 2, GetSystemMetrics(SM_CYSCREEN) / 2, 150, 108, 0, 0, hInstance, 0); //WS_OVERLAPPEDWINDOW //WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
  82.     if (hwnd == NULL)
  83.     {
  84.         MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
  85.         return 0;
  86.     }
  87.     DOUBLE dwRet = GetCurrentDirectory(MAX_PATH, NPath);
  88.     if (dwRet == 0)
  89.     {
  90.         wchar_t buff[256];
  91.         FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buff, 256, NULL);
  92.         MessageBoxW(NULL, buff, L"Error", MB_OK);
  93.     }
  94.     ShowWindow(hwnd, nCmdShow);
  95.     UpdateWindow(hwnd);
  96.     while (GetMessage(&msg, NULL, 0, 0))
  97.     {
  98.         TranslateMessage(&msg);
  99.         DispatchMessage(&msg);
  100.     }
  101.     return (int)msg.wParam;
  102. }
  103.  
  104. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  105. {
  106.     TCHAR BPath[MAX_PATH];
  107.     strcpy_s(BPath, NPath);
  108.     static HBITMAP hBitmap;
  109.     HDC hdc;
  110.     PAINTSTRUCT ps;
  111.     BITMAP bitmap;
  112.     HDC hdcMem;
  113.     HGDIOBJ oldBitmap;
  114.     std::wstring Path;
  115.     switch (msg)
  116.     {
  117.     case WM_LBUTTONDOWN:
  118.         if (FSon)
  119.         {
  120.             drawLine = true;
  121.             RedrawWindow(hwnd, NULL, NULL, RDW_INTERNALPAINT);
  122.         }
  123.         break;
  124.     case WM_RBUTTONDOWN:
  125.         if (FSon)
  126.         {
  127.             if (xCoords.size() > 0 && yCoords.size() > 0)
  128.             {
  129.                 delLastCross = true;
  130.                 RECT theRect;
  131.                 GetClientRect(hwnd, &theRect);
  132.                 InvalidateRect(hwnd, &theRect, TRUE);
  133.                 //redraw all rect here
  134.                 //DrawRect(hwnd); //not working
  135.                 //DrawAllRect(hwnd); //not working
  136.                 /*if ((CoordsList.size() != 0) && !crossIsDrawing)
  137.                 {
  138.                     drawAllRect = true;
  139.                 }
  140.                 drawRect = true;*/
  141.                 savePhase = false;
  142.             }
  143.         }
  144.         break;
  145.     case WM_MBUTTONDOWN:
  146.         if (FSon && isMouseInrect())
  147.         {
  148.             CoordsList.erase(CoordsList.begin() + getRect());
  149.             drawAllRect = true;
  150.         }
  151.         break;
  152.     case WM_KEYDOWN:
  153.         if (wParam == VK_ESCAPE)
  154.         {
  155.             int rekt = MessageBoxW(hwnd, L"Save and quit?", L"", MB_YESNOCANCEL);
  156.             if (rekt == IDYES)
  157.             {
  158.                 if (CoordsList.size() != 0)
  159.                 {
  160.                     if (!SetCurrentDirectory(BPath))
  161.                     {
  162.                         wchar_t buff[256];
  163.                         FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buff, 256, NULL);
  164.                         MessageBoxW(hwnd, buff, L"Error", MB_OK);
  165.                     }
  166.                     std::ifstream infile("C:\\Temp\\coordinates.txt");
  167.                     if (infile.good())
  168.                     {
  169.                         int rek = MessageBoxW(hwnd, L"File exists, do you want to replace it?", L"", MB_YESNO);
  170.                         if (rek == IDYES)
  171.                         {
  172.                             std::ofstream file("coordinates.txt", std::ios::out | std::ios::trunc);
  173.                             for (size_t CoordsListSize = 0, size = CoordsList.size(); CoordsListSize < size; ++CoordsListSize)
  174.                             {
  175.                                 file << getSwFract(CoordsList.at(CoordsListSize).smallestCoords.x) << " " << getShFract(CoordsList.at(CoordsListSize).smallestCoords.y) << " " << getSwFract(CoordsList.at(CoordsListSize).bigestCoords.x) << " " << getShFract(CoordsList.at(CoordsListSize).bigestCoords.y) << "\n";
  176.                             }
  177.                             file.close();
  178.                         }
  179.                     }
  180.                     else
  181.                     {
  182.                         std::ofstream file("C:\\Temp\\coordinates.txt", std::ios::out | std::ios::trunc);
  183.                         for (size_t CoordsListSize = 0, size = CoordsList.size(); CoordsListSize < size; ++CoordsListSize)
  184.                         {
  185.                             file << getSwFract(CoordsList.at(CoordsListSize).smallestCoords.x) << " " << getShFract(CoordsList.at(CoordsListSize).smallestCoords.y) << " " << getSwFract(CoordsList.at(CoordsListSize).bigestCoords.x) << " " << getShFract(CoordsList.at(CoordsListSize).bigestCoords.y) << "\n";
  186.                         }
  187.                         file.close();
  188.                     }
  189.                 }
  190.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  191.             }
  192.             else if (rekt == IDNO)
  193.             {
  194.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  195.             }
  196.         }
  197.         else if (wParam == 'S' && GetAsyncKeyState(VK_CONTROL))
  198.         {
  199.             if (!SetCurrentDirectory(BPath))
  200.             {
  201.                 wchar_t buff[256];
  202.                 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buff, 256, NULL);
  203.                 MessageBoxW(hwnd, buff, L"Error", MB_OK);
  204.             }
  205.  
  206.             std::ofstream file("C:\\Temp\\coordinates.txt", std::ios::out | std::ios::trunc);
  207.             for (size_t CoordsListSize = 0, size = CoordsList.size(); CoordsListSize < size; ++CoordsListSize)
  208.             {
  209.                 file << getSwFract(CoordsList.at(CoordsListSize).smallestCoords.x) << " " << getShFract(CoordsList.at(CoordsListSize).smallestCoords.y) << " " << getSwFract(CoordsList.at(CoordsListSize).bigestCoords.x) << " " << getShFract(CoordsList.at(CoordsListSize).bigestCoords.y) << "\n";
  210.             }
  211.             file.close();
  212.         }
  213.         else if ((wParam == VK_RETURN) && FSon)
  214.         {
  215.             if (!savePhase)
  216.             {
  217.                 /*if ((CoordsList.size() != 0) && !crossIsDrawing)
  218.                 {
  219.                     drawAllRect = true;
  220.                 }*/
  221.                 drawRect = true;
  222.                 drawAllRect = true;
  223.                 RECT theRect;
  224.                 GetClientRect(hwnd, &theRect);
  225.                 InvalidateRect(hwnd, &theRect, TRUE);
  226.             }
  227.             else if (savePhase)
  228.             {
  229.                 /*int rektt = MessageBoxW(hwnd, L"Save Rectangle coordinates?", L"", MB_OKCANCEL);
  230.                 if (rektt != IDOK)
  231.                 {
  232.                     CoordsList.pop_back();
  233.                 }
  234.                 RECT theRect;
  235.                 GetClientRect(hwnd, &theRect);
  236.                 InvalidateRect(hwnd, &theRect, TRUE);
  237.                 savePhase = false;
  238.                 drawAllRect = true;*/
  239.             }
  240.         }
  241.         break;
  242.     case WM_CREATE:
  243.         hwndComboBox = CreateWindow(WC_COMBOBOX, TEXT("ComboBox"), CBS_DROPDOWNLIST | WS_CHILD | WS_VISIBLE | WS_OVERLAPPED | CBS_HASSTRINGS, 20, 9, 100, 200, hwnd, (HMENU)ID_DROPLIST, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); // | WS_OVERLAPPED | CBS_HASSTRINGS 
  244.         SendMessage(hwndComboBox, CB_ADDSTRING, (UINT)0, (LPARAM)"Lines");
  245.         SendMessage(hwndComboBox, CB_ADDSTRING, (UINT)1, (LPARAM)"Rectangles");
  246.         SendMessage(hwndComboBox, CB_SETCURSEL, 0, 0);
  247.         hwndButton = CreateWindowW(L"Button", L"Open file", WS_VISIBLE | WS_CHILD, 30, 39, 75, 23, hwnd, (HMENU)ID_BUTTON, NULL, NULL);
  248.         SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_MAXIMIZEBOX);
  249.         break;
  250.     case WM_PAINT:
  251.         hdc = BeginPaint(hwnd, &ps);
  252.         hdcMem = CreateCompatibleDC(hdc);
  253.         oldBitmap = SelectObject(hdcMem, hBitmap);
  254.         GetObject(hBitmap, sizeof(bitmap), &bitmap);
  255.         BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);
  256.         SetStretchBltMode(hdc, HALFTONE);
  257.         StretchBlt(hdc, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
  258.         if (drawLine && !savePhase)
  259.         {
  260.             DrawCrossOnClick(hwnd);
  261.             drawLine = false;
  262.         }
  263.         else if (delLastCross && !savePhase)
  264.         {
  265.             ReDrawCross(hwnd);
  266.             if (lineMod)
  267.             {
  268.                 DrawAllLine(hwnd);
  269.             }
  270.             else
  271.             {
  272.                 DrawAllRect(hwnd);
  273.             }
  274.             delLastCross = false;
  275.         }
  276.         else if (drawRect && !savePhase)
  277.         {
  278.             if (lineMod)
  279.             {
  280.                 DrawLine(hwnd);
  281.                 DrawAllLine(hwnd);
  282.             }
  283.             else
  284.             {
  285.                 DrawRect(hwnd);
  286.                 DrawAllRect(hwnd);
  287.             }
  288.             drawRect = false;
  289.         }
  290.         else if (loadCoords && !savePhase)
  291.         {
  292.             if (lineMod)
  293.             {
  294.                 DrawLine(hwnd);
  295.                 DrawAllLine(hwnd);
  296.             }
  297.             else
  298.             {
  299.                 DrawRect(hwnd);
  300.                 DrawAllRect(hwnd);
  301.             }
  302.         }
  303.         else if (drawAllRect)
  304.         {
  305.             /*if (lineMod)
  306.             {
  307.                 DrawAllLine(hwnd);
  308.             }
  309.             else
  310.             {
  311.                 DrawAllRect(hwnd);
  312.             }*/
  313.             drawAllRect = false;
  314.         }
  315.         SelectObject(hdcMem, oldBitmap);
  316.         DeleteDC(hdcMem);
  317.         EndPaint(hwnd, &ps);
  318.         break;
  319.     case WM_COMMAND:
  320.         if (LOWORD(wParam) == ID_BUTTON)
  321.         {
  322.             int ItemIndex = SendMessage(hwndComboBox, CB_GETCURSEL, (WPARAM)0, (LPARAM)0);
  323.             if (ItemIndex == 0)
  324.             {
  325.                 lineMod = true;
  326.             }
  327.             else
  328.             {
  329.                 rectangleMod = true;
  330.             }
  331.             ShowWindow(hwndComboBox, SW_HIDE);
  332.             Path = OpenDialog(hwnd);
  333.             ResetWindow(hwnd);
  334.             hBitmap = (HBITMAP)LoadImageW(NULL, Path.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);//Path.c_str() //L"C:\\worlda.bmp"
  335.             if (hBitmap == NULL)
  336.             {
  337.                 wchar_t buff[256];
  338.                 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buff, 256, NULL);
  339.                 MessageBoxW(hwnd, buff, L"Error", MB_OK);
  340.             }
  341.             break;
  342.         }
  343.         break;
  344.     case WM_DESTROY:
  345.         DeleteObject(hBitmap);
  346.         PostQuitMessage(0);
  347.         break;
  348.     default:
  349.         break;
  350.     }
  351.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  352. }
  353.  
  354. std::wstring OpenDialog(HWND hwnd)
  355. {
  356.     std::wstring Path;
  357.     OPENFILENAME ofn;
  358.     TCHAR szFile[MAX_PATH];
  359.     ZeroMemory(&ofn, sizeof(ofn));
  360.     ofn.lStructSize = sizeof(ofn);
  361.     ofn.lpstrFile = szFile;
  362.     ofn.lpstrFile[0] = '\0';
  363.     ofn.hwndOwner = hwnd;
  364.     ofn.nMaxFile = sizeof(szFile);
  365.     ofn.lpstrFilter = TEXT("Image Files\0*.bmp\0Any File\0*.*\0");
  366.     ofn.nFilterIndex = 1;
  367.     ofn.lpstrTitle = TEXT("Select an image");
  368.     ofn.lpstrInitialDir = NPath;
  369.     ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
  370.     if (GetOpenFileName(&ofn))
  371.     {
  372.         OutputDebugString(ofn.lpstrFile);
  373.         std::string str = ofn.lpstrFile;
  374.         int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
  375.         std::wstring wPath(size_needed, 0);
  376.         MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wPath[0], size_needed);
  377.         Path = wPath;
  378.     }
  379.     return Path;
  380. }
  381.  
  382. void ResetWindow(HWND hwnd)
  383. {
  384.     HDC hdc;
  385.     hdc = GetDC(hwnd);
  386.     RECT theRect;
  387.     ReleaseDC(hwnd, hdc);
  388.     GetClientRect(hwnd, &theRect);
  389.     InvalidateRect(hwnd, &theRect, TRUE);
  390.     DestroyWindow(hwndButton);
  391.     InvalidateRect(hwndButton, NULL, TRUE);
  392.     UpdateWindow(hwndButton);
  393.     SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_THICKFRAME);
  394.     SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & WS_MAXIMIZEBOX);
  395.     //SetWindowLongPtr(hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW | WS_EX_TOPMOST);
  396.     SetWindowLongPtr(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
  397.     SetWindowPos(hwnd, HWND_TOP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_SHOWWINDOW); //HWND_TOPMOST
  398.     FSon = true;
  399.     std::string usedString, coords;
  400.     std::string delimiter = " ";
  401.     size_t pos = 0;
  402.     std::ifstream infile("C:\\Temp\\coordinates.txt");
  403.     int counter = 0;
  404.     Coords C1;
  405.     int screenW = GetSystemMetrics(SM_CXSCREEN);
  406.     int screenH = GetSystemMetrics(SM_CYSCREEN);
  407.     if (infile.good())
  408.     {
  409.         while (getline(infile, usedString, '\n'))
  410.         {
  411.             while ((pos = usedString.find(delimiter)) != std::string::npos)
  412.             {
  413.                 coords = usedString.substr(0, pos);
  414.                 switch (counter)
  415.                 {
  416.                 case 0:
  417.                     C1.smallestCoords.x = (int)(std::stod(coords) * (double)screenW);
  418.                     break;
  419.                 case 1:
  420.                     C1.smallestCoords.y = (int)(std::stod(coords) * (double)screenH);
  421.                     break;
  422.                 case 2:
  423.                     C1.bigestCoords.x = (int)(std::stod(coords) * (double)screenW);
  424.                     break;
  425.                 default:
  426.                     break;
  427.                 }
  428.                 usedString.erase(0, pos + delimiter.length());
  429.                 counter++;
  430.                 if ((pos = usedString.find(delimiter)) == std::string::npos)
  431.                 {
  432.                     coords = usedString.substr(0, pos);
  433.                     C1.bigestCoords.y = (int)(std::stod(coords) * (double)screenH);
  434.                 }
  435.             }
  436.         }
  437.         CoordsList.push_back(C1);
  438.         infile.close();
  439.         loadCoords = true;
  440.     }
  441. }
  442.  
  443. void DrawLines(HWND hwnd)
  444. {
  445.     HDC hdc = GetDC(hwnd);
  446.     HPEN hPen = CreatePen(PS_SOLID, 20, RGB(255, 0, 0));
  447.     HPEN holdPen = static_cast<HPEN>(SelectObject(hdc, hPen));
  448.     MoveToEx(hdc, 50, 350, NULL);
  449.     LineTo(hdc, 500, 350);
  450.     SelectObject(hdc, holdPen);
  451.     DeleteObject(hPen);
  452. }
  453.  
  454. void DrawCrossOnClick(HWND hwnd)
  455. {
  456.     crossCounter++;
  457.     POINT mouseCoords;
  458.     GetCursorPos(&mouseCoords);
  459.     xCoords.push_back(mouseCoords.x);
  460.     yCoords.push_back(mouseCoords.y);
  461.     HDC hdc = GetDC(hwnd);
  462.     HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  463.     HPEN holdPen = static_cast<HPEN>(SelectObject(hdc, hPen));
  464.     MoveToEx(hdc, mouseCoords.x - 8, mouseCoords.y, NULL);
  465.     LineTo(hdc, mouseCoords.x + 7, mouseCoords.y);
  466.     MoveToEx(hdc, mouseCoords.x, mouseCoords.y - 7, NULL);
  467.     LineTo(hdc, mouseCoords.x, mouseCoords.y + 7);
  468.     SelectObject(hdc, holdPen);
  469.     DeleteObject(hPen);
  470.     crossIsDrawing = true;
  471. }
  472.  
  473. void ReDrawCross(HWND hwnd)
  474. {
  475.     if (xCoords.size() > 0 && yCoords.size() > 0)
  476.     {
  477.         crossCounter--;
  478.         HDC hdc = GetDC(hwnd);
  479.         HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  480.         HPEN holdPen = static_cast<HPEN>(SelectObject(hdc, hPen));
  481.         for (size_t h = 0, size = xCoords.size(); h < size - 1; ++h)
  482.         {
  483.             MoveToEx(hdc, xCoords.at(h) - 8, yCoords.at(h), NULL);
  484.             LineTo(hdc, xCoords.at(h) + 7, yCoords.at(h));
  485.             MoveToEx(hdc, xCoords.at(h), yCoords.at(h) - 7, NULL);
  486.             LineTo(hdc, xCoords.at(h), yCoords.at(h) + 7);
  487.         }
  488.         xCoords.pop_back();
  489.         yCoords.pop_back();
  490.         SelectObject(hdc, holdPen);
  491.         DeleteObject(hPen);
  492.     }
  493. }
  494.  
  495. void DrawRect(HWND hwnd)
  496. {
  497.     if (xCoords.size() == 4 && yCoords.size() == 4)
  498.     {
  499.         HDC hdc = GetDC(hwnd);
  500.         HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  501.         HPEN holdPen = static_cast<HPEN>(SelectObject(hdc, hPen));
  502.         POINT smallestCoords;
  503.         POINT bigestCoords;
  504.         std::sort(xCoords.begin(), xCoords.end());
  505.         std::sort(yCoords.begin(), yCoords.end());
  506.         smallestCoords.x = xCoords.at(0);
  507.         smallestCoords.y = yCoords.at(0);
  508.         bigestCoords.x = xCoords.at(3);
  509.         bigestCoords.y = yCoords.at(3);
  510.         saveCoords(smallestCoords, bigestCoords);
  511.         MoveToEx(hdc, smallestCoords.x, smallestCoords.y, NULL);
  512.         LineTo(hdc, bigestCoords.x, smallestCoords.y);
  513.         MoveToEx(hdc, bigestCoords.x, smallestCoords.y, NULL);
  514.         LineTo(hdc, bigestCoords.x, bigestCoords.y);
  515.         MoveToEx(hdc, smallestCoords.x, bigestCoords.y, NULL);
  516.         LineTo(hdc, bigestCoords.x, bigestCoords.y);
  517.         MoveToEx(hdc, smallestCoords.x, smallestCoords.y, NULL);
  518.         LineTo(hdc, smallestCoords.x, bigestCoords.y);
  519.         xCoords.clear();
  520.         yCoords.clear();
  521.         SelectObject(hdc, holdPen);
  522.         DeleteObject(hPen);
  523.         if (CoordsList.size() != 0)
  524.         {
  525.             DrawAllRect(hwnd);
  526.         }
  527.         //savePhase = true;
  528.     }
  529. }
  530.  
  531. void DrawLine(HWND hwnd)
  532. {
  533.     if (xCoords.size() == 2 && yCoords.size() == 2)
  534.     {
  535.         HDC hdc = GetDC(hwnd);
  536.         HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  537.         HPEN holdPen = static_cast<HPEN>(SelectObject(hdc, hPen));
  538.         POINT firstPoint;
  539.         POINT secondPoint;
  540.         int xDiff, yDiff;
  541.         xDiff = xCoords.at(0) - xCoords.at(1);
  542.         yDiff = yCoords.at(0) - yCoords.at(1);
  543.         if (std::signbit((double)xDiff))
  544.         {
  545.             xDiff = -xDiff;
  546.         }
  547.         else if (std::signbit((double)yDiff))
  548.         {
  549.             yDiff = -yDiff;
  550.         }
  551.         if (xDiff < yDiff)
  552.         {
  553.             firstPoint.x = xCoords.at(0);
  554.             firstPoint.y = yCoords.at(0);
  555.             secondPoint.x = xCoords.at(0);
  556.             secondPoint.y = yCoords.at(1);
  557.         }
  558.         else
  559.         {
  560.             firstPoint.x = xCoords.at(0);
  561.             firstPoint.y = yCoords.at(0);
  562.             secondPoint.x = xCoords.at(1);
  563.             secondPoint.y = yCoords.at(0);
  564.         }
  565.         saveCoords(firstPoint, secondPoint);
  566.         MoveToEx(hdc, firstPoint.x, firstPoint.y, NULL);
  567.         LineTo(hdc, secondPoint.x, secondPoint.y);
  568.         xCoords.clear();
  569.         yCoords.clear();
  570.         SelectObject(hdc, holdPen);
  571.         DeleteObject(hPen);
  572.     }
  573. }
  574.  
  575. void saveCoords(POINT smallestCoords, POINT bigestCoords)
  576. {
  577.     Coords C1;
  578.     C1.smallestCoords.x = smallestCoords.x;
  579.     C1.smallestCoords.y = smallestCoords.y;
  580.     C1.bigestCoords.x = bigestCoords.x;
  581.     C1.bigestCoords.y = bigestCoords.y;
  582.     CoordsList.push_back(C1);
  583. }
  584.  
  585. void DrawAllRect(HWND hwnd)
  586. {
  587.     if (CoordsList.size() != 0)
  588.     {
  589.         HDC hdc = GetDC(hwnd);
  590.         HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  591.         HPEN holdPen = static_cast<HPEN>(SelectObject(hdc, hPen));
  592.         for (size_t CoordsListSize = 0, size = CoordsList.size(); CoordsListSize < size; ++CoordsListSize)
  593.         {
  594.             MoveToEx(hdc, CoordsList.at(CoordsListSize).smallestCoords.x, CoordsList.at(CoordsListSize).smallestCoords.y, NULL);
  595.             LineTo(hdc, CoordsList.at(CoordsListSize).bigestCoords.x, CoordsList.at(CoordsListSize).smallestCoords.y);
  596.             MoveToEx(hdc, CoordsList.at(CoordsListSize).bigestCoords.x, CoordsList.at(CoordsListSize).smallestCoords.y, NULL);
  597.             LineTo(hdc, CoordsList.at(CoordsListSize).bigestCoords.x, CoordsList.at(CoordsListSize).bigestCoords.y);
  598.             MoveToEx(hdc, CoordsList.at(CoordsListSize).smallestCoords.x, CoordsList.at(CoordsListSize).bigestCoords.y, NULL);
  599.             LineTo(hdc, CoordsList.at(CoordsListSize).bigestCoords.x, CoordsList.at(CoordsListSize).bigestCoords.y);
  600.             MoveToEx(hdc, CoordsList.at(CoordsListSize).smallestCoords.x, CoordsList.at(CoordsListSize).smallestCoords.y, NULL);
  601.             LineTo(hdc, CoordsList.at(CoordsListSize).smallestCoords.x, CoordsList.at(CoordsListSize).bigestCoords.y);
  602.         }
  603.         SelectObject(hdc, holdPen);
  604.         DeleteObject(hPen);
  605.     }
  606. }
  607.  
  608. void DrawAllLine(HWND hwnd)
  609. {
  610.     if (CoordsList.size() != 0)
  611.     {
  612.         HDC hdc = GetDC(hwnd);
  613.         HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  614.         HPEN holdPen = static_cast<HPEN>(SelectObject(hdc, hPen));
  615.         for (size_t CoordsListSize = 0, size = CoordsList.size(); CoordsListSize < size; ++CoordsListSize)
  616.         {
  617.             MoveToEx(hdc, CoordsList.at(CoordsListSize).smallestCoords.x, CoordsList.at(CoordsListSize).smallestCoords.y, NULL);
  618.             LineTo(hdc, CoordsList.at(CoordsListSize).bigestCoords.x, CoordsList.at(CoordsListSize).bigestCoords.y);
  619.         }
  620.         SelectObject(hdc, holdPen);
  621.         DeleteObject(hPen);
  622.     }
  623. }
  624.  
  625. bool isMouseInrect()
  626. {
  627.     bool InRect = false;
  628.     POINT mouseCoords;
  629.     GetCursorPos(&mouseCoords);
  630.     for (size_t CoordsListSize = 0, size = CoordsList.size(); CoordsListSize < size; ++CoordsListSize)
  631.     {
  632.         POINT vectorSmallestCoords;
  633.         POINT vectorBigestCoords;
  634.         vectorSmallestCoords = CoordsList.at(CoordsListSize).smallestCoords;
  635.         vectorBigestCoords = CoordsList.at(CoordsListSize).bigestCoords;
  636.         if (((mouseCoords.x >= CoordsList.at(CoordsListSize).smallestCoords.x) && (mouseCoords.x <= CoordsList.at(CoordsListSize).bigestCoords.x)) && ((mouseCoords.y >= CoordsList.at(CoordsListSize).smallestCoords.y) && (mouseCoords.y <= CoordsList.at(CoordsListSize).bigestCoords.y)))
  637.         {
  638.             InRect = true;
  639.         }
  640.     }
  641.     return InRect;
  642. }
  643.  
  644. int getRect()
  645. {
  646.     int rectNumber = 0;
  647.     POINT mouseCoords;
  648.     GetCursorPos(&mouseCoords);
  649.     for (size_t CoordsListSize = 0, size = CoordsList.size(); CoordsListSize < size; ++CoordsListSize)
  650.     {
  651.         if ((mouseCoords.x >= CoordsList.at(CoordsListSize).smallestCoords.x) && (mouseCoords.x <= CoordsList.at(CoordsListSize).bigestCoords.x)
  652.             && (mouseCoords.y >= CoordsList.at(CoordsListSize).smallestCoords.y) && (mouseCoords.y <= CoordsList.at(CoordsListSize).bigestCoords.y))
  653.         {
  654.             rectNumber = CoordsListSize;
  655.         }
  656.     }
  657.     return rectNumber;
  658. }
  659.  
  660. double getSwFract(int mouseX)
  661. {
  662.     double MouseX = 0;
  663.     int screenW = GetSystemMetrics(SM_CXSCREEN);
  664.     MouseX = (double)(mouseX) / (double)screenW;
  665.     return MouseX;
  666. }
  667.  
  668. double getShFract(int mouseY)
  669. {
  670.     double MouseY = 0;
  671.     int screenH = GetSystemMetrics(SM_CYSCREEN);
  672.     MouseY = (double)(mouseY) / (double)screenH;
  673.     return MouseY;
  674. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement