From bb3aeb7f4cd4c8e4af094ecc86c85b0f1cc62a60 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Sat, 11 Apr 2020 18:43:21 +0800 Subject: [PATCH] Update. Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- winnativeeventfilter.cpp | 43 +++++++++++++++++++++++++--------------- winnativeeventfilter.h | 2 ++ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/winnativeeventfilter.cpp b/winnativeeventfilter.cpp index c6532ec..a2c22ed 100644 --- a/winnativeeventfilter.cpp +++ b/winnativeeventfilter.cpp @@ -294,7 +294,9 @@ void WinNativeEventFilter::setFramelessWindows(QVector windows) { void WinNativeEventFilter::addFramelessWindow(HWND window, const WINDOWDATA *data) { - if (window && !m_framelessWindows.contains(window)) { + initWin32Api(); + if (window && m_lpIsWindow(window) && + !m_framelessWindows.contains(window)) { m_framelessWindows.append(window); if (data) { createUserData(window, data); @@ -317,7 +319,7 @@ void WinNativeEventFilter::clearFramelessWindows() { int WinNativeEventFilter::borderWidth(HWND handle) { initWin32Api(); - if (handle) { + if (handle && m_lpIsWindow(handle)) { createUserData(handle); const auto userData = reinterpret_cast( __GetWindowLongPtrW(handle, GWLP_USERDATA)); @@ -335,7 +337,7 @@ int WinNativeEventFilter::borderWidth(HWND handle) { int WinNativeEventFilter::borderHeight(HWND handle) { initWin32Api(); - if (handle) { + if (handle && m_lpIsWindow(handle)) { createUserData(handle); const auto userData = reinterpret_cast( __GetWindowLongPtrW(handle, GWLP_USERDATA)); @@ -354,7 +356,7 @@ int WinNativeEventFilter::borderHeight(HWND handle) { int WinNativeEventFilter::titlebarHeight(HWND handle) { initWin32Api(); - if (handle) { + if (handle && m_lpIsWindow(handle)) { createUserData(handle); const auto userData = reinterpret_cast( __GetWindowLongPtrW(handle, GWLP_USERDATA)); @@ -723,6 +725,10 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType, *result = 1; return true; } + case WM_WINDOWPOSCHANGED: { + updateWindow(msg->hwnd); + break; + } default: { break; } @@ -747,16 +753,7 @@ void WinNativeEventFilter::updateGlass(HWND handle) { margins = {-1, -1, -1, -1}; } m_lpDwmExtendFrameIntoClientArea(handle, &margins); - // Trigger a frame change event to kick in the change. - // You may find that remove this line doesn't seem to have any side-effects, - // well, you are right in most cases, but don't remove it because it insures - // our code still work well in some rare cases. - m_lpSetWindowPos(handle, nullptr, 0, 0, 0, 0, - SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOSIZE | - SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER); - // Redraw the window. - m_lpRedrawWindow(handle, nullptr, nullptr, - RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN); + updateWindow(handle); } UINT WinNativeEventFilter::getDotsPerInchForWindow(HWND handle) { @@ -830,7 +827,7 @@ int WinNativeEventFilter::getSystemMetricsForWindow(HWND handle, int index) { void WinNativeEventFilter::setWindowData(HWND window, const WINDOWDATA *data) { initWin32Api(); - if (window && data) { + if (window && m_lpIsWindow(window) && data) { createUserData(window, data); } } @@ -838,7 +835,7 @@ void WinNativeEventFilter::setWindowData(HWND window, const WINDOWDATA *data) { WinNativeEventFilter::WINDOWDATA * WinNativeEventFilter::windowData(HWND window) { initWin32Api(); - if (window) { + if (window && m_lpIsWindow(window)) { createUserData(window); return &reinterpret_cast( __GetWindowLongPtrW(window, GWLP_USERDATA)) @@ -1008,3 +1005,17 @@ qreal WinNativeEventFilter::getPreferedNumber(qreal num) { #endif return result; } + +void WinNativeEventFilter::updateWindow(HWND handle) { + initWin32Api(); + if (handle && m_lpIsWindow(handle)) { + m_lpSetWindowPos(handle, nullptr, 0, 0, 0, 0, + SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOSIZE | + SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER); + m_lpInvalidateRect(handle, nullptr, TRUE); + m_lpRedrawWindow(handle, nullptr, nullptr, + RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | + RDW_ALLCHILDREN); + m_lpUpdateWindow(handle); + } +} diff --git a/winnativeeventfilter.h b/winnativeeventfilter.h index 6a9aecf..8d7fd3a 100644 --- a/winnativeeventfilter.h +++ b/winnativeeventfilter.h @@ -94,6 +94,8 @@ public: // window (if the pointer is null, return the system's standard value). static int titlebarHeight(HWND handle); + static void updateWindow(HWND handle); + #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) bool nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result) override;