-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderWindow.cpp
More file actions
144 lines (122 loc) · 3.76 KB
/
RenderWindow.cpp
File metadata and controls
144 lines (122 loc) · 3.76 KB
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
#include "WindowContainer.h"
// Forward declare message handler from imgui_impl_win32.cpp
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (ImGui_ImplWin32_WndProcHandler(hWnd, message, wParam, lParam))
return true;
switch (message)
{
case WM_NCCREATE:
{
OutputDebugStringA("[+] The window was created!");
return DefWindowProc(hWnd, message, wParam, lParam);
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
bool RenderWindow::CreateWnd(HINSTANCE hInstance, std::string title, std::string wclass, int width, int height)
{
_hInstance = hInstance;
_width = width;
_height = height;
_sWindowTitle = title;
_wWindowTitle = StringConverter::StringToWide(title);
_sWindowClass = wclass;
_wWindowClass = StringConverter::StringToWide(wclass);
this->RegisterWindowClass();
// this->GetDesktopResolution(_width, _height);
int centerScreenX = GetSystemMetrics(SM_CXSCREEN) / 2 - _width / 2;
int centerScreenY = GetSystemMetrics(SM_CYSCREEN) / 2 - _height / 2;
// Using AdjustWindowRect to set an accurate size of the drawing area
RECT wr;
wr.left = centerScreenX;
wr.top = centerScreenY;
wr.right = wr.left + _width;
wr.bottom = wr.top + _height;
AdjustWindowRect(&wr, WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, FALSE);
// Create the window and use the result as the handle
_hWnd = CreateWindowEx(NULL,
_wWindowClass.c_str(),
_wWindowTitle.c_str(),
WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, // window flags
wr.left, // the starting x position
wr.top, // the starting y positions
wr.right - wr.left, // width of window
wr.bottom - wr.top, // height of window
NULL,
NULL,
hInstance,
NULL);
if (_hWnd == NULL)
{
ErrorLogger::Log(GetLastError(), "CreateWindowEx Failed for window " + _sWindowTitle);
return false;
}
// Show the window and set focus
ShowWindow(_hWnd, SW_SHOW);
SetForegroundWindow(_hWnd);
SetFocus(_hWnd);
return true;
}
RenderWindow::~RenderWindow()
{
if (_hWnd != NULL)
{
UnregisterClass(_wWindowClass.c_str(), _hInstance);
DestroyWindow(_hWnd);
}
}
void RenderWindow::RegisterWindowClass(void)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = _hInstance;
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = _wWindowClass.c_str();
wc.cbSize = sizeof(WNDCLASSEX);
RegisterClassEx(&wc);
}
bool RenderWindow::ProcessMessages()
{
// Handle windows messages
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while (PeekMessage(&msg, _hWnd, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Check if window was closed
if (msg.message == WM_NULL)
{
if (!IsWindow(_hWnd))
{
_hWnd = NULL; // message processing loop takes care of destroying this window
UnregisterClass(_wWindowClass.c_str(), _hInstance);
return false;
}
}
return true;
}
HWND RenderWindow::GetHWND() const
{
return _hWnd;
}
void RenderWindow::GetDesktopResolution(int& horizontal, int& vertical)
{
RECT desktop;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect(hDesktop, &desktop);
horizontal = desktop.right;
vertical = desktop.bottom;
}