From cdfa6ea2bfe1549d6a4b1d4c27e673fe07d6ce59 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Thu, 26 May 2022 17:20:53 +0400 Subject: [PATCH] Handle DPI change on Windows to avoid the window be resized by Qt --- ui/platform/win/ui_window_win.cpp | 23 ++++++++++++++++++++++- ui/platform/win/ui_window_win.h | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/ui/platform/win/ui_window_win.cpp b/ui/platform/win/ui_window_win.cpp index eea2221..5286e27 100644 --- a/ui/platform/win/ui_window_win.cpp +++ b/ui/platform/win/ui_window_win.cpp @@ -9,6 +9,7 @@ #include "ui/inactive_press.h" #include "ui/platform/win/ui_window_title_win.h" #include "ui/widgets/rp_window.h" +#include "base/platform/win/base_windows_safe_library.h" #include "base/platform/base_platform_info.h" #include "base/integration.h" #include "base/debug_log.h" @@ -36,6 +37,18 @@ constexpr auto kDWMWA_WINDOW_CORNER_PREFERENCE = DWORD(33); constexpr auto kDWMWA_CAPTION_COLOR = DWORD(35); constexpr auto kDWMWA_TEXT_COLOR = DWORD(36); +UINT(__stdcall *GetDpiForWindow)(_In_ HWND hwnd); + +[[nodiscard]] bool GetDpiForWindowSupported() { + static const auto Result = [&] { +#define LOAD_SYMBOL(lib, name) base::Platform::LoadMethod(lib, #name, name) + const auto user32 = base::Platform::SafeLoadLibrary(L"User32.dll"); + return LOAD_SYMBOL(user32, GetDpiForWindow); +#undef LOAD_SYMBOL + }(); + return Result; +} + [[nodiscard]] bool IsCompositionEnabled() { auto result = BOOL(FALSE); const auto success = (DwmIsCompositionEnabled(&result) == S_OK); @@ -163,7 +176,8 @@ WindowHelper::WindowHelper(not_null window) , _handle(ResolveWindowHandle(window)) , _title(Ui::CreateChild(window.get())) , _body(Ui::CreateChild(window.get())) -, _shadow(std::in_place, window, st::windowShadowFg->c) { +, _shadow(std::in_place, window, st::windowShadowFg->c) +, _dpi(GetDpiForWindowSupported() ? GetDpiForWindow(_handle) : 0) { Expects(_handle != nullptr); init(); @@ -595,6 +609,13 @@ bool WindowHelper::handleNativeEvent( } } return true; + // should return true for Qt not to change window size + // when moving the window between screens + // change to false once runtime scale change would be supported + case WM_DPICHANGED: { + _dpi = LOWORD(wParam); + } return true; + } return false; } diff --git a/ui/platform/win/ui_window_win.h b/ui/platform/win/ui_window_win.h index 1882a61..9ae881f 100644 --- a/ui/platform/win/ui_window_win.h +++ b/ui/platform/win/ui_window_win.h @@ -81,6 +81,7 @@ private: rpl::event_stream _systemButtonOver; rpl::event_stream _systemButtonDown; std::optional _shadow; + rpl::variable _dpi; QMargins _marginsDelta; HMENU _menu = nullptr; bool _updatingMargins = false;