100 lines
2.4 KiB
C++
100 lines
2.4 KiB
C++
#include "Window.h"
|
|
|
|
INT Window::init(HINSTANCE hInstance, UINT width, UINT height)
|
|
{
|
|
// 1. describe window class
|
|
WNDCLASS wc = {};
|
|
wc.hInstance = hInstance; // application instance
|
|
wc.lpszClassName = TEXT("D3D11"); // name of window class
|
|
wc.lpfnWndProc = WindowProc; // message queue handler function
|
|
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // behaviour style and not visual style
|
|
wc.hbrBackground = CreateSolidBrush(RGB(255,0,0)); // set window background color
|
|
wc.hCursor = LoadCursor(nullptr, IDC_ARROW); // set window cursor
|
|
wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION); // set window icon
|
|
|
|
// 2. register window class
|
|
if (!RegisterClass(&wc)) return 10;
|
|
|
|
// 3. adjust window size (optional)
|
|
DWORD style = WS_POPUP;
|
|
RECT rect = { 100, 100, width + 100, height + 100 }; // left-top corner, right-bottom corner
|
|
AdjustWindowRect(&rect, style, FALSE);
|
|
|
|
// 4. create window instance
|
|
_hWnd = CreateWindow(
|
|
wc.lpszClassName, // name of window class
|
|
wc.lpszClassName, // window title
|
|
style, // visual window style
|
|
rect.left, rect.top, // window position
|
|
rect.right - rect.left, rect.bottom - rect.top, // window size
|
|
nullptr, // parent window handle
|
|
nullptr, // menu instance handle
|
|
hInstance, // application instance
|
|
nullptr // optional parameter
|
|
);
|
|
|
|
if (_hWnd == nullptr) return 12;
|
|
|
|
// 5. show window
|
|
ShowWindow(_hWnd, SW_SHOW);
|
|
SetFocus(_hWnd);
|
|
|
|
return 0;
|
|
}
|
|
|
|
BOOL Window::run()
|
|
{
|
|
// 6. run window
|
|
|
|
// message queue
|
|
// GetMessage - event based message method, return something if a new a message occur
|
|
// PeekMessage - returns a boolean, to signale if a message is available or not
|
|
// TranslateMessage - translate hardware keyboard codes (scan code) into virtual key table
|
|
// DispatchMessage - send message to WindowProc
|
|
// https://learn.microsoft.com/en-us/windows/win32/winmsg/messages-and-message-queues
|
|
|
|
MSG msg = {};
|
|
|
|
if (PeekMessage(&msg, nullptr, 0, UINT_MAX, PM_REMOVE))
|
|
{
|
|
TranslateMessage(&msg);
|
|
DispatchMessage(&msg);
|
|
}
|
|
|
|
Sleep(10);
|
|
|
|
return msg.message != WM_QUIT;
|
|
}
|
|
|
|
void Window::deInit()
|
|
{
|
|
}
|
|
|
|
LRESULT WindowProc(
|
|
HWND hWnd, // handle to window instance
|
|
UINT msg, // message id
|
|
WPARAM wParam, // main paramater
|
|
LPARAM lParam // additional parameters
|
|
)
|
|
{
|
|
switch (msg)
|
|
{
|
|
case WM_CLOSE:
|
|
DestroyWindow(hWnd);
|
|
break;
|
|
|
|
case WM_DESTROY:
|
|
PostQuitMessage(0);
|
|
break;
|
|
|
|
case WM_KEYDOWN:
|
|
if (wParam == VK_ESCAPE) DestroyWindow(hWnd);
|
|
break;
|
|
|
|
default:
|
|
return DefWindowProc(hWnd, msg, wParam, lParam);
|
|
}
|
|
|
|
return 0;
|
|
}
|