-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
222 lines (183 loc) · 5.8 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"
#include <vector>
#include <cmath>
#include <string>
#define M_PI 3.14159
HINSTANCE hInst;
using namespace std;
PAINTSTRUCT _paintstruct;
HWND hDlg;
HINSTANCE hInstance;
HBRUSH PointBrush = CreateSolidBrush(RGB(255, 127, 0));
int prev_timer = 0;
int OffsetX = 0;
int OffsetY = 0;
static int ticks = 0;
double cur_angle = 0.0;
int cx, cy;
POINT point;
HWND UP, DOWN, LEFT, RIGHT;
const int ID_Up=1002,
ID_Down=1003,
ID_Left=1004,
ID_Right = 1005,
SCALE = 100,
AXIS_LEN= 1000,
F_RAD = 500,
P_RAD = 30,
AXIS_MOVING = 50;
const double F_K = 4.0/3.0,
F_PERIOD = 6*M_PI;
POINT Figure(double angle)
{
POINT p;
p.x = F_RAD*(cos(F_K*angle))*cos(angle) + OffsetX;
p.y = F_RAD*(cos(F_K*angle))*sin(angle) + OffsetY;
return p;
}
void SetImage(const char* caption, HWND& control)
{
auto bmp = (HBITMAP)LoadImage(nullptr, caption, IMAGE_BITMAP, NULL, NULL, LR_LOADFROMFILE);
SendMessage(control, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)bmp);
}
HWND CreateControl(HWND &parent, const char* className, long style, int x, int y, int width, int heigth, int id)
{
return CreateWindow(
className, "",
WS_CHILD | WS_VISIBLE | style,
x, y,
width, heigth,
parent,
(HMENU)id,
hInst,
nullptr);
}
void CreateButtons(HWND hwndDlg)
{
UP = CreateControl(hwndDlg,WC_BUTTON, BS_BITMAP, 650, 10, 50, 50, ID_Up);
RIGHT = CreateControl(hwndDlg, WC_BUTTON, BS_BITMAP, 700, 60, 50, 50, ID_Right);
DOWN = CreateControl(hwndDlg, WC_BUTTON, BS_BITMAP, 650, 110, 50, 50, ID_Down);
LEFT = CreateControl(hwndDlg, WC_BUTTON, BS_BITMAP, 600, 60, 50, 50, ID_Left);
SetImage("up.bmp", UP);
SetImage("right.bmp", RIGHT);
SetImage("down.bmp", DOWN);
SetImage("left.bmp", LEFT);
}
void CALLBACK TimerProc(HWND hwndDlg, UINT _, UINT_PTR __, DWORD ms)
{
auto delta = ms - prev_timer;
prev_timer = ms;
cur_angle -= 0.001 * delta;
point = Figure(cur_angle);
if (cur_angle < -F_PERIOD) cur_angle = 0.0;
InvalidateRect(hwndDlg, NULL, FALSE);
}
void OnPaint(HWND &hwndDlg)
{
HDC hRealDC;
RECT rect;
hRealDC = BeginPaint(hwndDlg, &_paintstruct);
GetClientRect(hwndDlg, &rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
//Double buffering
auto hdc = CreateCompatibleDC(hRealDC);
auto CompBitmap = CreateCompatibleBitmap(hRealDC, width, height);
auto OldBitmap = (HBITMAP)SelectObject(hdc, CompBitmap);
PatBlt(hdc, 0, 0, width, height, WHITENESS);
//Coord transformation
SetMapMode(hdc, MM_ANISOTROPIC);
SetWindowExtEx(hdc, 1000, 1000, nullptr);
SetViewportExtEx(hdc, (rect.right - rect.left) / 2, -(rect.bottom - rect.top) / 2, 0);
SetViewportOrgEx(hdc, (rect.right - rect.left) / 2, (rect.bottom - rect.top) / 2, 0);
//Axises drawing
MoveToEx(hdc, -AXIS_LEN, 0, 0);
LineTo(hdc, AXIS_LEN, 0);
MoveToEx(hdc, 0, AXIS_LEN, 0);
LineTo(hdc, 0, -AXIS_LEN);
//Numbers drawing
SetTextAlign(hdc, TA_CENTER);
for (int i = 0, a = -10; i <= 20; i++, a++)
{
MoveToEx(hdc, -AXIS_LEN + SCALE*i, -10, 0);
LineTo(hdc, -AXIS_LEN + SCALE*i, 10);
MoveToEx(hdc, -10, -AXIS_LEN + SCALE*i, 0);
LineTo(hdc, 10, -AXIS_LEN + SCALE*i);
if (a != 0)
{
TextOut(hdc,-AXIS_LEN + SCALE*i, -20, (LPCSTR)to_string(a).c_str(), to_string(a).size());
TextOut(hdc, -40, 25-AXIS_LEN + SCALE*i, (LPCSTR)to_string(a).c_str(), to_string(a).size());
}
}
//Figure drawing
MoveToEx(hdc, F_RAD+OffsetX, OffsetY, 0);
for (double angle = 0.0; angle < F_PERIOD; angle += 0.05)
{
POINT p = Figure(angle);
LineTo(hdc, p.x, p.y);
}
//Point drawing
SelectObject(hdc, PointBrush);
Ellipse(hdc, point.x - P_RAD, point.y - P_RAD, point.x + P_RAD, point.y + P_RAD);
//Coord transformation
SetMapMode(hdc, MM_TEXT);
SetWindowExtEx(hdc, (rect.right - rect.left) / 2, (rect.bottom - rect.top) / 2, nullptr);
SetViewportExtEx(hdc, 0, 0, 0);
SetViewportOrgEx(hdc, 0, 0, 0);
//Double buffering
BitBlt(hRealDC, 0, 0, width, height, hdc, 0, 0, SRCCOPY);
DeleteObject(CompBitmap);
DeleteDC(hdc);
EndPaint(hwndDlg, &_paintstruct);
}
void OnCommand(WPARAM wParam, HINSTANCE hInstance, HWND hwndDlg)
{
switch (LOWORD(wParam))
{
case ID_Up:
OffsetY += AXIS_MOVING;
break;
case ID_Down:
OffsetY -= AXIS_MOVING;
break;
case ID_Right:
OffsetX += AXIS_MOVING;
break;
case ID_Left:
OffsetX -= AXIS_MOVING;
break;
}
}
BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
hDlg = hwndDlg;
SetTimer(hwndDlg, 1, 10, TimerProc);
CreateButtons(hwndDlg);
return TRUE;
case WM_CLOSE:
DestroyWindow(hwndDlg);
return TRUE;
case WM_COMMAND:
OnCommand(wParam, hInstance, hwndDlg);
break;
case WM_PAINT:
OnPaint(hwndDlg);
break;
case WM_DESTROY:
PostQuitMessage(0);
return TRUE;
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst=hInstance;
InitCommonControls();
return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}