Advertisement
NyanCoder

График

May 2nd, 2016
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <Windows.h>
  3.  
  4. #include <map>
  5.  
  6. HRESULT InitWindow(HINSTANCE, int);
  7. void PrepareGraph();
  8.  
  9. HWND g_hWnd;
  10. std::map<float, int> g_aGraph;
  11.  
  12. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
  13. {
  14.     if (FAILED(InitWindow(hInstance, nCmdShow)))
  15.     {
  16.         MessageBox(NULL, L"Окно каким-то образом не создалось", L"Error!", MB_ICONERROR | MB_OK);
  17.     }
  18.     PrepareGraph();
  19.  
  20.     MSG msg = { 0 };
  21.     while (WM_QUIT != msg.message)
  22.     {
  23.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  24.         {
  25.             TranslateMessage(&msg);
  26.             DispatchMessage(&msg);
  27.         }
  28.     }
  29.  
  30.     return (int)msg.wParam;
  31. }
  32.  
  33. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  34. {
  35.    
  36.  
  37.     switch (message)
  38.     {
  39.     case WM_PAINT:
  40.     {
  41.         PAINTSTRUCT ps;
  42.         HDC hdc = BeginPaint(hWnd, &ps);
  43.  
  44.         // Размеры окна
  45.         RECT rect;
  46.         GetClientRect(hWnd, &rect);
  47.         int width = rect.right;
  48.         int height = rect.bottom;
  49.  
  50.         HBRUSH hBrush = CreateSolidBrush(RGB(0, 127, 255)); // Синяя кисть
  51.         HPEN hPen = CreatePen(0, 1, RGB(255, 255, 0));// Жёлтый карандаш
  52.        
  53.         // Задний буффер
  54.         HDC hdcmem = CreateCompatibleDC(hdc);
  55.         HBITMAP hbmBackBuffer = CreateCompatibleBitmap(hdc, width, height);
  56.         SelectObject(hdcmem, hbmBackBuffer);
  57.  
  58.         // Заполняем задний буфер цветом
  59.         SelectObject(hdcmem, hBrush);
  60.         Rectangle(hdcmem, -1, -1, width+1, height+1);
  61.  
  62.         // Выбираем цвет линии
  63.         SelectObject(hdcmem, hPen);
  64.  
  65.         float min = g_aGraph.begin()->first,
  66.             max = g_aGraph.rbegin()->first;
  67.  
  68.         MoveToEx(hdcmem, (g_aGraph.begin()->first - min)*width / (max - min), double(g_aGraph.begin()->second + 0x80000000) / 0xffffffff * height, nullptr);
  69.         for (const auto& e : g_aGraph)
  70.             LineTo(hdcmem, (e.first - min)*width / (max - min), double(e.second + 0x80000000) / 0xffffffff * height);
  71.  
  72.         // Переносим всё из заднего буфера на экран
  73.         BitBlt(hdc, 0, 0, width, height, hdcmem, 0, 0, SRCCOPY);
  74.  
  75.         DeleteDC(hdcmem);
  76.         EndPaint(hWnd, &ps);
  77.     }
  78.         break;
  79.  
  80.     case WM_DESTROY:
  81.         PostQuitMessage(0);
  82.         break;
  83.  
  84.     default:
  85.         return DefWindowProc(hWnd, message, wParam, lParam);
  86.     }
  87.  
  88.     return 0;
  89. }
  90.  
  91. HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow)
  92. {
  93.     WNDCLASSEX wcex;
  94.     wcex.cbSize = sizeof(WNDCLASSEX);
  95.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  96.     wcex.lpfnWndProc = WndProc;
  97.     wcex.cbClsExtra = 0;
  98.     wcex.cbWndExtra = 0;
  99.     wcex.hInstance = hInstance;
  100.     wcex.hIcon = NULL;
  101.     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  102.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  103.     wcex.lpszMenuName = NULL;
  104.     wcex.lpszClassName = L"Graph";
  105.     wcex.hIconSm = NULL;
  106.     if (!RegisterClassEx(&wcex))
  107.         return E_FAIL;
  108.  
  109.     RECT rc = { 0, 0, 640, 480 };
  110.     AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
  111.     g_hWnd = CreateWindow(L"Graph", L"График", WS_OVERLAPPEDWINDOW,
  112.         CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL);
  113.     if (!g_hWnd)
  114.         return E_FAIL;
  115.  
  116.     ShowWindow(g_hWnd, nCmdShow);
  117.     return S_OK;
  118. }
  119.  
  120. void PrepareGraph()
  121. {
  122.     for (float i = -100; i <= 100; i += 0.1)
  123.         g_aGraph[i] = (sin(i)*(unsigned int(-1)>>1));
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement