From 44a74509af96b7ff1a65eb84fda0c690ecc81fcf Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 16 Sep 2024 13:56:25 +0200 Subject: [PATCH] Backends: Win32: fixed direct calls to platform_io.Platform_SetWindowPos()/Platform_SetWindowSize() on windows created by application (typically main viewport). --- backends/imgui_impl_win32.cpp | 19 +++++++++++++++---- docs/CHANGELOG.txt | 2 ++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/backends/imgui_impl_win32.cpp b/backends/imgui_impl_win32.cpp index 816003509..1e9c58bbd 100644 --- a/backends/imgui_impl_win32.cpp +++ b/backends/imgui_impl_win32.cpp @@ -1158,11 +1158,20 @@ static ImVec2 ImGui_ImplWin32_GetWindowPos(ImGuiViewport* viewport) return ImVec2((float)pos.x, (float)pos.y); } +static void ImGui_ImplWin32_UpdateWin32StyleFromWindow(ImGuiViewport* viewport) +{ + ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; + vd->DwStyle = ::GetWindowLongW(vd->Hwnd, GWL_STYLE); + vd->DwExStyle = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE); +} + static void ImGui_ImplWin32_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos) { ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; IM_ASSERT(vd->Hwnd != 0); RECT rect = { (LONG)pos.x, (LONG)pos.y, (LONG)pos.x, (LONG)pos.y }; + if (viewport->Flags & ImGuiViewportFlags_OwnedByApp) + ImGui_ImplWin32_UpdateWin32StyleFromWindow(viewport); // Not our window, poll style before using ::AdjustWindowRectEx(&rect, vd->DwStyle, FALSE, vd->DwExStyle); ::SetWindowPos(vd->Hwnd, nullptr, rect.left, rect.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE); } @@ -1181,6 +1190,8 @@ static void ImGui_ImplWin32_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; IM_ASSERT(vd->Hwnd != 0); RECT rect = { 0, 0, (LONG)size.x, (LONG)size.y }; + if (viewport->Flags & ImGuiViewportFlags_OwnedByApp) + ImGui_ImplWin32_UpdateWin32StyleFromWindow(viewport); // Not our window, poll style before using ::AdjustWindowRectEx(&rect, vd->DwStyle, FALSE, vd->DwExStyle); // Client to Screen ::SetWindowPos(vd->Hwnd, nullptr, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE); } @@ -1227,14 +1238,14 @@ static void ImGui_ImplWin32_SetWindowAlpha(ImGuiViewport* viewport, float alpha) IM_ASSERT(alpha >= 0.0f && alpha <= 1.0f); if (alpha < 1.0f) { - DWORD style = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE) | WS_EX_LAYERED; - ::SetWindowLongW(vd->Hwnd, GWL_EXSTYLE, style); + DWORD ex_style = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE) | WS_EX_LAYERED; + ::SetWindowLongW(vd->Hwnd, GWL_EXSTYLE, ex_style); ::SetLayeredWindowAttributes(vd->Hwnd, 0, (BYTE)(255 * alpha), LWA_ALPHA); } else { - DWORD style = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED; - ::SetWindowLongW(vd->Hwnd, GWL_EXSTYLE, style); + DWORD ex_style = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED; + ::SetWindowLongW(vd->Hwnd, GWL_EXSTYLE, ex_style); } } diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 6c1343423..e0442dcdb 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -80,6 +80,8 @@ Docking+Viewports Branch: - Viewports: fixed an issue where a window manually constrained to the main viewport while crossing over main viewport bounds isn't translated properly. (#7985) +- Backends: Win32: fixed direct calls to platform_io.Platform_SetWindowPos()/Platform_SetWindowSize() + on windows created by application (typically main viewport). - Backends: SDL3: added support for viewport->ParentViewportId field to support parenting windows at OS level. (#7973) [@RT2code]