-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
171 lines (157 loc) · 4.45 KB
/
MainWindow.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
#include "MainWindow.h"
#include "Application.h"
#include "GuiException.h"
#include "ScreenshotGrabberException.h"
#include <iostream>
using namespace PPSolver;
using namespace std;
HWND MainWindow::handle = NULL;
std::vector<Match> MainWindow::results;
MainWindow::MainWindow(HINSTANCE hInst, int nShowCmd)
{
// Create instance of window class
WNDCLASSEX wClass;
ZeroMemory(&wClass, sizeof(WNDCLASSEX));
wClass.cbClsExtra = 0;
wClass.cbSize = sizeof(WNDCLASSEX);
wClass.cbWndExtra = 0;
wClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
wClass.hCursor = LoadCursor(NULL,IDC_ARROW);
wClass.hIcon = NULL;
wClass.hIconSm = NULL;
wClass.hInstance = hInst;
wClass.lpfnWndProc = (WNDPROC)(MainWindow::WinProc);
wClass.lpszClassName = "OpaOpa window class";
wClass.lpszMenuName = NULL;
wClass.style = CS_HREDRAW|CS_VREDRAW;
// Register window class
if (!RegisterClassEx(&wClass)) {
int result = GetLastError();
throw GuiException("Window class creation failed");
}
// Create main window
MainWindow::handle = CreateWindowEx(
0,
"OpaOpa window class",
"OpaOpa - sophisticated cheat tool",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL,
NULL,
hInst,
NULL
);
if (!MainWindow::handle) {
int result = GetLastError();
throw GuiException("Failed to create main window");
}
ShowWindow(MainWindow::handle, nShowCmd);
}
// Handler of WM_PAINT message
// Paints results of solving on application's main window
void MainWindow::doPaint()
{
ScreenshotGrabber& scGrabber = Application::getInstance().getScreenshotGrabber();
HDC memoryDc = scGrabber.getMemoryDC();
if (memoryDc != NULL) {
PixelCellMatrix& pcm = Application::getInstance().getCellMatrix();
HDC myDc = GetDC(MainWindow::handle);
Coords& offset = scGrabber.getClientAreaBaseCoords();
for (auto i = MainWindow::results.begin(); i != MainWindow::results.end(); i++) {
BitBlt(
myDc,
MainWindow::BASEX + i->first.x * PixelCellMatrix::CELL_WIDTH,
MainWindow::BASEY + i->first.y * PixelCellMatrix::CELL_HEIGHT,
PixelCellMatrix::CELL_WIDTH,
PixelCellMatrix::CELL_HEIGHT,
memoryDc,
offset.x + pcm.getCellBaseX(i->first.x),
offset.y + pcm.getCellBaseY(i->first.y),
SRCCOPY
);
BitBlt(
myDc,
MainWindow::BASEX + i->second.x * PixelCellMatrix::CELL_WIDTH,
MainWindow::BASEY + i->second.y * PixelCellMatrix::CELL_HEIGHT,
PixelCellMatrix::CELL_WIDTH,
PixelCellMatrix::CELL_HEIGHT,
memoryDc,
offset.x + pcm.getCellBaseX(i->second.x),
offset.y + pcm.getCellBaseY(i->second.y),
SRCCOPY
);
}
ReleaseDC(MainWindow::handle, myDc);
}
}
LRESULT CALLBACK MainWindow::WinProc(HWND handle, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND hWndButton;
PAINTSTRUCT ps;
HDC hdc;
Application& app = Application::getInstance();
ScreenshotGrabber& scGrabber = app.getScreenshotGrabber();
HWND targetHandle;
PixelMatrix pm;
switch(msg) {
case WM_CREATE:
hWndButton = CreateWindowEx(
0,
"BUTTON",
"GO!",
WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
350,
10,
100,
24,
handle,
(HMENU)IDC_MAIN_BUTTON,
GetModuleHandle(NULL),
NULL
);
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDC_MAIN_BUTTON:
// Button click handler
targetHandle = scGrabber.findTargetWindow();
if (targetHandle == NULL) {
MessageBox(
handle,
"Can't find PaoPao window. Is it running?",
"Warning",
MB_ICONERROR
);
} else {
try {
pm = scGrabber.getScreenshot(targetHandle);
} catch (ScreenshotGrabberException e) {
MessageBox(
handle,
e.what(),
"Error",
MB_ICONERROR
);
break;
}
app.getCellMatrix().setPixelMatrix(pm);
MainWindow::results = app.getSolver().solve();
InvalidateRect(handle, NULL, TRUE);
}
break;
}
break;
case WM_PAINT:
hdc = BeginPaint(handle, &ps);
MainWindow::doPaint();
EndPaint(handle, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(handle, msg, wParam, lParam);
}