-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
243 lines (215 loc) · 7.41 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <windows.h>
#include "window/Window.h"
#include <cmath>
#include "rect/Rect.h"
#include "const.h"
RECT windowSpace;
Rect rect;
byte selected_mode = FIRST_MODE_VAL;
PAINTSTRUCT ps;
HDC memDC;
HBITMAP bmp, memBmp, oldBitmap;
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void create_mode_combobox(HWND &hwnd);
void create_obj_combobox(HWND &hwnd);
void handle_mousewheel(WPARAM &wParam);
void handle_keydown(WPARAM &wparam, HWND &hwnd);
void handle_auto_mode(HWND &hwnd);
void check_zone_validation(HWND &hwnd);
void load_image_bmp(HWND &hwnd);
void draw_image(HDC &hdc);
void paint_image(HWND &hwnd);
int calc_offset(byte sign, int coord, int delta);
void handle_wm_command(HWND &hwnd, WPARAM wParam, LPARAM lParam);
void track_coord();
int WinMain(HINSTANCE hinstance, HINSTANCE, LPTSTR, int mode) {
Window window = Window();
window.reg_window(hinstance, "Window", WndProc);
window.create_window(hinstance);
window.show_window();
while (GetMessage(&window.msg, nullptr, 0, 0)) {
TranslateMessage(&window.msg);
DispatchMessage(&window.msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
rect = Rect(RECT_START_X, RECT_START_Y, RECT_HEIGHT, RECT_WIDTH, RECT_VX, RECT_VY);
create_mode_combobox(hwnd);
create_obj_combobox(hwnd);
SetTimer(hwnd, 1, 25, nullptr);
break;
case WM_KEYDOWN:
handle_keydown(wParam, hwnd);
break;
case WM_MOUSEWHEEL:
if (selected_mode == FIRST_MODE_VAL) {
handle_mousewheel(wParam);
}
break;
case WM_MOUSEMOVE:
if (selected_mode == SECOND_MODE_VAL) {
rect.x = LOWORD(lParam);
rect.y = HIWORD(lParam);
}
break;
case WM_TIMER: {
InvalidateRect(hwnd, &rect.rect, true);
if (selected_mode == THIRD_MODE_VAL) {
handle_auto_mode(hwnd);
}
track_coord();
InvalidateRect(hwnd, &rect.rect, false);
}
break;
case WM_COMMAND:
handle_wm_command(hwnd,wParam,lParam);
break;
case WM_PAINT:
if (!bmp) {
rect.paint_rect(hwnd);
} else {
paint_image(hwnd);
}
UpdateWindow(hwnd);
break;
case WM_DESTROY:
KillTimer(hwnd, 1);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
void create_mode_combobox(HWND &hwnd) {
HWND mode_combo_box = CreateWindow("combobox", "", WS_VISIBLE | WS_CHILD | CBS_DROPDOWNLIST, 0, 0, CB_MODE_WIDTH,
CB_MODE_HEIGHT, hwnd, (HMENU) CB_MODE_ID, nullptr, nullptr);
SendMessageA(mode_combo_box, CB_ADDSTRING, 0, (LPARAM) FIRST_MODE);
SendMessageA(mode_combo_box, CB_ADDSTRING, 0, (LPARAM) SECOND_MODE);
SendMessageA(mode_combo_box, CB_ADDSTRING, 0, (LPARAM) THIRD_MODE);
SendMessage(mode_combo_box, CB_SETCURSEL, 0, 0);
}
void create_obj_combobox(HWND &hwnd) {
HWND obj_combo_box = CreateWindow("combobox", "", WS_VISIBLE | WS_CHILD | CBS_DROPDOWNLIST, CB_MODE_WIDTH, 0,
CB_OBJ_WIDTH,
CB_OBJ_HEIGHT, hwnd, (HMENU) CB_OBJ_ID, nullptr, nullptr);
SendMessageA(obj_combo_box, CB_ADDSTRING, 0, (LPARAM) FIRST_OBJ);
SendMessageA(obj_combo_box, CB_ADDSTRING, 0, (LPARAM) SECOND_OBJ);
SendMessage(obj_combo_box, CB_SETCURSEL, 0, 0);
}
void handle_wm_command(HWND &hwnd, WPARAM wParam, LPARAM lParam){
switch (LOWORD(wParam)) {
case CB_MODE_ID:
if (HIWORD(wParam) == CBN_SELCHANGE) {
selected_mode = SendMessage((HWND) lParam, CB_GETCURSEL, 0, 0);
}
break;
case CB_OBJ_ID:
if (HIWORD(wParam) == CBN_SELCHANGE) {
if (!bmp) {
load_image_bmp(hwnd);
} else {
bmp = nullptr;
}
}
break;
}
}
void track_coord() {
rect.rect.left = rect.x - rect.width;
rect.rect.top = rect.y - rect.height;
rect.rect.right = rect.x + rect.width;
rect.rect.bottom = rect.y + rect.height;
}
void handle_auto_mode(HWND &hwnd) {
check_zone_validation(hwnd);
rect.x += (int) rect.vx;
rect.y += (int) rect.vy;
UpdateWindow(hwnd);
}
void check_zone_validation(HWND &hwnd) {
GetClientRect(hwnd, &windowSpace);
if (rect.x >= windowSpace.right - rect.width) rect.vx = -std::abs(rect.vx);
if (rect.y > windowSpace.bottom - rect.height) rect.vy = -std::abs(rect.vy);
if (rect.x < 0) rect.vx = std::abs(rect.vx);
if (rect.y < 0) rect.vy = std::abs(rect.vy);
}
void handle_keydown(WPARAM &wparam, HWND &hwnd) {
switch (wparam) {
case VK_LEFT:
case 0x41:
rect.x -= (int) (rect.vx * RECT_DELTA_SPEED);
break;
case VK_RIGHT:
case 0x44:
rect.x += (int) (rect.vx * RECT_DELTA_SPEED);
break;
case VK_UP:
case 0x57:
rect.y -= (int) (rect.vy * RECT_DELTA_SPEED);
break;
case VK_DOWN:
case 0x53:
rect.y += (int) (rect.vy * RECT_DELTA_SPEED);
break;
default:
break;
}
}
void handle_mousewheel(WPARAM &wParam) {
byte sign = GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? 1 : -1;
int delta = GET_WHEEL_DELTA_WPARAM(wParam) / 10;
if (LOWORD(wParam) == MK_SHIFT) {
rect.x = calc_offset(sign, rect.x, delta);
} else {
rect.y = calc_offset(sign, rect.y, delta);
}
}
int calc_offset(byte sign, int coord, int delta) {
return sign > 0 ? coord -= delta : coord += delta;
}
void load_image_bmp(HWND &hwnd) {
if ((bmp = (HBITMAP) LoadImage(nullptr,
PATH_IMAGE, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)) == nullptr) {
MessageBox(nullptr, "Error", "Error to loading image!", MB_OK);
} else {
BITMAP bitmap;
GetObject(bmp, sizeof(bitmap), &bitmap);
UpdateWindow(hwnd);
CoUninitialize();
}
}
void paint_image(HWND &hwnd) {
GetClientRect(hwnd, &windowSpace);
HDC hdc;
hdc = BeginPaint(hwnd, &ps);
memDC = CreateCompatibleDC(hdc);
memBmp = CreateCompatibleBitmap(hdc, windowSpace.right - windowSpace.left,
windowSpace.bottom - windowSpace.top);
oldBitmap = (HBITMAP) SelectObject(memDC, memBmp);
FillRect(memDC, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 1));
draw_image(memDC);
BitBlt(hdc, windowSpace.left, windowSpace.top, windowSpace.right - windowSpace.left,
windowSpace.bottom - windowSpace.top, memDC,
windowSpace.left,
windowSpace.top, SRCCOPY);
DeleteDC(memDC);
DeleteObject(memBmp);
DeleteObject(oldBitmap);
EndPaint(hwnd, &ps);
}
void draw_image(HDC &hdc) {
HDC hdcMem;
BITMAP bitmap;
HGDIOBJ oldBitmap;
hdcMem = CreateCompatibleDC(hdc);
oldBitmap = SelectObject(hdcMem, bmp);
GetObject(bmp, sizeof(bitmap), &bitmap);
BitBlt(hdc, rect.x, rect.y, rect.width, rect.height, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, oldBitmap);
DeleteDC(hdcMem);
DeleteObject(oldBitmap);
}