Advertisement
Guest User

LAB1_summer

a guest
Jul 29th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4. #include <math.h>
  5. #define MAX_T 5;
  6. #define MAX_X 5;
  7. #define MAX_Y 7;
  8. // Константы идентификаторов командных пунктов меню
  9. enum {
  10. IDM_AXIS = 0x1000, // Рисовать оси координат
  11. IDM_XT, // График x(t)
  12. IDM_YT, // График y(t)
  13. IDM_YX, // График y(x)
  14. IDM_ABOUT, // О программе
  15. IDM_EXIT // Выход
  16. };
  17. LONG WINAPI WndProc(HWND, UINT, WPARAM, LPARAM);
  18. // Функция рисования осей
  19. void DrawAxis(HDC hdc, RECT rectClient)
  20. {
  21. int OffsetY, OffsetX;
  22. int height, width;
  23. HPEN hpen;
  24. height = rectClient.bottom - rectClient.top;
  25. width = rectClient.right - rectClient.left;
  26. hpen = CreatePen(PS_SOLID, 0, 0);
  27. SelectObject(hdc, hpen);
  28. OffsetY = (height) >> 1;
  29. OffsetX = (width) >> 1;
  30. MoveToEx(hdc, 0, OffsetY, 0);
  31. LineTo(hdc, width, OffsetY);
  32. MoveToEx(hdc, OffsetX, 0, 0);
  33. LineTo(hdc, OffsetX, height);
  34. }
  35. void DrawGraphXT(HDC hdc, RECT rectClient)
  36. {
  37. //здесь x=t, x=y(t);
  38. int OffsetX, OffsetY;
  39. float ScaleX, ScaleY;
  40. int height, width;
  41. float x, y;
  42. int X, Y;
  43. HPEN hpen;
  44. height = rectClient.bottom - rectClient.top;
  45. width = rectClient.right - rectClient.left;
  46. hpen = CreatePen(PS_SOLID, 2, 0xFF);
  47. SelectObject(hdc, hpen);
  48. OffsetY = (height) >> 1;
  49. OffsetX = (width) >> 1;
  50. ScaleX = (float)(width >> 1) / MAX_T;
  51. ScaleY = (float)((height >> 1)) / MAX_X;
  52. x = 0;
  53. y = 0;
  54. X = OffsetX + int(x*ScaleX);
  55. Y = OffsetY + int(y*ScaleY);
  56. MoveToEx(hdc, X, Y, 0);
  57. while (x<7)
  58. {
  59. x = x + 0.1f;
  60. y = 2 * sin(x) + cos(3 * x);
  61. X = OffsetX + x*ScaleX;
  62. Y = OffsetY - y*ScaleY;
  63. LineTo(hdc, X, Y);
  64. }
  65. }
  66.  
  67. void DrawGraphYT(HDC hdc, RECT rectClient)
  68. {
  69. //здесь x=t, y=y(t);
  70. int OffsetX, OffsetY;
  71. float ScaleX, ScaleY;
  72. int height, width;
  73. float x, y;
  74. int X, Y;
  75. HPEN hpen;
  76. height = rectClient.bottom - rectClient.top;
  77. width = rectClient.right - rectClient.left;
  78. hpen = CreatePen(PS_SOLID, 2, 0xFF);
  79. SelectObject(hdc, hpen);
  80. OffsetY = (height) >> 1;
  81. OffsetX = (width) >> 1;
  82. ScaleX = (float)(width >> 1) / MAX_T;
  83. ScaleY = (float)((height >> 1)) / MAX_Y;
  84. x = 0;
  85. y = 2;
  86. X = OffsetX + int(x*ScaleX);
  87. Y = OffsetY - int(y*ScaleY);
  88. MoveToEx(hdc, X, Y, 0);
  89. while (x<7)
  90. {
  91. x = x + 0.1f;
  92. y = cos(3 * x) + cos(x);
  93. X = OffsetX + x*ScaleX;
  94. Y = OffsetY - y*ScaleY;
  95. LineTo(hdc, X, Y);
  96. }
  97. }
  98. void DrawGraphYX(HDC hdc, RECT rectClient)
  99. {
  100. //здесь x=x(t), y=y(t)
  101. int OffsetY, OffsetX;
  102. float ScaleX, ScaleY;
  103. int height, width;
  104. float x, y, t;
  105. int X, Y;
  106. HPEN hpen;
  107. height = rectClient.bottom - rectClient.top;
  108. width = rectClient.right - rectClient.left;
  109. hpen = CreatePen(PS_SOLID, 2, 0xFF);
  110. SelectObject(hdc, hpen);
  111. OffsetY = (height) >> 1;
  112. OffsetX = (width) >> 1;
  113. ScaleX = (float)(width >> 1) / MAX_X;
  114. ScaleY = (float)((height >> 1)) / MAX_Y;
  115. t = 0;
  116. x = 1;
  117. y = 2;
  118. X = OffsetX + int(x*ScaleX);
  119. Y = OffsetY - int(y*ScaleY);
  120. MoveToEx(hdc, X, Y, 0);
  121. while (t<7)
  122. {
  123. t = t + 0.1f;
  124. y = cos(3 * t) + cos(t);
  125. x = 2*sin(t) + cos(3 * t);
  126. X = OffsetX + x*ScaleX;
  127. Y = OffsetY - y*ScaleY;
  128. LineTo(hdc, X, Y);
  129. }
  130. }
  131. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  132. {
  133. HWND hwnd;
  134. MSG msg;
  135. WNDCLASS w;
  136. HMENU hMenu, hFileMenu, hChoiceMenu, hHelpMenu;
  137. memset(&w, 0, sizeof(WNDCLASS));
  138. w.style = CS_HREDRAW | CS_VREDRAW;
  139. w.lpfnWndProc = WndProc;
  140. w.hInstance = hInstance;
  141. w.hbrBackground = CreateSolidBrush(0x00FFFFFF);
  142. w.lpszClassName = _T("Задание №1");
  143. RegisterClass(&w);
  144. hwnd = CreateWindow(_T("Задание №1"),
  145. _T("Графики функции"),
  146. WS_OVERLAPPEDWINDOW,
  147. 450, 450, 450, 450, NULL, NULL,
  148. hInstance, NULL);
  149. hChoiceMenu = CreatePopupMenu();
  150. AppendMenu(hChoiceMenu, MF_ENABLED | MF_STRING, IDM_XT, _T("График x = x(t)"));
  151. AppendMenu(hChoiceMenu, MF_ENABLED | MF_STRING, IDM_YT, _T("График y = y(t)"));
  152. AppendMenu(hChoiceMenu, MF_ENABLED | MF_STRING, IDM_YX, _T("График y = y(x)"));
  153.  
  154. hFileMenu = CreatePopupMenu();
  155. AppendMenu(hFileMenu, MF_ENABLED | MF_STRING, IDM_AXIS, _T("Рисовать Оси"));
  156. AppendMenu(hFileMenu, MF_POPUP | MF_STRING, (UINT)hChoiceMenu, _T("Отображение функции"));
  157. AppendMenu(hFileMenu, MF_SEPARATOR, 0, 0);
  158. AppendMenu(hFileMenu, MF_ENABLED | MF_STRING, IDM_EXIT, _T("Выход"));
  159.  
  160. hHelpMenu = CreatePopupMenu();
  161. AppendMenu(hHelpMenu, MF_ENABLED | MF_STRING, IDM_ABOUT, _T("О программе"));
  162.  
  163. hMenu = CreateMenu();
  164. AppendMenu(hMenu, MF_ENABLED | MF_POPUP, (UINT)hFileMenu, _T("М&еню"));
  165. AppendMenu(hMenu, MF_ENABLED | MF_POPUP, (UINT)hHelpMenu, _T("С&правка"));
  166.  
  167. SetMenu(hwnd, hMenu);
  168. ShowWindow(hwnd, nCmdShow);
  169. UpdateWindow(hwnd);
  170. DrawMenuBar(hwnd);
  171.  
  172. while (GetMessage(&msg, NULL, 0, 0))
  173. {
  174. TranslateMessage(&msg);
  175. DispatchMessage(&msg);
  176. }
  177. return msg.wParam;
  178. }
  179. // Оконная функция
  180. LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  181. {
  182. HDC hdc;
  183. PAINTSTRUCT ps;
  184. static BOOL bShowAxis = FALSE;
  185. static int nShowFunc = 0;
  186. HMENU hSelectMenu;
  187. switch (Message)
  188. {
  189. case WM_COMMAND:
  190. switch (wParam)
  191. {
  192. case IDM_EXIT:
  193. SendMessage(hwnd, WM_CLOSE, (WPARAM)NULL, (LPARAM)NULL);
  194. break;
  195. case IDM_ABOUT:
  196. MessageBox(hwnd, _T("ПС(КТУР)-182 Федотов М.А."), _T("Справка"), MB_OK | MB_ICONINFORMATION);
  197. break;
  198. case IDM_AXIS:
  199. hSelectMenu = GetMenu(hwnd);
  200. if (CheckMenuItem(hSelectMenu, IDM_AXIS, MF_CHECKED))
  201. {
  202. bShowAxis = FALSE;
  203. CheckMenuItem(hSelectMenu, IDM_AXIS, MF_UNCHECKED);
  204. }
  205. else
  206. {
  207. bShowAxis = TRUE;
  208. CheckMenuItem(hSelectMenu, IDM_AXIS, MF_CHECKED);
  209. }
  210. break;
  211. case IDM_XT:
  212. nShowFunc = 1;
  213. break;
  214. case IDM_YT:
  215. nShowFunc = 2;
  216. break;
  217. case IDM_YX:
  218. nShowFunc = 3;
  219. break;
  220. }
  221. InvalidateRect(hwnd, NULL, TRUE); //перерисовать окно вместе с фоном
  222. break;
  223.  
  224. case WM_PAINT:
  225. hdc = BeginPaint(hwnd, &ps);
  226. if (bShowAxis)
  227. DrawAxis(hdc, ps.rcPaint);
  228. SetTextColor(hdc, 0x00FF0000);
  229. switch (nShowFunc)
  230. {
  231. case 0:
  232. TextOut(hdc, 10, 20, _T("Для начала работы выберите пункт в разделе Меню"), 47);
  233. break;
  234. case 1:
  235. TextOut(hdc, 10, 20, _T("x(t) = 2sin(t) + cos(3t)"), 28);
  236. DrawGraphXT(hdc, ps.rcPaint);
  237. break;
  238. case 2:
  239. TextOut(hdc, 10, 20, _T("y(t) = cos(3t) + cos(t)"), 24);
  240. DrawGraphYT(hdc, ps.rcPaint);
  241. break;
  242. case 3:
  243. TextOut(hdc, 10, 20, _T("y(t) = cos(3t) + cos(t)"), 24);
  244. TextOut(hdc, 10, 40, _T("x(t) = 2sin(t) + cos(3t)"), 28);
  245.  
  246. DrawGraphYX(hdc, ps.rcPaint);
  247. break;
  248. }
  249. SetTextColor(hdc, 0x00FF0000);
  250. EndPaint(hwnd, &ps);
  251. break;
  252. case WM_DESTROY:
  253. PostQuitMessage(0);
  254. break;
  255. default:
  256. return DefWindowProc(hwnd, Message, wParam, lParam);
  257. }
  258. return 0;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement