-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin.cpp
275 lines (245 loc) · 6.78 KB
/
win.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* Sergeev Artemiy, 33601/2 (3057/2) */
#include "win.h"
#include "winmsgx.h"
/* Class constructor */
notepad::win::win( HINSTANCE hInst, int cmdShow ) : isInit(FALSE)
{
WNDCLASS wc;
/* Store application instance handle */
if (hInst == NULL)
hInst = GetModuleHandle(NULL);
hInstance = hInst;
/* Fill and register window class */
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(void *);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL, (char *)IDC_ARROW);
wc.hIcon = LoadIcon(NULL, (char *)IDI_APPLICATION);
wc.lpszMenuName = NULL;
wc.hInstance = hInstance;
wc.lpfnWndProc = WinFuncCallBack;
wc.lpszClassName = c_MainWndClassName;
if (!RegisterClass(&wc))
{
MessageBox(NULL, "Error register window class", "ERROR",
MB_OK | MB_ICONSTOP);
return;
}
/* Window creation */
hWnd = CreateWindow(c_MainWndClassName,
"Notepad",
WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, (void *)this);
if (hWnd == NULL)
{
MessageBox(NULL, "Error create window", "ERROR",
MB_OK | MB_ICONSTOP);
exit(0);
}
/* Show and update window */
ShowWindow(hWnd, cmdShow);
UpdateWindow(hWnd);
/* Send initialization timer message */
PostMessage(hWnd, WM_TIMER, (WPARAM)c_initializationTimer, 0);
}
/* Windowed application running function */
int notepad::win::Run( void )
{
MSG msg;
/* Message loop */
while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
if (isInit)
Idle();
}
return msg.wParam;
}
/* Class destructor */
notepad::win::~win( void )
{
}
/* System exit function */
void notepad::win::DoExit( void )
{
PostMessage(hWnd, WM_CLOSE, 0, 0);
}
/* The set full screen application mode */
void notepad::win::SetFullScreen( bool IsFull )
{
RECT rc;
DWORD Style;
if (IsFull == isFullScreen)
return;
if ((isFullScreen = IsFull) != FALSE)
{
GetWindowRect(hWnd, &saveRC);
rc.left = 0;
rc.top = 0;
rc.right = GetSystemMetrics(SM_CXSCREEN);
rc.bottom = GetSystemMetrics(SM_CYSCREEN);
Style = GetWindowLong(hWnd, GWL_STYLE);
AdjustWindowRect(&rc, Style, FALSE);
SetWindowPos(hWnd, HWND_TOP, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
SWP_NOOWNERZORDER | SWP_NOREDRAW | SWP_NOSENDCHANGING);
}
else
{
SetWindowPos(hWnd, HWND_TOP, saveRC.left, saveRC.top, saveRC.right - saveRC.left, saveRC.bottom - saveRC.top,
SWP_NOOWNERZORDER | SWP_NOSENDCHANGING);
}
}
/* Window message handle function (CALLBACK version) */
LRESULT CALLBACK notepad::win::WinFuncCallBack( HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam )
{
win *Win;
switch (msg)
{
case WM_CREATE:
{
/* Attach 'this' pointer to window class to window */
notepad::win *myThis = (notepad::win *)((CREATESTRUCT *)lParam)->lpCreateParams;
SetWindowLong(hWnd, 0, (LONG)myThis);
myThis->OnCreate(hWnd, (CREATESTRUCT *)lParam);
}
default:
Win = (win *)GetWindowLong(hWnd, 0);
if (Win != NULL)
return Win->WinFunc(msg, wParam, lParam);
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
/* Window handle message (with crack message calls) */
LRESULT notepad::win::WinFunc( unsigned int msg, WPARAM wParam, LPARAM lParam )
{
switch (msg)
{
/* Run specialized macros to convert 'wParam' and 'lParam' for user message process functions */
HANDLE_MSG(WM_SIZE, OnSize);
HANDLE_MSG(WM_ACTIVATE, OnActivate);
HANDLE_MSG(WM_ERASEBKGND, OnEraseBkgnd);
HANDLE_MSG(WM_PAINT, OnPaint);
HANDLE_MSG(WM_TIMER, OnTimer);
HANDLE_MSG(WM_DESTROY, OnClose);
HANDLE_MSG(WM_CLOSE, OnClose);
HANDLE_MSG(WM_KEYDOWN, Key);
HANDLE_MSG(WM_KEYUP, Key);
HANDLE_MSG(WM_MOUSEMOVE, MouseMove);
HANDLE_MSG(WM_MOUSEWHEEL, MouseWheel);
HANDLE_MSG(WM_COMMAND, Command);
HANDLE_MSG(WM_VSCROLL, VScroll);
HANDLE_MSG(WM_HSCROLL, HScroll);
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
/* WM_CREATE window message handle function */
bool notepad::win::OnCreate( HWND hWnd, CREATESTRUCT *createStruct )
{
HDC hDC;
/* Create second frame buffer */
hDC = ::GetDC(hWnd);
this->hDC = CreateCompatibleDC(hDC);
hBm = CreateCompatibleBitmap(hDC, 10, 10);
SelectObject(this->hDC, hBm);
ReleaseDC(hWnd, hDC);
SetTimer(hWnd, 1110111, 5, NULL);
isFullScreen = FALSE;
return TRUE;
}
/* WM_DESTROY window message handle function */
void notepad::win::OnClose( void )
{
KillTimer(hWnd, 1110111);
ReleaseDC(hWnd, hDC);
DeleteObject(hBm);
/* Call user level deinitialization */
if (isInit)
{
isInit = FALSE;
Close();
}
PostQuitMessage(0);
}
/* WM_SIZE window message handle function */
void notepad::win::OnSize( unsigned int state, int newWidth, int newHeight )
{
width = newWidth;
height = newHeight;
HDC hDC;
/* Delete old second frame buffer */
DeleteObject(hBm);
/* Create new frame buffer */
hDC = ::GetDC(hWnd);
hBm = CreateCompatibleBitmap(hDC, width, height);
ReleaseDC(hWnd, hDC);
SelectObject(this->hDC, hBm);
/* Call user level change size notifivcation */
if (isInit)
Resize();
InvalidateRect(hWnd, NULL, FALSE);
}
/* WM_ERASEBKGND window message handle function */
bool notepad::win::OnEraseBkgnd( HDC hDC )
{
/* Call user level erase background function */
if (isInit)
Erase();
return FALSE;
}
/* WM_PAINT window message handle function */
void notepad::win::OnPaint( void )
{
HDC hDC;
PAINTSTRUCT ps;
HPEN hOldPen = (HPEN)SelectObject(this->hDC, GetStockObject(NULL_PEN));
HBRUSH hOldBrush = (HBRUSH)SelectObject(this->hDC, GetStockObject(WHITE_BRUSH));
hDC = BeginPaint(hWnd, &ps);
/* Call user level paint window content function */
if (isInit)
{
/* Default action - draw white rectangle */
Rectangle(this->hDC, 0, 0, width + 1, height + 1);
SelectObject(this->hDC, hOldPen);
SelectObject(this->hDC, hOldBrush);
Paint();
}
BitBlt(hDC, 0, 0, width, height, this->hDC, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
}
/* WM_ACTIVATE window message handle function */
void notepad::win::OnActivate( unsigned int reason, HWND hWndActDeact, bool isMinimized )
{
isActive = reason == WA_CLICKACTIVE || reason == WA_ACTIVE;
/* Call user level activate handle function */
if (isInit)
Activate(reason != 0);
}
/* WM_TIMER window message handle function */
void notepad::win::OnTimer( int id )
{
if (id == c_initializationTimer)
{
isInit = TRUE;
Init();
Resize();
InvalidateRect(hWnd, NULL, TRUE);
if (isActive)
Activate(TRUE);
}
else
/* Call user level timer handle function */
if (isInit)
{
Timer(id);
InvalidateRect(hWnd, NULL, FALSE);
}
}