Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // кр2сем2Корбит.cpp : Defines the entry point for the application.
- //
- #include "stdafx.h"
- #include "кр2сем2Корбит.h"
- #define MAX_LOADSTRING 100
- #include "stdafx.h"
- #include "кр2сем2Корбит.h"
- #include <string>
- #define _USE_MATH_DEFINES
- #include <ostream>
- #include <iostream>
- #include <commdlg.h>
- #define DLGTITLE L"My dialog"
- #define DLGFONT L"MS Shell Dlg"
- #define MAX_LOADSTRING 100
- #include <windows.h>
- #include <windowsx.h>
- #include <cmath>
- #define APP_NAME "MyWindowTest"
- int N = 3;
- bool firstmove = true;
- bool secondmove = true;
- double g = 0.6;
- int speed = 0;
- int t = 0;
- double phi = -3.14;
- double len2 = sqrt(420 * 420 + 4 * 4);
- bool down = true;
- double frec;
- double main_angle;
- int window_width = 800;
- int window_height = 500;
- HINSTANCE hInst;
- HBRUSH hWindowBrush;
- HBRUSH hBrush;
- int mx = window_width;
- int my = window_height;
- bool paused = false;
- int x3, y3, y2, x2;
- int len = 400;
- int mxx = 400, myy = len2;
- HBRUSH hRedBrush;
- static const int MinDialogWidth = 300;
- static const int MinDialogHeight = 250;
- static const int Timer1ID = 1;
- static const int Timer1Interval = 10;
- static const int Timer1MaxValue = 1;
- static int _timer1Ticks = 0;
- namespace Control
- {
- int const IDC_TestTextBoxB = 1001;
- int const IDC_TestTextBoxA = 1002;
- int const IDC_TestButtonOk = 1003;
- int const IDC_TestButtonCancel = 1004;
- int const IDC_TestARadioButton = 1005;
- int const IDC_TestBRadioButton = 1006;
- int const IDC_TestComboBox = 1007;
- int const IDC_InvisibleButton = 1008;
- int const IDC_Test2GroupBox = 1009;
- int const IDC_TestCRadioButton = 1010;
- int const IDC_TestDRadioButton = 1011;
- int const IDC_MessageButton = 1012;
- int const IDC_TestLabel = 1013;
- }
- #pragma pack(push, 4)
- static struct
- {
- DWORD style;
- DWORD dwExtendedStyle;
- WORD ccontrols;
- short x;
- short y;
- short cx;
- short cy;
- WORD menu;
- WORD windowClass;
- WCHAR wszTitle[ARRAYSIZE(DLGTITLE)];
- short pointsize;
- WCHAR wszFont[ARRAYSIZE(DLGFONT)];
- }
- EmptyDialogTemplate =
- {
- DS_SHELLFONT | DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU, // ?????????!
- 0x0,
- 0,
- 0, 0, 150, 80,
- 0, // menu: none
- 0, // window class: none
- DLGTITLE, // Window caption
- 8, // font pointsize
- DLGFONT,
- };
- class MathToHdcConverter
- {
- HWND _hWnd;
- public:
- explicit MathToHdcConverter(HWND hWnd) : _hWnd(hWnd) { }
- int GetX(const double x) const
- {
- return GetCenterY() + x;
- }
- int GetY(const double y) const
- {
- return GetCenterX() - y;
- }
- double PolarToX(double r, double phi)
- {
- return r * cos(phi);; // TODO.
- }
- double PolarToY(double r, double phi)
- {
- return r * sin(phi); // TODO.
- }
- private:
- int GetCenterX() const
- {
- return GetClientRect().right / 2;
- }
- int GetCenterY() const
- {
- return 50;
- }
- RECT GetClientRect() const
- {
- RECT clientRect;
- ::GetClientRect(_hWnd, &clientRect);
- return clientRect;
- }
- };
- #pragma pack(pop)
- void OnTimer1(HWND hWnd, int timerID)
- {
- if (timerID != Timer1ID) return;
- if (!paused)
- ++_timer1Ticks;
- if (_timer1Ticks > Timer1MaxValue)
- {
- _timer1Ticks = 0;
- }
- InvalidateRect(hWnd, nullptr, true);
- }
- void CalculatePositionFor1(HWND hWnd)
- {
- if (_timer1Ticks == 0 && !paused)
- {
- if (firstmove) {
- secondmove = false;
- auto converter = MathToHdcConverter(hWnd);
- if (down)
- {
- t++;
- frec = sqrt(((double)g) / 420); //420-length1 and radius
- main_angle = frec*t;
- phi = main_angle;
- auto a = converter.PolarToX(420, phi);
- y2 = converter.GetX(a);
- auto b = converter.PolarToY(420, phi);
- mx = converter.GetY(b);
- }
- }
- }
- }
- void CalculatePositionFor2(HWND hWnd) {
- if (_timer1Ticks == 0 && !paused)
- {
- if (secondmove) {
- auto converter = MathToHdcConverter(hWnd);
- if (down)
- {
- t++;
- frec = sqrt(((double)g) / len2);
- main_angle = frec*t;
- phi = -main_angle;
- auto a = converter.PolarToX(len2, phi);
- myy = converter.GetX(a);
- auto b = converter.PolarToY(len2, phi);
- mxx = converter.GetY(b);
- }
- }
- }
- }
- void CheckDown(HWND hWnd, double phi)
- {
- if (phi >= 5.175*g) {
- firstmove = false;
- secondmove = true;
- t = 0;
- phi = 0;
- }
- if (firstmove == false) {
- if (phi >= 5.165*g) {
- firstmove = true;
- secondmove = false;
- t = 0;
- }
- }
- }
- int GetNewAcceleration(HWND hWnd)
- {
- auto textLength = GetWindowTextLengthW(hWnd);
- auto buffer = new wchar_t[textLength + 1];
- GetWindowText(hWnd, buffer, textLength + 1);
- double result = wcstod(buffer, NULL);
- return result;
- }
- void OnGetMinMaxInfo(LPMINMAXINFO minMaxInfo)
- {
- minMaxInfo->ptMinTrackSize.x = MinDialogWidth;
- minMaxInfo->ptMinTrackSize.y = MinDialogHeight;
- }
- INT_PTR CALLBACK DlgProc(HWND hwndDlg, UINT msg, WPARAM wParam,
- LPARAM lParam)
- {
- switch (msg)
- {
- case WM_INITDIALOG:
- {
- CreateWindowEx(NULL, TEXT("STATIC"), TEXT("Edit a : "),
- WS_CHILD | WS_VISIBLE, 10, 10, 80,
- 20, hwndDlg, nullptr, nullptr, nullptr);
- CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT(""),
- WS_CHILD | WS_VISIBLE, 100, 10, 60,
- 20, hwndDlg, (HMENU)Control::IDC_TestTextBoxA, nullptr, nullptr);
- CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Button"), TEXT("Ok"),
- WS_CHILD | WS_VISIBLE, 100, 40, 30,
- 20, hwndDlg, (HMENU)Control::IDC_TestButtonOk, NULL, NULL);
- CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Button"), TEXT("Cancel"),
- WS_CHILD | WS_VISIBLE, 150, 40, 50,
- 20, hwndDlg, (HMENU)Control::IDC_TestButtonCancel, NULL, NULL);
- return (INT_PTR)TRUE;
- }
- case WM_COMMAND:
- {
- if (wParam == Control::IDC_TestButtonOk)
- {
- auto TextA = GetDlgItem(hwndDlg, Control::IDC_TestTextBoxA);
- if (GetWindowTextLengthW(TextA) != 0)
- if (GetNewAcceleration(TextA) >= 0)
- {
- g = GetNewAcceleration(TextA);
- if (paused)
- paused = !paused;
- }
- else
- MessageBoxA(hwndDlg, "Wrong data", NULL, MB_OK);
- EndDialog(hwndDlg, IDCANCEL);
- }
- else
- {
- if (wParam == Control::IDC_TestButtonCancel)
- EndDialog(hwndDlg, IDCANCEL);
- }
- }
- break;
- case WM_CLOSE:
- {
- EndDialog(hwndDlg, 0);
- return FALSE;
- }
- }
- return FALSE;
- }
- LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch (msg)
- {
- case WM_CREATE:
- {
- hWindowBrush = CreateSolidBrush(RGB(100, 30, 30));
- hRedBrush = CreateSolidBrush(RGB(255, 0, 0));
- SetTimer(hwnd, 1, 1, NULL);
- }
- break;
- case WM_RBUTTONDBLCLK:
- {
- hBrush = CreateSolidBrush(RGB(0, 255, 255)); // RED TO GREEN
- hRedBrush = hBrush;
- }
- break;
- case WM_KEYDOWN:
- {
- switch (wParam)
- {
- break;
- case VK_F3:
- {
- DialogBoxIndirectParamW(hInst, (LPCDLGTEMPLATEW)&EmptyDialogTemplate, hwnd, DlgProc, NULL);
- break;
- }
- break;
- case VK_SPACE:
- {
- paused = !paused;
- }
- break;
- }
- }
- break;
- case WM_TIMER:
- {
- OnTimer1(hwnd, (int)wParam);
- }
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hwnd, &ps);
- HDC hMemDC = CreateCompatibleDC(hdc);
- HBITMAP hScreen = CreateCompatibleBitmap(hdc, window_width, window_height);
- HBITMAP oldBmp = (HBITMAP)SelectObject(hMemDC, hScreen);
- PatBlt(hMemDC, 0, 0, window_width, window_height, WHITENESS);
- SetBkMode(hMemDC, TRANSPARENT);
- FillRect(hMemDC, &ps.rcPaint, hWindowBrush);
- SelectObject(hMemDC, CreateSolidBrush(RGB(255, 0, 0)));
- Rectangle(hMemDC, 350, 10, 450, 20);
- int x1 = 380, y1 = 20;
- x2 = mx, y2 = 0;
- x3 = mxx;
- y3 = myy;
- for (int i = y1; i <= window_height; i++)
- {
- int y = (int)sqrt(pow(x1 - x2, 2) + pow(y1 - i, 2));
- if (y == len)
- {
- y2 = i;
- i = window_height;
- }
- }
- for (int i = y1; i <= window_height; i++)
- {
- int y = (int)sqrt(pow(x1 - x3, 2) + pow(y1 - i, 2));
- if (y == len)
- {
- y3 = i;
- i = window_height;
- }
- }
- MoveToEx(hMemDC, x1, y1, NULL);
- LineTo(hMemDC, x2, y2);
- MoveToEx(hMemDC, x1, y1, NULL);
- LineTo(hMemDC, x3 - 4, y3);
- MoveToEx(hMemDC, x1, y1, NULL);
- LineTo(hMemDC, x3 + 4, y3);
- SelectBrush(hMemDC, hRedBrush);
- Ellipse(hMemDC, x2 - 4, y2 - 4, x2 + 4, y2 + 4);
- Ellipse(hMemDC, x3 - 8, y3 - 4, x3, y3 + 4);
- Ellipse(hMemDC, x3, y3 - 4, x3 + 8, y3 + 4);
- BitBlt(hdc, 0, 0, window_width, window_height, hMemDC, 0, 0, SRCCOPY);
- CheckDown(hwnd, main_angle);
- CalculatePositionFor1(hwnd);
- CalculatePositionFor2(hwnd);
- SelectObject(hMemDC, oldBmp);
- DeleteObject(hScreen);
- DeleteDC(hMemDC);
- EndPaint(hwnd, &ps);
- }
- break;
- case WM_DESTROY:
- {
- DeleteBrush(hWindowBrush);
- KillTimer(hwnd, 1);
- PostQuitMessage(0);
- }
- break;
- }
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- LPCWSTR Class = L"Animation_Class";
- WNDCLASS wc = { 0 };
- ZeroMemory(&wc, sizeof(wc));
- wc.style = CS_DBLCLKS;
- wc.lpfnWndProc = WindowProcedure;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(hInstance, (LPCWSTR)VP_TV_STANDARD_WIN_VGA);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = CreateSolidBrush(RGB(100, 30, 30));
- wc.lpszClassName = Class;
- wc.lpszMenuName = NULL;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- if (!RegisterClass(&wc))
- {
- MessageBox(NULL, L"Window Registration Failed!", L"Error!", MB_OK | MB_ICONERROR);
- return 0;
- }
- HWND hwnd = CreateWindow(Class, L"Animation",
- WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX,
- (GetSystemMetrics(SM_CXSCREEN) >> 1) - (window_width >> 1),
- (GetSystemMetrics(SM_CYSCREEN) >> 1) - (window_height >> 1),
- window_width, window_height, NULL, NULL, hInstance, NULL);
- if (!hwnd)
- {
- MessageBox(NULL, L"Window Creation Failed!", L"Error!", MB_OK | MB_ICONERROR);
- return 0;
- }
- ShowWindow(hwnd, SW_SHOW);
- ShowCursor(SW_SHOW);
- UpdateWindow(hwnd);
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0))
- {
- DispatchMessage(&msg);
- TranslateMessage(&msg);
- }
- UnregisterClass(Class, hInstance);
- return (int)msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement