From 6b3f555a95330f6211058e4497ca12349f9f21b5 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Fri, 3 Apr 2020 09:57:00 +0800 Subject: [PATCH] Update. Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- winnativeeventfilter.cpp | 85 ++++++++++++++++++++++++---------------- winnativeeventfilter.h | 1 + 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/winnativeeventfilter.cpp b/winnativeeventfilter.cpp index bc26dbf..486b0ae 100644 --- a/winnativeeventfilter.cpp +++ b/winnativeeventfilter.cpp @@ -16,6 +16,11 @@ Q_DECLARE_METATYPE(QMargins) +#ifndef USER_DEFAULT_SCREEN_DPI +// FIXME: Since which version? +#define USER_DEFAULT_SCREEN_DPI 96 +#endif + #ifndef SM_CXPADDEDBORDER // Only available since Windows Vista #define SM_CXPADDEDBORDER 92 @@ -104,39 +109,7 @@ QVector m_framelessWindows; } // namespace -WinNativeEventFilter::WinNativeEventFilter() { - QLibrary user32Lib(QString::fromUtf8("User32")), - shcoreLib(QString::fromUtf8("SHCore")); - if (QOperatingSystemVersion::current() >= - QOperatingSystemVersion::Windows7) { - m_SetWindowCompositionAttribute = - reinterpret_cast( - user32Lib.resolve("SetWindowCompositionAttribute")); - } - if (QOperatingSystemVersion::current() >= - QOperatingSystemVersion::Windows8_1) { - m_GetDpiForMonitor = reinterpret_cast( - shcoreLib.resolve("GetDpiForMonitor")); - } - // Windows 10, version 1607 (10.0.14393) - if (QOperatingSystemVersion::current() >= - QOperatingSystemVersion(QOperatingSystemVersion::Windows, 10, 0, - 14393)) { - m_GetDpiForWindow = reinterpret_cast( - user32Lib.resolve("GetDpiForWindow")); - m_GetDpiForSystem = reinterpret_cast( - user32Lib.resolve("GetDpiForSystem")); - m_GetSystemMetricsForDpi = reinterpret_cast( - user32Lib.resolve("GetSystemMetricsForDpi")); - } - // Windows 10, version 1803 (10.0.17134) - if (QOperatingSystemVersion::current() >= - QOperatingSystemVersion(QOperatingSystemVersion::Windows, 10, 0, - 17134)) { - m_GetSystemDpiForProcess = reinterpret_cast( - user32Lib.resolve("GetSystemDpiForProcess")); - } -} +WinNativeEventFilter::WinNativeEventFilter() { initDLLs(); } WinNativeEventFilter::~WinNativeEventFilter() = default; @@ -874,3 +847,49 @@ void WinNativeEventFilter::updateQtFrame(QWindow *window) { } } } + +void WinNativeEventFilter::initDLLs() { + QLibrary user32Lib(QString::fromUtf8("User32")), + shcoreLib(QString::fromUtf8("SHCore")); + if (QOperatingSystemVersion::current() >= + QOperatingSystemVersion::Windows7) { + if (!m_SetWindowCompositionAttribute) { + m_SetWindowCompositionAttribute = + reinterpret_cast( + user32Lib.resolve("SetWindowCompositionAttribute")); + } + } + if (QOperatingSystemVersion::current() >= + QOperatingSystemVersion::Windows8_1) { + if (!m_GetDpiForMonitor) { + m_GetDpiForMonitor = reinterpret_cast( + shcoreLib.resolve("GetDpiForMonitor")); + } + } + // Windows 10, version 1607 (10.0.14393) + if (QOperatingSystemVersion::current() >= + QOperatingSystemVersion(QOperatingSystemVersion::Windows, 10, 0, + 14393)) { + if (!m_GetDpiForWindow) { + m_GetDpiForWindow = reinterpret_cast( + user32Lib.resolve("GetDpiForWindow")); + } + if (!m_GetDpiForSystem) { + m_GetDpiForSystem = reinterpret_cast( + user32Lib.resolve("GetDpiForSystem")); + } + if (!m_GetSystemMetricsForDpi) { + m_GetSystemMetricsForDpi = reinterpret_cast( + user32Lib.resolve("GetSystemMetricsForDpi")); + } + } + // Windows 10, version 1803 (10.0.17134) + if (QOperatingSystemVersion::current() >= + QOperatingSystemVersion(QOperatingSystemVersion::Windows, 10, 0, + 17134)) { + if (!m_GetSystemDpiForProcess) { + m_GetSystemDpiForProcess = reinterpret_cast( + user32Lib.resolve("GetSystemDpiForProcess")); + } + } +} diff --git a/winnativeeventfilter.h b/winnativeeventfilter.h index 576da3f..c99449d 100644 --- a/winnativeeventfilter.h +++ b/winnativeeventfilter.h @@ -66,6 +66,7 @@ public: private: void init(LPWINDOW data); + void initDLLs(); static void createUserData(HWND handle, WINDOWDATA *data = nullptr); void handleDwmCompositionChanged(LPWINDOW data); void handleThemeChanged(LPWINDOW data);