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
| #include <windows.h>
#include <windowsx.h>
#include <cmath>
const int window_width = 640;
const int window_height = 480;
HBRUSH hWindowBrush;
HPEN hPen;
HBRUSH hBrush1, hBrush2;
int R = 200, xr = window_width/2, yr = window_height/2, r = 30;
double x, y; // R, xr, yr - радиус и координаты цента траектории; r - радиус маленького эллипса.
double fi;
double h1 = 3;
double x2 = 360;
double x3 = 0;
class Base
{
public:
virtual void draw(HDC h, int r, int x, int y) = 0;
};
class tCircle : public Base
{
public:
void draw(HDC h, int r, int x, int y)
{
HBRUSH hbr = CreateSolidBrush(RGB(255, 0, 30));
int x1, x2, y1, y2;//координаты углов прямоугольника вокруг эллипса
x1 = x - r;
y1 = y - r / 2;
x2 = x + r;
y2 = y + r / 2;
SelectObject(h, hbr);
Ellipse(h, x1, y1, x2, y2);//отрисовка эллипса
}
};
class tEllipse : public Base
{
public:
void draw(HDC h, int r, int x, int y)
{
HBRUSH hbr = CreateSolidBrush(RGB(255, 255, 30));
int x1, x2, y1, y2;//координаты углов прямоугольника вокруг эллипса
x1 = x - r / 2;
y1 = y - r;
x2 = x + r / 2;
y2 = y + r;
SelectObject(h, hbr);
Ellipse(h, x1, y1, x2, y2);//отрисовка эллипса
}
};
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
hWindowBrush = CreateSolidBrush(RGB(255, 255, 255));
//hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
//hBrush1 = CreateSolidBrush(RGB(255, 255, 255));
//hBrush2 = CreateSolidBrush(RGB(255, 0, 30));
SetTimer(hwnd, 1, 10, NULL);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HDC hMemDC = CreateCompatibleDC(hdc);
HBITMAP hScreen = CreateCompatibleBitmap(hdc, window_width, window_height+20);
HBITMAP oldBmp = (HBITMAP)SelectObject(hMemDC, hScreen);
PatBlt(hMemDC, 0, 0, window_width, window_height, WHITENESS);
SetBkMode(hMemDC, TRANSPARENT);
FillRect(hMemDC, &ps.rcPaint, hWindowBrush);
//SelectPen(hMemDC, hPen);
//SelectBrush(hMemDC, hBrush1);
Base *p1 = new tEllipse;
p1->draw(hMemDC, R, xr, yr);// доступ к draw класса tEllipse
Base *p2 = new tCircle;
p2->draw(hMemDC, r, x, y);// доступ к draw класса tCircle
delete p1, p2;
BitBlt(hdc, 0, 0, window_width, window_height, hMemDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, oldBmp);
DeleteObject(hScreen);
DeleteDC(hMemDC);
EndPaint(hwnd, &ps);
}
break;
case WM_TIMER:
{
fi = x3 * 3.14 / 180;
x = (R/2*cos(fi)) + xr;
y = (R*sin(fi)) + yr;
x3 = x3 + h1;
InvalidateRect(hwnd, NULL, FALSE);
}
break;
case WM_DESTROY:
{
DeleteBrush(hWindowBrush);
KillTimer(hwnd, 1);
DeletePen(hPen);
DeleteBrush(hBrush1);
DeleteBrush(hBrush2);
PostQuitMessage(0);
}
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
char Class[] = "Animation_Class";
WNDCLASS wc = { 0 };
ZeroMemory(&wc, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProcedure;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = 0;
wc.lpszClassName = Class;
wc.lpszMenuName = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
if (!RegisterClass(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_OK | MB_ICONERROR);
return 0;
}
HWND hwnd = CreateWindow(Class, "Animation", WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, window_width, window_height, NULL, NULL, hInstance, NULL);
if (!hwnd)
{
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_OK | MB_ICONERROR);
return 0;
}
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
TranslateMessage(&msg);
}
UnregisterClass(Class, hInstance);
return (int)msg.wParam;
} |