Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.08 KB | None | 0 0
  1. // кр2сем2Корбит.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "кр2сем2Корбит.h"
  6.  
  7. #define MAX_LOADSTRING 100
  8.  
  9. #include "stdafx.h"
  10. #include "кр2сем2Корбит.h"
  11. #include <string>
  12. #define _USE_MATH_DEFINES
  13. #include <ostream>
  14. #include <iostream>
  15. #include <commdlg.h>
  16. #define DLGTITLE L"My dialog"
  17. #define DLGFONT L"MS Shell Dlg"
  18.  
  19. #define MAX_LOADSTRING 100
  20. #include <windows.h>
  21. #include <windowsx.h>
  22. #include <cmath>
  23.  
  24. #define APP_NAME "MyWindowTest"
  25.  
  26. int N = 3;
  27.  
  28. bool firstmove = true;
  29. bool secondmove = true;
  30. double g = 0.6;
  31. int speed = 0;
  32. int t = 0;
  33. double phi = -3.14;
  34. double len2 = sqrt(420 * 420 + 4 * 4);
  35. bool down = true;
  36. double frec;
  37. double main_angle;
  38. int window_width = 800;
  39. int window_height = 500;
  40. HINSTANCE hInst;
  41. HBRUSH hWindowBrush;
  42. HBRUSH hBrush;
  43. int mx = window_width;
  44. int my = window_height;
  45. bool paused = false;
  46. int x3, y3, y2, x2;
  47. int len = 400;
  48. int mxx = 400, myy = len2;
  49. HBRUSH hRedBrush;
  50. static const int MinDialogWidth = 300;
  51. static const int MinDialogHeight = 250;
  52. static const int Timer1ID = 1;
  53.  
  54. static const int Timer1Interval = 10;
  55.  
  56. static const int Timer1MaxValue = 1;
  57.  
  58. static int _timer1Ticks = 0;
  59. namespace Control
  60. {
  61. int const IDC_TestTextBoxB = 1001;
  62. int const IDC_TestTextBoxA = 1002;
  63. int const IDC_TestButtonOk = 1003;
  64. int const IDC_TestButtonCancel = 1004;
  65. int const IDC_TestARadioButton = 1005;
  66. int const IDC_TestBRadioButton = 1006;
  67. int const IDC_TestComboBox = 1007;
  68. int const IDC_InvisibleButton = 1008;
  69. int const IDC_Test2GroupBox = 1009;
  70. int const IDC_TestCRadioButton = 1010;
  71. int const IDC_TestDRadioButton = 1011;
  72. int const IDC_MessageButton = 1012;
  73. int const IDC_TestLabel = 1013;
  74. }
  75.  
  76.  
  77. #pragma pack(push, 4)
  78. static struct
  79. {
  80. DWORD style;
  81. DWORD dwExtendedStyle;
  82. WORD ccontrols;
  83. short x;
  84. short y;
  85. short cx;
  86. short cy;
  87. WORD menu;
  88. WORD windowClass;
  89. WCHAR wszTitle[ARRAYSIZE(DLGTITLE)];
  90. short pointsize;
  91. WCHAR wszFont[ARRAYSIZE(DLGFONT)];
  92. }
  93. EmptyDialogTemplate =
  94. {
  95. DS_SHELLFONT | DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU, // ?????????!
  96. 0x0,
  97. 0,
  98. 0, 0, 150, 80,
  99. 0, // menu: none
  100. 0, // window class: none
  101. DLGTITLE, // Window caption
  102. 8, // font pointsize
  103. DLGFONT,
  104. };
  105. class MathToHdcConverter
  106. {
  107. HWND _hWnd;
  108.  
  109. public:
  110. explicit MathToHdcConverter(HWND hWnd) : _hWnd(hWnd) { }
  111.  
  112. int GetX(const double x) const
  113. {
  114. return GetCenterY() + x;
  115. }
  116.  
  117. int GetY(const double y) const
  118. {
  119. return GetCenterX() - y;
  120. }
  121.  
  122.  
  123.  
  124. double PolarToX(double r, double phi)
  125. {
  126. return r * cos(phi);; // TODO.
  127. }
  128.  
  129. double PolarToY(double r, double phi)
  130. {
  131. return r * sin(phi); // TODO.
  132. }
  133.  
  134. private:
  135. int GetCenterX() const
  136. {
  137. return GetClientRect().right / 2;
  138. }
  139.  
  140. int GetCenterY() const
  141. {
  142. return 50;
  143. }
  144.  
  145. RECT GetClientRect() const
  146. {
  147. RECT clientRect;
  148. ::GetClientRect(_hWnd, &clientRect);
  149. return clientRect;
  150. }
  151.  
  152. };
  153.  
  154. #pragma pack(pop)
  155. void OnTimer1(HWND hWnd, int timerID)
  156. {
  157. if (timerID != Timer1ID) return;
  158. if (!paused)
  159. ++_timer1Ticks;
  160.  
  161. if (_timer1Ticks > Timer1MaxValue)
  162. {
  163. _timer1Ticks = 0;
  164. }
  165.  
  166. InvalidateRect(hWnd, nullptr, true);
  167. }
  168.  
  169. void CalculatePositionFor1(HWND hWnd)
  170. {
  171. if (_timer1Ticks == 0 && !paused)
  172. {
  173. if (firstmove) {
  174. secondmove = false;
  175. auto converter = MathToHdcConverter(hWnd);
  176. if (down)
  177. {
  178. t++;
  179. frec = sqrt(((double)g) / 420); //420-length1 and radius
  180. main_angle = frec*t;
  181. phi = main_angle;
  182. auto a = converter.PolarToX(420, phi);
  183. y2 = converter.GetX(a);
  184. auto b = converter.PolarToY(420, phi);
  185. mx = converter.GetY(b);
  186. }
  187. }
  188. }
  189. }
  190. void CalculatePositionFor2(HWND hWnd) {
  191. if (_timer1Ticks == 0 && !paused)
  192. {
  193. if (secondmove) {
  194. auto converter = MathToHdcConverter(hWnd);
  195. if (down)
  196. {
  197. t++;
  198. frec = sqrt(((double)g) / len2);
  199. main_angle = frec*t;
  200. phi = -main_angle;
  201. auto a = converter.PolarToX(len2, phi);
  202. myy = converter.GetX(a);
  203. auto b = converter.PolarToY(len2, phi);
  204. mxx = converter.GetY(b);
  205. }
  206. }
  207. }
  208. }
  209.  
  210. void CheckDown(HWND hWnd, double phi)
  211. {
  212. if (phi >= 5.175*g) {
  213. firstmove = false;
  214. secondmove = true;
  215. t = 0;
  216. phi = 0;
  217. }
  218. if (firstmove == false) {
  219. if (phi >= 5.165*g) {
  220. firstmove = true;
  221. secondmove = false;
  222. t = 0;
  223. }
  224. }
  225. }
  226.  
  227. int GetNewAcceleration(HWND hWnd)
  228. {
  229. auto textLength = GetWindowTextLengthW(hWnd);
  230. auto buffer = new wchar_t[textLength + 1];
  231. GetWindowText(hWnd, buffer, textLength + 1);
  232. double result = wcstod(buffer, NULL);
  233. return result;
  234. }
  235. void OnGetMinMaxInfo(LPMINMAXINFO minMaxInfo)
  236. {
  237. minMaxInfo->ptMinTrackSize.x = MinDialogWidth;
  238. minMaxInfo->ptMinTrackSize.y = MinDialogHeight;
  239. }
  240. INT_PTR CALLBACK DlgProc(HWND hwndDlg, UINT msg, WPARAM wParam,
  241. LPARAM lParam)
  242. {
  243. switch (msg)
  244. {
  245. case WM_INITDIALOG:
  246. {
  247. CreateWindowEx(NULL, TEXT("STATIC"), TEXT("Edit a : "),
  248. WS_CHILD | WS_VISIBLE, 10, 10, 80,
  249. 20, hwndDlg, nullptr, nullptr, nullptr);
  250. CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT(""),
  251. WS_CHILD | WS_VISIBLE, 100, 10, 60,
  252. 20, hwndDlg, (HMENU)Control::IDC_TestTextBoxA, nullptr, nullptr);
  253.  
  254. CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Button"), TEXT("Ok"),
  255. WS_CHILD | WS_VISIBLE, 100, 40, 30,
  256. 20, hwndDlg, (HMENU)Control::IDC_TestButtonOk, NULL, NULL);
  257. CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Button"), TEXT("Cancel"),
  258. WS_CHILD | WS_VISIBLE, 150, 40, 50,
  259. 20, hwndDlg, (HMENU)Control::IDC_TestButtonCancel, NULL, NULL);
  260.  
  261.  
  262. return (INT_PTR)TRUE;
  263. }
  264. case WM_COMMAND:
  265. {
  266.  
  267. if (wParam == Control::IDC_TestButtonOk)
  268. {
  269. auto TextA = GetDlgItem(hwndDlg, Control::IDC_TestTextBoxA);
  270. if (GetWindowTextLengthW(TextA) != 0)
  271. if (GetNewAcceleration(TextA) >= 0)
  272. {
  273. g = GetNewAcceleration(TextA);
  274. if (paused)
  275. paused = !paused;
  276. }
  277. else
  278. MessageBoxA(hwndDlg, "Wrong data", NULL, MB_OK);
  279.  
  280. EndDialog(hwndDlg, IDCANCEL);
  281. }
  282. else
  283. {
  284. if (wParam == Control::IDC_TestButtonCancel)
  285. EndDialog(hwndDlg, IDCANCEL);
  286. }
  287.  
  288. }
  289. break;
  290. case WM_CLOSE:
  291. {
  292. EndDialog(hwndDlg, 0);
  293. return FALSE;
  294. }
  295. }
  296. return FALSE;
  297. }
  298.  
  299.  
  300. LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  301. {
  302. switch (msg)
  303. {
  304. case WM_CREATE:
  305. {
  306. hWindowBrush = CreateSolidBrush(RGB(100, 30, 30));
  307. hRedBrush = CreateSolidBrush(RGB(255, 0, 0));
  308. SetTimer(hwnd, 1, 1, NULL);
  309. }
  310. break;
  311.  
  312. case WM_RBUTTONDBLCLK:
  313. {
  314. hBrush = CreateSolidBrush(RGB(0, 255, 255)); // RED TO GREEN
  315. hRedBrush = hBrush;
  316. }
  317. break;
  318. case WM_KEYDOWN:
  319. {
  320. switch (wParam)
  321. {
  322. break;
  323. case VK_F3:
  324. {
  325. DialogBoxIndirectParamW(hInst, (LPCDLGTEMPLATEW)&EmptyDialogTemplate, hwnd, DlgProc, NULL);
  326. break;
  327. }
  328. break;
  329. case VK_SPACE:
  330. {
  331. paused = !paused;
  332. }
  333. break;
  334. }
  335.  
  336. }
  337. break;
  338. case WM_TIMER:
  339. {
  340. OnTimer1(hwnd, (int)wParam);
  341. }
  342.  
  343. case WM_PAINT:
  344. {
  345. PAINTSTRUCT ps;
  346. HDC hdc = BeginPaint(hwnd, &ps);
  347.  
  348. HDC hMemDC = CreateCompatibleDC(hdc);
  349. HBITMAP hScreen = CreateCompatibleBitmap(hdc, window_width, window_height);
  350. HBITMAP oldBmp = (HBITMAP)SelectObject(hMemDC, hScreen);
  351. PatBlt(hMemDC, 0, 0, window_width, window_height, WHITENESS);
  352.  
  353. SetBkMode(hMemDC, TRANSPARENT);
  354.  
  355. FillRect(hMemDC, &ps.rcPaint, hWindowBrush);
  356.  
  357. SelectObject(hMemDC, CreateSolidBrush(RGB(255, 0, 0)));
  358. Rectangle(hMemDC, 350, 10, 450, 20);
  359.  
  360. int x1 = 380, y1 = 20;
  361. x2 = mx, y2 = 0;
  362. x3 = mxx;
  363. y3 = myy;
  364. for (int i = y1; i <= window_height; i++)
  365. {
  366. int y = (int)sqrt(pow(x1 - x2, 2) + pow(y1 - i, 2));
  367. if (y == len)
  368. {
  369. y2 = i;
  370. i = window_height;
  371. }
  372. }
  373. for (int i = y1; i <= window_height; i++)
  374. {
  375. int y = (int)sqrt(pow(x1 - x3, 2) + pow(y1 - i, 2));
  376. if (y == len)
  377. {
  378. y3 = i;
  379. i = window_height;
  380. }
  381. }
  382.  
  383. MoveToEx(hMemDC, x1, y1, NULL);
  384. LineTo(hMemDC, x2, y2);
  385. MoveToEx(hMemDC, x1, y1, NULL);
  386. LineTo(hMemDC, x3 - 4, y3);
  387. MoveToEx(hMemDC, x1, y1, NULL);
  388. LineTo(hMemDC, x3 + 4, y3);
  389.  
  390. SelectBrush(hMemDC, hRedBrush);
  391. Ellipse(hMemDC, x2 - 4, y2 - 4, x2 + 4, y2 + 4);
  392. Ellipse(hMemDC, x3 - 8, y3 - 4, x3, y3 + 4);
  393. Ellipse(hMemDC, x3, y3 - 4, x3 + 8, y3 + 4);
  394. BitBlt(hdc, 0, 0, window_width, window_height, hMemDC, 0, 0, SRCCOPY);
  395.  
  396. CheckDown(hwnd, main_angle);
  397. CalculatePositionFor1(hwnd);
  398. CalculatePositionFor2(hwnd);
  399.  
  400. SelectObject(hMemDC, oldBmp);
  401. DeleteObject(hScreen);
  402. DeleteDC(hMemDC);
  403.  
  404. EndPaint(hwnd, &ps);
  405. }
  406. break;
  407.  
  408. case WM_DESTROY:
  409. {
  410. DeleteBrush(hWindowBrush);
  411. KillTimer(hwnd, 1);
  412. PostQuitMessage(0);
  413. }
  414. break;
  415. }
  416. return DefWindowProc(hwnd, msg, wParam, lParam);
  417. }
  418. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  419. {
  420. LPCWSTR Class = L"Animation_Class";
  421.  
  422. WNDCLASS wc = { 0 };
  423. ZeroMemory(&wc, sizeof(wc));
  424.  
  425. wc.style = CS_DBLCLKS;
  426. wc.lpfnWndProc = WindowProcedure;
  427. wc.hInstance = hInstance;
  428. wc.hIcon = LoadIcon(hInstance, (LPCWSTR)VP_TV_STANDARD_WIN_VGA);
  429. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  430. wc.hbrBackground = CreateSolidBrush(RGB(100, 30, 30));
  431. wc.lpszClassName = Class;
  432. wc.lpszMenuName = NULL;
  433. wc.cbClsExtra = 0;
  434. wc.cbWndExtra = 0;
  435.  
  436. if (!RegisterClass(&wc))
  437. {
  438. MessageBox(NULL, L"Window Registration Failed!", L"Error!", MB_OK | MB_ICONERROR);
  439. return 0;
  440. }
  441.  
  442. HWND hwnd = CreateWindow(Class, L"Animation",
  443. WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX,
  444. (GetSystemMetrics(SM_CXSCREEN) >> 1) - (window_width >> 1),
  445. (GetSystemMetrics(SM_CYSCREEN) >> 1) - (window_height >> 1),
  446. window_width, window_height, NULL, NULL, hInstance, NULL);
  447. if (!hwnd)
  448. {
  449. MessageBox(NULL, L"Window Creation Failed!", L"Error!", MB_OK | MB_ICONERROR);
  450. return 0;
  451. }
  452.  
  453. ShowWindow(hwnd, SW_SHOW);
  454. ShowCursor(SW_SHOW);
  455. UpdateWindow(hwnd);
  456.  
  457. MSG msg;
  458. while (GetMessage(&msg, NULL, 0, 0))
  459. {
  460. DispatchMessage(&msg);
  461. TranslateMessage(&msg);
  462. }
  463.  
  464. UnregisterClass(Class, hInstance);
  465. return (int)msg.wParam;
  466. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement