peterzig

[PIU] Wykresy

Nov 16th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.30 KB | None | 0 0
  1. #include <windows.h>
  2. #include <time.h>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
  9. bool createWinClass(HINSTANCE&, TCHAR*);
  10.  
  11. class CWykres
  12. {
  13.     public:
  14.         CWykres();
  15.         void paint(const HWND);
  16.         short int getVal() const;
  17.         short int getCount() const;
  18.  
  19.     private:
  20.         void randBars();
  21.         short int maxval, count, minval;
  22.         vector<int> bars;
  23. };
  24.  
  25. CWykres::CWykres()
  26. {
  27.     srand((unsigned int)time(NULL));
  28.     maxval = (rand() % 11) + 5;
  29.     count = (rand() % 21) + 5;
  30.  
  31.     if(maxval % 2) maxval = maxval - 1;
  32.     bars.reserve(count);
  33.     randBars();
  34. }
  35.  
  36. void CWykres::randBars()
  37. {
  38.     srand((unsigned int)time(NULL));
  39.     for(unsigned int i = 0; i < (unsigned int)count; i++)
  40.     {
  41.         int val = (rand() % (maxval + 1));
  42.         int sign = rand() % 2;
  43.        
  44.         if(sign) val = -val;
  45.  
  46.         bars.push_back(val);
  47.     }
  48. }
  49.  
  50. short int CWykres::getVal() const
  51. {
  52.     return maxval;
  53. }
  54.  
  55. short int CWykres::getCount() const
  56. {
  57.     return count;
  58. }
  59.  
  60. void CWykres::paint(const HWND hwnd)
  61. {
  62.     PAINTSTRUCT paintdata;
  63.     RECT window_size, window_margin;
  64.     POINT position;
  65.  
  66.     HDC hdc = BeginPaint(hwnd, &paintdata);
  67.     HPEN emptyPen = (HPEN) SelectObject(hdc, GetStockObject(NULL_PEN));
  68.     HBRUSH emptyBrush = (HBRUSH) SelectObject(hdc, GetStockObject(NULL_BRUSH));
  69.  
  70.     HPEN standardPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
  71.     HBRUSH standardBrush = CreateSolidBrush(RGB(255, 0, 0));
  72.     HPEN redPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
  73.  
  74.     SelectObject(hdc, standardBrush);
  75.     SelectObject(hdc, standardPen);
  76.     GetClientRect(hwnd, &window_size);
  77.  
  78.     // window for axis
  79.     window_margin = window_size;
  80.     window_margin.left = static_cast<LONG>(window_margin.right * 0.1);
  81.     window_margin.right = static_cast<LONG>(window_margin.right * 0.9);
  82.     window_margin.top = static_cast<LONG>(window_margin.bottom * 0.1);
  83.     window_margin.bottom = static_cast<LONG>(window_margin.bottom * 0.9);
  84.  
  85.     // y axis
  86.     MoveToEx(hdc, window_margin.left, window_margin.top, &position);
  87.     LineTo(hdc, window_margin.left, window_margin.bottom);
  88.  
  89.     // y axis triangle
  90.     POINT yTriangle[4];
  91.     yTriangle[0].x = window_margin.left - 10;
  92.     yTriangle[1].x = window_margin.left + 10;
  93.     yTriangle[0].y = yTriangle[1].y = window_margin.top;
  94.     yTriangle[2].x = window_margin.left;
  95.     yTriangle[2].y = window_margin.top - 10;
  96.     yTriangle[3] = yTriangle[0];
  97.     Polyline(hdc,  yTriangle, 4);
  98.  
  99.     // x axis
  100.     int halftop = static_cast<int>((window_margin.bottom - window_margin.top)/2 + window_margin.top);
  101.     MoveToEx(hdc, window_margin.left, halftop, &position);
  102.     LineTo(hdc, window_margin.right, halftop);
  103.  
  104.     // x axis triangle
  105.     POINT xTriangle[4];
  106.     xTriangle[0].x = xTriangle[1].x = window_margin.right;
  107.     xTriangle[0].y = halftop - 10;
  108.     xTriangle[1].y = halftop + 10;
  109.     xTriangle[2].x = window_margin.right + 10;
  110.     xTriangle[2].y = halftop;
  111.     xTriangle[3] = xTriangle[0];
  112.     Polyline(hdc,  xTriangle, 4);
  113.  
  114.     // describe y axis
  115.     int maxval = getVal();
  116.     int copy = maxval + 1;
  117.     int spacing = (window_margin.bottom - window_margin.top) / (copy + 1);
  118.  
  119.     for(unsigned int i = 1; i <= (unsigned int)copy; i++, maxval = (maxval - 2))
  120.     {
  121.         if(maxval == 0)
  122.             continue;
  123.  
  124.         unsigned int top = window_margin.top + (i * spacing);
  125.         std::wstring s = to_wstring(maxval) + L".0";
  126.         TextOut(hdc, window_margin.left - 40, top - 5, s.c_str(), s.size());
  127.  
  128.         MoveToEx(hdc, window_margin.left - 5, top, &position);
  129.         LineTo(hdc, window_margin.left + 5, top);
  130.     }
  131.  
  132.     // describe x axis
  133.     maxval = getCount();
  134.     spacing = (window_margin.right - window_margin.left - 20) / maxval;
  135.     int spacingY = (window_margin.bottom - window_margin.top) / 2;
  136.  
  137.     SelectObject(hdc, redPen);
  138.     // make rectangles
  139.     for(unsigned int i = 1; i <= (unsigned int)maxval; i++)
  140.     {
  141.         unsigned int left = window_margin.left + (i * spacing);
  142.         float rectSize = (float)bars[i - 1];
  143.         float max = (float)getVal();
  144.  
  145.         if(rectSize > 0)
  146.         {
  147.             float top = (rectSize / (max + 2)) * (float)spacingY;
  148.             Rectangle(hdc, left - (spacing / 4), halftop - (int)top, left + (spacing / 4), halftop);
  149.         }
  150.         else
  151.         {
  152.             float top = ((-rectSize) / (max + 2)) * (float)spacingY;
  153.             Rectangle(hdc, left - (spacing / 4), halftop + (int)top, left + (spacing / 4), halftop);
  154.         }
  155.     }
  156.     SelectObject(hdc, standardPen);
  157.  
  158.     // make descriptions
  159.     for(unsigned int i = 1; i <= (unsigned int)maxval; i++)
  160.     {
  161.         unsigned int left = window_margin.left + (i * spacing);
  162.         std::wstring s = to_wstring(i);
  163.  
  164.         SetBkMode(hdc, TRANSPARENT);
  165.         TextOut(hdc, left - 5, halftop + 5, s.c_str(), s.size());
  166.  
  167.         MoveToEx(hdc, left, halftop - 5, &position);
  168.         LineTo(hdc, left, halftop + 5);
  169.     }
  170.  
  171.     SelectObject(hdc, emptyPen);
  172.     SelectObject(hdc, emptyBrush);
  173.  
  174.     EndPaint(hwnd, &paintdata);
  175.     DeleteObject(standardPen);
  176.     DeleteObject(standardBrush);
  177.     DeleteObject(redPen);
  178. }
  179.  
  180. CWykres Wykres;
  181. TCHAR APPName[] = TEXT("Moja aplikacja");
  182. TCHAR ClassName[] = TEXT("Nazwa klasy");
  183.  
  184. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPevInstance, LPSTR lpCmdLine, int nShowCmd)
  185. {
  186.     if(!createWinClass(hInstance, ClassName))
  187.         return 0;
  188.  
  189.     HWND window = CreateWindowEx(0, ClassName, APPName, WS_OVERLAPPEDWINDOW, 200, 200, 500, 400, 0, 0, 0, 0);
  190.     if(window) {
  191.         MSG msg;
  192.         ShowWindow(window, nShowCmd);
  193.  
  194.         while(GetMessage(&msg, 0, 0, 0) > 0) {
  195.             TranslateMessage(&msg);
  196.             DispatchMessage(&msg);
  197.         }
  198.     }
  199.     else MessageBox(0, TEXT("Inicjalizacja okna nie powiodła się."), APPName, MB_OK | MB_ICONERROR);
  200.  
  201.     UnregisterClass(ClassName, hInstance);
  202.     return 0;
  203. }
  204.  
  205. static LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  206. {
  207.     switch(msg)
  208.     {
  209.         case WM_PAINT:
  210.             Wykres.paint(hwnd);
  211.             break;
  212.         case WM_MOVE:
  213.         case WM_SIZE:
  214.             InvalidateRect(hwnd, 0, TRUE);
  215.             break;
  216.         case WM_CLOSE:
  217.             DestroyWindow(hwnd);
  218.             break;
  219.         case WM_DESTROY:
  220.             PostQuitMessage(0);
  221.             break;
  222.         default:
  223.             return DefWindowProc(hwnd, msg, wParam, lParam);
  224.     }
  225.     return 0;
  226. }
  227.  
  228. static bool createWinClass(HINSTANCE& hInstance, TCHAR* ClassName)
  229. {
  230.     WNDCLASSEX wc;
  231.     wc.cbClsExtra = wc.cbWndExtra = 0;
  232.     wc.cbSize = sizeof(WNDCLASSEX);
  233.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  234.     wc.hCursor = LoadCursor(0, IDC_ARROW);
  235.     wc.hIconSm = wc.hIcon = LoadIcon(0, IDI_APPLICATION);
  236.     wc.hInstance = hInstance;
  237.     wc.lpfnWndProc = WinProc;
  238.     wc.lpszClassName = ClassName;
  239.     wc.lpszMenuName = 0;
  240.     wc.style = 0;
  241.     return (RegisterClassEx(&wc) != 0);
  242. }
Add Comment
Please, Sign In to add comment