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
|
-BaseWindow.cpp
//#include "EngineCommonStd.h" //Custom Precompiled Header
#include "BaseWindow.h"
//#include "WindowController.h"
//===============================================
#pragma region Private Definitions
LRESULT CALLBACK BaseWindow::s_MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
BaseWindow *self;
if (msg == WM_NCCREATE)
{
LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
self = reinterpret_cast<BaseWindow*>(lpcs->lpCreateParams);
self->m_hwnd = hwnd;
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(self));
}
else
{
self = reinterpret_cast<BaseWindow*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
}
if (self)
{
return self->HandleMessage(msg, wParam, lParam);
}
else
{
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}
#pragma endregion
#pragma region Protected Definitions
void BaseWindow::Register()
{
WNDCLASSEX wcx;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = 0;
wcx.lpfnWndProc = BaseWindow::s_MsgProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = m_appInstance;
wcx.hIcon = NULL;
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcx.lpszMenuName = NULL;
wcx.lpszClassName = ClassName();
wcx.hIconSm = NULL;
if (!WinRegisterClass(&wcx))
{
DWORD error = GetLastError();
if (error == ERROR_CLASS_ALREADY_EXISTS)
{
ENGINE_WARNING(L"Class already exists\n"
L"\tClass: " + std::wstring(ClassName()) + L" ");
}
else
{
ENGINE_ERROR(L"Failed to register window: " + m_caption + L"\n" +
L"<Unknown Reason> \n" +
L"Class: " + ClassName() + L"\n" +
L"Closing Application.");
PostQuitMessage(0);
}
}
}
void BaseWindow::WinCreateWindowEx(DWORD exStyle, LPCWSTR caption, DWORD style, int x, int y, int w, int h, HWND hwndParent, HMENU menu)
{
m_caption = caption;
m_position = { x, y };
m_size = { w, h };
RECT R = { 0, 0, w, h };
AdjustWindowRectEx(&R, style, (menu ? true : false), exStyle);
m_hwnd = CreateWindowEx(exStyle, ClassName(),
caption,
style,
x, y,
R.right, R.bottom,
hwndParent,
menu,
m_appInstance,
this);
if (!m_hwnd)
{
DWORD error = GetLastError();
ENGINE_ERROR(L"Failed to create window: " + m_caption + L"\n" +
L"Class: " + ClassName() + L"\n" +
L"Closing Application.");
PostQuitMessage(0);
}
}
LRESULT BaseWindow::HandleMessage(UINT msg, WPARAM wParam, LPARAM lParam)
{
LRESULT lres;
switch (msg)
{
case WM_NCDESTROY:
lres = DefWindowProc(m_hwnd, msg, wParam, lParam);
SetWindowLongPtr(m_hwnd, GWLP_USERDATA, 0);
return lres;
case WM_DESTROY:
if (m_isMainWindow) PostQuitMessage(0);
return 0;
}
return DefWindowProc(m_hwnd, msg, wParam, lParam);
}
#pragma endregion
//===============================================
#pragma region Public API
//. . .
void BaseWindow::Show(BOOL forceForeground /* = false */)
{
ShowWindow(m_hwnd, SW_SHOW);
if (forceForeground)
SetForegroundWindow(m_hwnd);
UpdateWindow(m_hwnd);
}
//. . .
#pragma endregion
//===============================================
|