Advertisement
kielbasior

WYKRESY POZIOME

Nov 14th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <vector>
  5.  
  6. using std::vector;
  7.  
  8. HINSTANCE hInst;
  9.  
  10. TCHAR ClassName[] = TEXT("Nazwa klasy");
  11. TCHAR AppName[] = TEXT("Nazwa aplikacji");
  12.  
  13. POINT ekran = {800, 600};
  14. const COLORREF kolory[] = {RGB(255, 0, 0), RGB(200, 0, 0), RGB(150, 0,0)};
  15.  
  16. HBRUSH Pedzel;
  17. struct Wykres
  18. {
  19.     long int a,b;
  20. }wykres;
  21.  
  22. vector<Wykres> dane;
  23. long int max = 0;
  24. unsigned int margines = 50, podzialka_x=0, podzialka_y=0, ilepodzialek_x = 5;
  25. wchar_t bufor[100];
  26. int k=0;
  27.  
  28. LRESULT CALLBACK WndProc(HWND Okno, UINT Kom, WPARAM wParam, LPARAM lParam)
  29. {
  30.     switch(Kom)
  31.     {
  32.     case WM_CREATE:
  33.         srand(time(NULL));
  34.         break;
  35.     case WM_PAINT:
  36.  
  37.         HDC Kontekst;
  38.         PAINTSTRUCT PS;
  39.  
  40.         RECT R;
  41.  
  42.         GetClientRect(Okno,&R);
  43.  
  44.         Kontekst = BeginPaint(Okno,&PS);
  45.  
  46.         SetMapMode(Kontekst,MM_ANISOTROPIC);
  47.         SetWindowExtEx(Kontekst,ekran.x,ekran.y,NULL);
  48.         SetViewportExtEx(Kontekst,R.right,-R.bottom,NULL);
  49.         SetViewportOrgEx(Kontekst,0,R.bottom,NULL);
  50.  
  51.         SaveDC(Kontekst);
  52.  
  53.         podzialka_x = (ekran.x - 2*margines) / (max/1000); //zbyt duze liczby
  54.         podzialka_y = (ekran.y - margines) / dane.size();
  55.  
  56.         Pedzel = CreateSolidBrush(RGB(0,230,0));
  57.         SelectObject(Kontekst,Pedzel);
  58.  
  59.         for(unsigned int i=0;i<dane.size();++i)
  60.         {
  61.             for(unsigned int j=0;j<ilepodzialek_x;++j)
  62.             {
  63.                 Rectangle(Kontekst,margines+j*(ekran.x-2*margines)/5,margines+podzialka_y*i+podzialka_y,margines+j*(ekran.x-2*margines)/5+(ekran.x-2*margines)/5,margines+podzialka_y*i);
  64.             }
  65.         }
  66.  
  67.         RestoreDC(Kontekst,-1);
  68.         DeleteObject(Pedzel);
  69.  
  70.        
  71.         for(unsigned int i=0;i<dane.size();++i)
  72.         {
  73.             Pedzel = CreateSolidBrush(kolory[dane[i].b]);
  74.             SelectObject(Kontekst,Pedzel);
  75.             swprintf(bufor,L"%d",i+1);
  76.             TextOut(Kontekst,0,i*podzialka_y+podzialka_y+margines,bufor,wcslen(bufor));
  77.             Rectangle(Kontekst,margines,i*podzialka_y+podzialka_y+margines,(dane[i].a/1000)*podzialka_x+margines,i*podzialka_y+margines);
  78.             RestoreDC(Kontekst,-1);
  79.             DeleteObject(Pedzel);
  80.         }
  81.  
  82.        
  83.         for(unsigned int i=0;i<ilepodzialek_x;++i)
  84.         {
  85.             swprintf(bufor,L"%d",max/5*(i+1));
  86.             TextOut(Kontekst,(ekran.x-margines)/5*(i+1),margines,bufor,wcslen(bufor));
  87.         }
  88.        
  89.         DeleteDC(Kontekst);
  90.         EndPaint(Okno,&PS);
  91.         break;
  92.     case WM_LBUTTONDOWN:
  93.         long int tmp;
  94.         tmp = 1000+rand() % 100000-999;
  95.         if(max < tmp) max = tmp;
  96.         if (k>2) k=0;
  97.         wykres.a=tmp;
  98.         wykres.b=k;
  99.         dane.push_back(wykres);
  100.         k++;
  101.         InvalidateRect(Okno,NULL,true);
  102.         break;
  103.     case WM_DESTROY:
  104.         dane.clear();
  105.         PostQuitMessage(0);
  106.     default:
  107.         return DefWindowProc(Okno,Kom,wParam,lParam);
  108.     }
  109.     return 0;
  110. }
  111.  
  112. WNDCLASSEX RejestrujKlase()
  113. {
  114.     WNDCLASSEX wc;
  115.     wc.cbClsExtra = 0;
  116.     wc.cbSize = sizeof(WNDCLASSEX);
  117.     wc.cbWndExtra = 0;
  118.     wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
  119.     wc.hCursor = 0;
  120.     wc.hIcon = 0;
  121.     wc.hIconSm = 0;
  122.     wc.hInstance = hInst;
  123.     wc.lpfnWndProc = WndProc;
  124.     wc.lpszClassName = ClassName;
  125.     wc.lpszMenuName = 0;
  126.     wc.style = CS_HREDRAW | CS_VREDRAW;
  127.     return wc;
  128. }
  129.  
  130. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  131. {
  132.     hInstance = hInst;
  133.     WNDCLASSEX wc = RejestrujKlase();
  134.     if(!RegisterClassEx(&wc))
  135.     {
  136.         MessageBox(NULL,L"Nie udalo sie zarejestrowac klasy okna",L"Blad",NULL);
  137.         return 1;
  138.     }
  139.  
  140.     HWND Okno = CreateWindowEx(NULL,ClassName,AppName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,ekran.x,ekran.y,0,0,hInstance,0);
  141.  
  142.     if(!Okno)
  143.     {
  144.         MessageBox(NULL,L"Nie udalo sie stworzyc uchwytu okna",L"Blad",NULL);
  145.         return 2;
  146.     }
  147.  
  148.     UpdateWindow(Okno);
  149.     ShowWindow(Okno,nCmdShow);
  150.  
  151.     MSG Kom;
  152.  
  153.     while(GetMessage(&Kom,0,0,0) > 0)
  154.     {
  155.         TranslateMessage(&Kom);
  156.         DispatchMessage(&Kom);
  157.     }
  158.  
  159.     return Kom.wParam;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement