From 8e73e3eac8b6b2f02ca451840b42a59bf3bb2dd2 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao Date: Tue, 18 Jul 2023 17:53:44 +0800 Subject: [PATCH] win: add more sanity checks --- include/FramelessHelper/Core/utils.h | 3 +- src/core/framelesshelper_win.cpp | 4 +-- src/core/utils_win.cpp | 48 ++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/include/FramelessHelper/Core/utils.h b/include/FramelessHelper/Core/utils.h index 763e3996..7a8aeab1 100644 --- a/include/FramelessHelper/Core/utils.h +++ b/include/FramelessHelper/Core/utils.h @@ -145,7 +145,8 @@ FRAMELESSHELPER_CORE_API void bringWindowToFront(const WId windowId); [[nodiscard]] FRAMELESSHELPER_CORE_API QRect getWindowRestoreGeometry(const WId windowId); FRAMELESSHELPER_CORE_API void removeMicaWindow(const WId windowId); FRAMELESSHELPER_CORE_API void removeSysMenuHook(const WId windowId); -FRAMELESSHELPER_CORE_API quint64 queryMouseState(); +FRAMELESSHELPER_CORE_API quint64 queryMouseButtonState(); +FRAMELESSHELPER_CORE_API bool isValidWindow(const WId windowId, const bool checkVisible, const bool checkTopLevel); #endif // Q_OS_WINDOWS #ifdef Q_OS_LINUX diff --git a/src/core/framelesshelper_win.cpp b/src/core/framelesshelper_win.cpp index 25010df8..735fa709 100644 --- a/src/core/framelesshelper_win.cpp +++ b/src/core/framelesshelper_win.cpp @@ -624,8 +624,9 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me // Anyway, we should skip the entire processing in this case. return false; } + const auto windowId = reinterpret_cast(hWnd); // Let's be extra safe. - if (IsWindow(hWnd) == FALSE) { + if (!Utils::isValidWindow(windowId, true, true)) { return false; } const UINT uMsg = msg->message; @@ -633,7 +634,6 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me if ((uMsg == WM_CLOSE) || (uMsg == WM_DESTROY)) { return false; } - const auto windowId = reinterpret_cast(hWnd); if (!g_win32Helper()->data.contains(windowId)) { return false; } diff --git a/src/core/utils_win.cpp b/src/core/utils_win.cpp index 0e2f5cfd..7fd771f8 100644 --- a/src/core/utils_win.cpp +++ b/src/core/utils_win.cpp @@ -2464,31 +2464,61 @@ void Utils::removeSysMenuHook(const WId windowId) g_utilsHelper()->data.remove(windowId); } -quint64 Utils::queryMouseState() +quint64 Utils::queryMouseButtonState() { - WPARAM result = 0; - if (GetKeyState(VK_LBUTTON) < 0) { + quint64 result = 0; + if (::GetKeyState(VK_LBUTTON) < 0) { result |= MK_LBUTTON; } - if (GetKeyState(VK_RBUTTON) < 0) { + if (::GetKeyState(VK_RBUTTON) < 0) { result |= MK_RBUTTON; } - if (GetKeyState(VK_SHIFT) < 0) { + if (::GetKeyState(VK_SHIFT) < 0) { result |= MK_SHIFT; } - if (GetKeyState(VK_CONTROL) < 0) { + if (::GetKeyState(VK_CONTROL) < 0) { result |= MK_CONTROL; } - if (GetKeyState(VK_MBUTTON) < 0) { + if (::GetKeyState(VK_MBUTTON) < 0) { result |= MK_MBUTTON; } - if (GetKeyState(VK_XBUTTON1) < 0) { + if (::GetKeyState(VK_XBUTTON1) < 0) { result |= MK_XBUTTON1; } - if (GetKeyState(VK_XBUTTON2) < 0) { + if (::GetKeyState(VK_XBUTTON2) < 0) { result |= MK_XBUTTON2; } return result; } +bool Utils::isValidWindow(const WId windowId, const bool checkVisible, const bool checkTopLevel) +{ + Q_ASSERT(windowId); + if (!windowId) { + return false; + } + const auto hwnd = reinterpret_cast(windowId); + if (::IsWindow(hwnd) == FALSE) { + return false; + } + RECT rect = { 0, 0, 0, 0 }; + if (::GetWindowRect(hwnd, &rect) == FALSE) { + return false; + } + if ((rect.left >= rect.right) || (rect.top >= rect.bottom)) { + return false; + } + if (checkVisible) { + if (::IsWindowVisible(hwnd) == FALSE) { + return false; + } + } + if (checkTopLevel) { + if (::GetAncestor(hwnd, GA_ROOT) != hwnd) { + return false; + } + } + return true; +} + FRAMELESSHELPER_END_NAMESPACE