Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WIN32_LEAN_AND_MEAN
- #include <Windows.h>
- #include <map>
- HRESULT InitWindow(HINSTANCE, int);
- void PrepareGraph();
- HWND g_hWnd;
- std::map<float, int> g_aGraph;
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
- {
- if (FAILED(InitWindow(hInstance, nCmdShow)))
- {
- MessageBox(NULL, L"Окно каким-то образом не создалось", L"Error!", MB_ICONERROR | MB_OK);
- }
- PrepareGraph();
- MSG msg = { 0 };
- while (WM_QUIT != msg.message)
- {
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int)msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hWnd, &ps);
- // Размеры окна
- RECT rect;
- GetClientRect(hWnd, &rect);
- int width = rect.right;
- int height = rect.bottom;
- HBRUSH hBrush = CreateSolidBrush(RGB(0, 127, 255)); // Синяя кисть
- HPEN hPen = CreatePen(0, 1, RGB(255, 255, 0));// Жёлтый карандаш
- // Задний буффер
- HDC hdcmem = CreateCompatibleDC(hdc);
- HBITMAP hbmBackBuffer = CreateCompatibleBitmap(hdc, width, height);
- SelectObject(hdcmem, hbmBackBuffer);
- // Заполняем задний буфер цветом
- SelectObject(hdcmem, hBrush);
- Rectangle(hdcmem, -1, -1, width+1, height+1);
- // Выбираем цвет линии
- SelectObject(hdcmem, hPen);
- float min = g_aGraph.begin()->first,
- max = g_aGraph.rbegin()->first;
- MoveToEx(hdcmem, (g_aGraph.begin()->first - min)*width / (max - min), double(g_aGraph.begin()->second + 0x80000000) / 0xffffffff * height, nullptr);
- for (const auto& e : g_aGraph)
- LineTo(hdcmem, (e.first - min)*width / (max - min), double(e.second + 0x80000000) / 0xffffffff * height);
- // Переносим всё из заднего буфера на экран
- BitBlt(hdc, 0, 0, width, height, hdcmem, 0, 0, SRCCOPY);
- DeleteDC(hdcmem);
- EndPaint(hWnd, &ps);
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = NULL;
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = L"Graph";
- wcex.hIconSm = NULL;
- if (!RegisterClassEx(&wcex))
- return E_FAIL;
- RECT rc = { 0, 0, 640, 480 };
- AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
- g_hWnd = CreateWindow(L"Graph", L"График", WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL);
- if (!g_hWnd)
- return E_FAIL;
- ShowWindow(g_hWnd, nCmdShow);
- return S_OK;
- }
- void PrepareGraph()
- {
- for (float i = -100; i <= 100; i += 0.1)
- g_aGraph[i] = (sin(i)*(unsigned int(-1)>>1));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement