Skip to content

Commit

Permalink
Win32 backends: Center window on screen when opening it
Browse files Browse the repository at this point in the history
  • Loading branch information
mikke89 committed Jul 7, 2024
1 parent be9a497 commit 284574d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions Backends/RmlUi_Backend_Win32_GL2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,12 @@ static HWND InitializeWindow(HINSTANCE instance_handle, const std::wstring& name
SetWindowLong(window_handle, GWL_EXSTYLE, extended_style);
SetWindowLong(window_handle, GWL_STYLE, style);

// Resize the window.
SetWindowPos(window_handle, HWND_TOP, 0, 0, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top, SWP_NOACTIVATE);
// Resize the window and center it on the screen.
Rml::Vector2i screen_size = {GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)};
Rml::Vector2i window_size = {int(window_rect.right - window_rect.left), int(window_rect.bottom - window_rect.top)};
Rml::Vector2i window_pos = Rml::Math::Max((screen_size - window_size) / 2, Rml::Vector2i(0));

SetWindowPos(window_handle, HWND_TOP, window_pos.x, window_pos.y, window_size.x, window_size.y, SWP_NOACTIVATE);

return window_handle;
}
Expand Down
8 changes: 6 additions & 2 deletions Backends/RmlUi_Backend_Win32_VK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,12 @@ static HWND InitializeWindow(HINSTANCE instance_handle, const std::wstring& name
SetWindowLong(window_handle, GWL_EXSTYLE, extended_style);
SetWindowLong(window_handle, GWL_STYLE, style);

// Resize the window.
SetWindowPos(window_handle, HWND_TOP, 0, 0, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top, SWP_NOACTIVATE);
// Resize the window and center it on the screen.
Rml::Vector2i screen_size = {GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)};
Rml::Vector2i window_size = {int(window_rect.right - window_rect.left), int(window_rect.bottom - window_rect.top)};
Rml::Vector2i window_pos = Rml::Math::Max((screen_size - window_size) / 2, Rml::Vector2i(0));

SetWindowPos(window_handle, HWND_TOP, window_pos.x, window_pos.y, window_size.x, window_size.y, SWP_NOACTIVATE);

return window_handle;
}
Expand Down

0 comments on commit 284574d

Please sign in to comment.