#include "Window.h" INT Window::init(HINSTANCE hInstance, UINT width, UINT height) { // 1. describe window class WNDCLASS wc = {}; wc.hInstance = hInstance; wc.lpszClassName = TEXT("D3D11"); // class name wc.lpfnWndProc = WindowProc; // global communication (message queue) function wc.hbrBackground = CreateSolidBrush(RGB(0, 255, 0)); // handle to background color wc.hCursor = LoadCursor(nullptr, IDC_ARROW); // handle to cursor icon wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION); // handle to application icon wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // technical behaviour // 2. register window class if (!RegisterClass(&wc)) return 100; // 3. adjust window size (optional) // 4. create window instance _hWnd = CreateWindow( wc.lpszClassName, // class name wc.lpszClassName, // window title WS_OVERLAPPEDWINDOW, // visual style 100, 100, // window position (left-top corner) width, height, // window size nullptr, // optional handle to a parent window nullptr, // optional handle to a menu instance hInstance, nullptr // pointer to optional parameters ); if (_hWnd == nullptr) return 110; // 5. show window ShowWindow(_hWnd, SW_SHOW); SetFocus(_hWnd); return 0; } BOOL Window::run() { // 6. run window // GetMessage - synchronous call to get the latest message, but holds the execution until there is a new message // PeekMessage - return also the latest message, but also returns false if there is no message available // TranslateMessage - translates keyboard input // DispatchMessage - sends message to window procedure MSG msg = {}; if (PeekMessage(&msg, nullptr, 0, UINT_MAX, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.message != WM_QUIT; } void Window::deInit() { } LRESULT WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CLOSE: DestroyWindow(hWnd); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; case WM_KEYDOWN: if (wParam == VK_ESCAPE) DestroyWindow(hWnd); return 0; default: break; } return DefWindowProc(hWnd, msg, wParam, lParam); }