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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "MyWindow.h"

using namespace MyWindow;

/*-----------------------------------------------------------
Function:	ctor

-------------------------------------------------------------*/
Form::Form(HINSTANCE instance)
{		
	this->name = L"Window";
	this->caption = L"Window caption";
	
	this->width = 800;
	this->height = 600;
	
	this->positionX = GetSystemMetrics(SM_CXSCREEN) / 2;
	this->positionX -=(this->width / 2);

	this->positionY = GetSystemMetrics(SM_CYSCREEN) / 2;
	this->positionY -= (this->height / 2);

	this->fullscreen = false;
	
	this->style = WS_OVERLAPPED|WS_SYSMENU|WS_VISIBLE;
	
	this->instance = instance;

	if (!this->InitWnd(instance))
	{
		throw std::exception("Can not initialize window");
	}

}

/*-----------------------------------------------------------
Function:	dtor

-------------------------------------------------------------*/
Form::~Form()
{
	this->DestroyWnd();
}

/*-----------------------------------------------------------
Function:	protected DestroyWnd()

Destroy window and clean up
-------------------------------------------------------------*/
bool Form::DestroyWnd()
{
	//Test if our window is valid
	if((this->mainWindowHandle))
	{
		if(!DestroyWindow((this->mainWindowHandle)))
		{
			//We failed to destroy our window, this shouldn't ever happen 
			return false;
		}
		else
		{
			MSG msg;
			//Clean up any pending messages
			while(PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
   }

	(this->mainWindowHandle)=NULL;

	UnregisterClass(this->name.c_str(), this->instance);

	return true;
}

/*-----------------------------------------------------------
Function:	protected virtual InitWnd()

Initialize window class (WNDCLASS)
-------------------------------------------------------------*/
bool Form::InitWnd(HINSTANCE instance)
{
	WNDCLASS window_class;
	window_class.style          = CS_OWNDC;
	window_class.cbClsExtra     = 0;
	window_class.cbWndExtra     = 0;
	window_class.hInstance      = instance;
	window_class.hIcon          = LoadIcon(NULL,IDI_APPLICATION);
	window_class.hCursor        = LoadCursor(NULL,IDC_ARROW);
	window_class.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
	window_class.lpszMenuName   = NULL;
	window_class.lpszClassName  = this->name.c_str();
	window_class.lpfnWndProc    = &Form::MainWndProc;     //An external function to handle window messages


	if (!RegisterClass(&window_class))
	{
		MessageBox(0, L"Register class failed", 0, 0);
		return false;
	}
	

	return true;
}

/*-----------------------------------------------------------
Function:	protected static MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

Main window procedure
-------------------------------------------------------------*/
LRESULT CALLBACK Form::MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static Form* me = 0;

	switch ( msg )
	{
		case WM_CREATE:
		{
			CREATESTRUCT* createStruct = (CREATESTRUCT*)lParam;
			me = (Form*)createStruct->lpCreateParams;
			return 0;
		}
	}

	if ( me )
	{		
		return me->WndProc(hWnd, msg, wParam, lParam);
	}
	else
	{
		return DefWindowProc(hWnd, msg, wParam, lParam);
	}

}


/*-----------------------------------------------------------
Function:	protected virtual WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

Window procedure called from MainWindowProc
-------------------------------------------------------------*/
LRESULT CALLBACK Form::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

	switch ( msg )
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
		case WM_KEYDOWN:		// Is A Key Being Held Down?
		{
			if (wParam == VK_ESCAPE)
			{
				PostQuitMessage(0);
				return 0;
			}
			//keys[wParam] = TRUE; // If So, Mark It As TRUE
			//return 0; // Jump Back
		}
	}


	this->UserKeyCallbacks(msg, wParam, lParam);

	return DefWindowProc(hWnd, msg, wParam, lParam);

}


/*-----------------------------------------------------------
Function:	public Run()

Run form - call CreateWindow and main loop while incomming
message != WM_QUIT calls UserRunCode()
-------------------------------------------------------------*/
int Form::Run(int cmd)
{		
	this->mainWindowHandle = CreateWindow(this->name.c_str(), //name of registered class
									this->caption.c_str(),	//caption
									this->style,	//style flags
									this->positionX,	//x pos
									this->positionY, //y pos
									this->width,
									this->height,
									NULL, //parent window
									NULL, //menu
									this->instance,
									this); //pointer to window creation data

	if (this->mainWindowHandle == 0)
	{
		MessageBox(0, L"Create window failed", 0, 0);
		return false;
	}

	this->UpdateSettings();

	ShowWindow(this->mainWindowHandle, cmd);
	UpdateWindow(this->mainWindowHandle);
	
	MSG msg = {0};

	//MessageBox(0, L"DDDDDD", 0, 0);
	
	
	while ( msg.message != WM_QUIT )
	{
		if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) //nebo GetMesaage
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			//co se ma vykonavat
			this->UserRunCode();
		}

	}
	
	//MessageBox(0, L"EXIT", 0, 0);
	return (int)msg.wParam;
}

/*-----------------------------------------------------------
Function:	protected UpdateSettings()

Update window size, caption etc.
-------------------------------------------------------------*/
void Form::UpdateSettings()
{
	SetWindowText(this->mainWindowHandle, this->caption.c_str());
	SetWindowLongPtr(this->mainWindowHandle, GWL_STYLE, this->style);
	
	SetWindowPos(this->mainWindowHandle, 
			HWND_TOP, 
			0, 
			0,
            0,    //New Width
            0, //New Height, 
			SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);

	
InvalidateRect(this->mainWindowHandle, NULL, TRUE);


	

	if (!this->fullscreen)
	{
		/*
		MoveWindow(this->mainWindowHandle,
              this->positionX, 
			  this->positionY,
              this->width,    //New Width
              this->height, //New Height
              TRUE);
		*/
		SetWindowPos(this->mainWindowHandle, 
			HWND_TOP, 
			this->positionX, 
			this->positionY,
            this->width,    //New Width
            this->height, //New Height, 
			SWP_SHOWWINDOW);

		ShowCursor(TRUE);
		
	}
	else 
	{	
		
		SetWindowPos(this->mainWindowHandle, 
			HWND_TOP, 
			50, 
			50, 
			GetSystemMetrics(SM_CXSCREEN),
			GetSystemMetrics(SM_CYSCREEN), 
			SWP_SHOWWINDOW);

		ShowCursor(FALSE);
	}
}



/*-----------------------------------------------------------
Function:	public SetCaption(std::string caption)

Set new form caption
-------------------------------------------------------------*/
void Form::SetCaption(std::string caption)
{
	this->caption = this->StringToWString(caption);
	this->UpdateSettings();
}



/*-----------------------------------------------------------
Function:	public SetWidth(int width)

Set new form width
-------------------------------------------------------------*/
void Form::SetWidth(int width)
{
	this->width = width;
	this->UpdateSettings();
}


/*-----------------------------------------------------------
Function:	public SetHeight(int height)

Set new form height
-------------------------------------------------------------*/
void Form::SetHeight(int height)
{
	this->height = height;
	this->UpdateSettings();
}


/*-----------------------------------------------------------
Function:	public SetFullScreen(bool fullscreen)

Set fullscreen
-------------------------------------------------------------*/
void Form::SetFullScreen(bool fullscreen)
{
	this->fullscreen = fullscreen;
	if (this->fullscreen)
	{
		this->style = WS_POPUP|WS_VISIBLE;
	}
	else 
	{
		this->style = WS_OVERLAPPED|WS_SYSMENU|WS_VISIBLE;
	}
	this->UpdateSettings();
}
/*-----------------------------------------------------------
Function:	protected StringToWString(const std::string &input)

Convert std::string to std::wstring
-------------------------------------------------------------*/
std::wstring Form::StringToWString(const std::string &input){
	int len;
	int slength = (int)input.length() + 1;
	len = MultiByteToWideChar(CP_ACP, 0, input.c_str(), slength, 0, 0);
	wchar_t* buf = new wchar_t[len];
	MultiByteToWideChar(CP_ACP, 0, input.c_str(), slength, buf, len);
	std::wstring output(buf);
	//delete[] buf;
	//free(buf);
	return output;
}