32 std::expected<std::unique_ptr<Window>, std::string>
Window::Create(std::wstring_view title, uint32_t clientWidth,
33 uint32_t clientHeight) {
35 SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
37 return std::unexpected(
"窓のクラスを登録できない: " +
DescribeHresult(HRESULT_FROM_WIN32(GetLastError())));
41 .left = 0, .top = 0, .right =
static_cast<LONG
>(clientWidth), .bottom =
static_cast<LONG
>(clientHeight)};
42 AdjustWindowRectEx(&rect, WINDOW_STYLE, FALSE, 0);
44 std::unique_ptr<Window> window(
new Window());
45 const std::wstring titleText(title);
47 const HWND handle = CreateWindowExW(0, WINDOW_CLASS_NAME, titleText.c_str(), WINDOW_STYLE, CW_USEDEFAULT,
48 CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top,
nullptr,
49 nullptr, GetModuleHandleW(
nullptr), window.get());
51 if (handle ==
nullptr)
52 return std::unexpected(
"窓を作れない: " +
DescribeHresult(HRESULT_FROM_WIN32(GetLastError())));
54 ShowWindow(handle, SW_SHOWNORMAL);
127 if (message == WM_NCCREATE) {
129 const auto* create =
reinterpret_cast<const CREATESTRUCTW*
>(lParam);
130 auto* window =
static_cast<Window*
>(create->lpCreateParams);
131 window->m_handle = handle;
132 SetWindowLongPtrW(handle, GWLP_USERDATA,
reinterpret_cast<LONG_PTR
>(window));
136 auto* window =
reinterpret_cast<Window*
>(GetWindowLongPtrW(handle, GWLP_USERDATA));
137 if (window ==
nullptr)
138 return DefWindowProcW(handle, message, wParam, lParam);
140 return window->HandleMessage(message, wParam, lParam);
161 case WM_MOUSEWHEEL:
AddWheel(wParam);
return 0;
164 .key =
static_cast<uint32_t
>(wParam),
165 .shift = (GetKeyState(VK_SHIFT) & 0x8000) != 0});
169 case WM_DPICHANGED: {
172 const auto* suggested =
reinterpret_cast<const RECT*
>(lParam);
173 SetWindowPos(
m_handle,
nullptr, suggested->left, suggested->top, suggested->right - suggested->left,
174 suggested->bottom - suggested->top, SWP_NOZORDER | SWP_NOACTIVATE);
183 SetWindowLongPtrW(handle, GWLP_USERDATA, 0);
189 return DefWindowProcW(handle, message, wParam, lParam);