Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <stdio.h>
- #include <tchar.h>
- #include <math.h>
- #define MAX_T 5;
- #define MAX_X 5;
- #define MAX_Y 7;
- // Константы идентификаторов командных пунктов меню
- enum {
- IDM_AXIS = 0x1000, // Рисовать оси координат
- IDM_XT, // График x(t)
- IDM_YT, // График y(t)
- IDM_YX, // График y(x)
- IDM_ABOUT, // О программе
- IDM_EXIT // Выход
- };
- LONG WINAPI WndProc(HWND, UINT, WPARAM, LPARAM);
- // Функция рисования осей
- void DrawAxis(HDC hdc, RECT rectClient)
- {
- int OffsetY, OffsetX;
- int height, width;
- HPEN hpen;
- height = rectClient.bottom - rectClient.top;
- width = rectClient.right - rectClient.left;
- hpen = CreatePen(PS_SOLID, 0, 0);
- SelectObject(hdc, hpen);
- OffsetY = (height) >> 1;
- OffsetX = (width) >> 1;
- MoveToEx(hdc, 0, OffsetY, 0);
- LineTo(hdc, width, OffsetY);
- MoveToEx(hdc, OffsetX, 0, 0);
- LineTo(hdc, OffsetX, height);
- }
- void DrawGraphXT(HDC hdc, RECT rectClient)
- {
- //здесь x=t, x=y(t);
- int OffsetX, OffsetY;
- float ScaleX, ScaleY;
- int height, width;
- float x, y;
- int X, Y;
- HPEN hpen;
- height = rectClient.bottom - rectClient.top;
- width = rectClient.right - rectClient.left;
- hpen = CreatePen(PS_SOLID, 2, 0xFF);
- SelectObject(hdc, hpen);
- OffsetY = (height) >> 1;
- OffsetX = (width) >> 1;
- ScaleX = (float)(width >> 1) / MAX_T;
- ScaleY = (float)((height >> 1)) / MAX_X;
- x = 0;
- y = 0;
- X = OffsetX + int(x*ScaleX);
- Y = OffsetY + int(y*ScaleY);
- MoveToEx(hdc, X, Y, 0);
- while (x<7)
- {
- x = x + 0.1f;
- y = 2 * sin(x) + cos(3 * x);
- X = OffsetX + x*ScaleX;
- Y = OffsetY - y*ScaleY;
- LineTo(hdc, X, Y);
- }
- }
- void DrawGraphYT(HDC hdc, RECT rectClient)
- {
- //здесь x=t, y=y(t);
- int OffsetX, OffsetY;
- float ScaleX, ScaleY;
- int height, width;
- float x, y;
- int X, Y;
- HPEN hpen;
- height = rectClient.bottom - rectClient.top;
- width = rectClient.right - rectClient.left;
- hpen = CreatePen(PS_SOLID, 2, 0xFF);
- SelectObject(hdc, hpen);
- OffsetY = (height) >> 1;
- OffsetX = (width) >> 1;
- ScaleX = (float)(width >> 1) / MAX_T;
- ScaleY = (float)((height >> 1)) / MAX_Y;
- x = 0;
- y = 2;
- X = OffsetX + int(x*ScaleX);
- Y = OffsetY - int(y*ScaleY);
- MoveToEx(hdc, X, Y, 0);
- while (x<7)
- {
- x = x + 0.1f;
- y = cos(3 * x) + cos(x);
- X = OffsetX + x*ScaleX;
- Y = OffsetY - y*ScaleY;
- LineTo(hdc, X, Y);
- }
- }
- void DrawGraphYX(HDC hdc, RECT rectClient)
- {
- //здесь x=x(t), y=y(t)
- int OffsetY, OffsetX;
- float ScaleX, ScaleY;
- int height, width;
- float x, y, t;
- int X, Y;
- HPEN hpen;
- height = rectClient.bottom - rectClient.top;
- width = rectClient.right - rectClient.left;
- hpen = CreatePen(PS_SOLID, 2, 0xFF);
- SelectObject(hdc, hpen);
- OffsetY = (height) >> 1;
- OffsetX = (width) >> 1;
- ScaleX = (float)(width >> 1) / MAX_X;
- ScaleY = (float)((height >> 1)) / MAX_Y;
- t = 0;
- x = 1;
- y = 2;
- X = OffsetX + int(x*ScaleX);
- Y = OffsetY - int(y*ScaleY);
- MoveToEx(hdc, X, Y, 0);
- while (t<7)
- {
- t = t + 0.1f;
- y = cos(3 * t) + cos(t);
- x = 2*sin(t) + cos(3 * t);
- X = OffsetX + x*ScaleX;
- Y = OffsetY - y*ScaleY;
- LineTo(hdc, X, Y);
- }
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- HWND hwnd;
- MSG msg;
- WNDCLASS w;
- HMENU hMenu, hFileMenu, hChoiceMenu, hHelpMenu;
- memset(&w, 0, sizeof(WNDCLASS));
- w.style = CS_HREDRAW | CS_VREDRAW;
- w.lpfnWndProc = WndProc;
- w.hInstance = hInstance;
- w.hbrBackground = CreateSolidBrush(0x00FFFFFF);
- w.lpszClassName = _T("Задание №1");
- RegisterClass(&w);
- hwnd = CreateWindow(_T("Задание №1"),
- _T("Графики функции"),
- WS_OVERLAPPEDWINDOW,
- 450, 450, 450, 450, NULL, NULL,
- hInstance, NULL);
- hChoiceMenu = CreatePopupMenu();
- AppendMenu(hChoiceMenu, MF_ENABLED | MF_STRING, IDM_XT, _T("График x = x(t)"));
- AppendMenu(hChoiceMenu, MF_ENABLED | MF_STRING, IDM_YT, _T("График y = y(t)"));
- AppendMenu(hChoiceMenu, MF_ENABLED | MF_STRING, IDM_YX, _T("График y = y(x)"));
- hFileMenu = CreatePopupMenu();
- AppendMenu(hFileMenu, MF_ENABLED | MF_STRING, IDM_AXIS, _T("Рисовать Оси"));
- AppendMenu(hFileMenu, MF_POPUP | MF_STRING, (UINT)hChoiceMenu, _T("Отображение функции"));
- AppendMenu(hFileMenu, MF_SEPARATOR, 0, 0);
- AppendMenu(hFileMenu, MF_ENABLED | MF_STRING, IDM_EXIT, _T("Выход"));
- hHelpMenu = CreatePopupMenu();
- AppendMenu(hHelpMenu, MF_ENABLED | MF_STRING, IDM_ABOUT, _T("О программе"));
- hMenu = CreateMenu();
- AppendMenu(hMenu, MF_ENABLED | MF_POPUP, (UINT)hFileMenu, _T("М&еню"));
- AppendMenu(hMenu, MF_ENABLED | MF_POPUP, (UINT)hHelpMenu, _T("С&правка"));
- SetMenu(hwnd, hMenu);
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- DrawMenuBar(hwnd);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- // Оконная функция
- LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
- {
- HDC hdc;
- PAINTSTRUCT ps;
- static BOOL bShowAxis = FALSE;
- static int nShowFunc = 0;
- HMENU hSelectMenu;
- switch (Message)
- {
- case WM_COMMAND:
- switch (wParam)
- {
- case IDM_EXIT:
- SendMessage(hwnd, WM_CLOSE, (WPARAM)NULL, (LPARAM)NULL);
- break;
- case IDM_ABOUT:
- MessageBox(hwnd, _T("ПС(КТУР)-182 Федотов М.А."), _T("Справка"), MB_OK | MB_ICONINFORMATION);
- break;
- case IDM_AXIS:
- hSelectMenu = GetMenu(hwnd);
- if (CheckMenuItem(hSelectMenu, IDM_AXIS, MF_CHECKED))
- {
- bShowAxis = FALSE;
- CheckMenuItem(hSelectMenu, IDM_AXIS, MF_UNCHECKED);
- }
- else
- {
- bShowAxis = TRUE;
- CheckMenuItem(hSelectMenu, IDM_AXIS, MF_CHECKED);
- }
- break;
- case IDM_XT:
- nShowFunc = 1;
- break;
- case IDM_YT:
- nShowFunc = 2;
- break;
- case IDM_YX:
- nShowFunc = 3;
- break;
- }
- InvalidateRect(hwnd, NULL, TRUE); //перерисовать окно вместе с фоном
- break;
- case WM_PAINT:
- hdc = BeginPaint(hwnd, &ps);
- if (bShowAxis)
- DrawAxis(hdc, ps.rcPaint);
- SetTextColor(hdc, 0x00FF0000);
- switch (nShowFunc)
- {
- case 0:
- TextOut(hdc, 10, 20, _T("Для начала работы выберите пункт в разделе Меню"), 47);
- break;
- case 1:
- TextOut(hdc, 10, 20, _T("x(t) = 2sin(t) + cos(3t)"), 28);
- DrawGraphXT(hdc, ps.rcPaint);
- break;
- case 2:
- TextOut(hdc, 10, 20, _T("y(t) = cos(3t) + cos(t)"), 24);
- DrawGraphYT(hdc, ps.rcPaint);
- break;
- case 3:
- TextOut(hdc, 10, 20, _T("y(t) = cos(3t) + cos(t)"), 24);
- TextOut(hdc, 10, 40, _T("x(t) = 2sin(t) + cos(3t)"), 28);
- DrawGraphYX(hdc, ps.rcPaint);
- break;
- }
- SetTextColor(hdc, 0x00FF0000);
- EndPaint(hwnd, &ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hwnd, Message, wParam, lParam);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement