win: add more sanity checks

This commit is contained in:
Yuhang Zhao 2023-07-18 17:53:44 +08:00
parent 2cd4d133f3
commit 8e73e3eac8
3 changed files with 43 additions and 12 deletions

View File

@ -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

View File

@ -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<WId>(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<WId>(hWnd);
if (!g_win32Helper()->data.contains(windowId)) {
return false;
}

View File

@ -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<HWND>(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