From f38b86e9922c45a5069db6a4fa5f16aa8434926c Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Sun, 18 Oct 2020 11:53:04 +0800 Subject: [PATCH] Fix the context menu issue. Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- examples/Win32Demo/widget.cpp | 34 ++++++++++++++++++++++++++++++++++ examples/Win32Demo/widget.h | 1 + winnativeeventfilter.cpp | 11 ++++++----- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/examples/Win32Demo/widget.cpp b/examples/Win32Demo/widget.cpp index a762799..1d3d491 100644 --- a/examples/Win32Demo/widget.cpp +++ b/examples/Win32Demo/widget.cpp @@ -26,8 +26,14 @@ #include "../../winnativeeventfilter.h" #include "ui_widget.h" #include +#include #include #include +#include + +// Copied from windowsx.h +#define GET_X_LPARAM(lp) ((int) (short) LOWORD(lp)) +#define GET_Y_LPARAM(lp) ((int) (short) HIWORD(lp)) namespace { @@ -175,3 +181,31 @@ bool Widget::eventFilter(QObject *object, QEvent *event) } return QWidget::eventFilter(object, event); } + +bool Widget::nativeEvent(const QByteArray &eventType, void *message, long *result) +{ + Q_ASSERT(eventType == "windows_generic_MSG"); + Q_ASSERT(message); + Q_ASSERT(result); + const auto msg = static_cast(message); + const auto getCursorPosition = [](const LPARAM lParam) -> QPoint { + return {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)}; + }; + switch (msg->message) { + case WM_NCRBUTTONUP: { + const QPoint pos = getCursorPosition(msg->lParam); + qDebug() << "WM_NCRBUTTONUP -->" << pos; + if (WinNativeEventFilter::displaySystemMenu(msg->hwnd, false, pos.x(), pos.y())) { + *result = 0; + return true; + } + break; + } + case WM_RBUTTONUP: + qDebug() << "WM_RBUTTONUP -->" << getCursorPosition(msg->lParam); + break; + default: + break; + } + return QWidget::nativeEvent(eventType, message, result); +} diff --git a/examples/Win32Demo/widget.h b/examples/Win32Demo/widget.h index 2fe0cd3..2efb100 100644 --- a/examples/Win32Demo/widget.h +++ b/examples/Win32Demo/widget.h @@ -43,6 +43,7 @@ public: protected: bool eventFilter(QObject *object, QEvent *event) override; + bool nativeEvent(const QByteArray &eventType, void *message, long *result) override; private: Ui::Widget *ui = nullptr; diff --git a/winnativeeventfilter.cpp b/winnativeeventfilter.cpp index a022afe..10a7370 100644 --- a/winnativeeventfilter.cpp +++ b/winnativeeventfilter.cpp @@ -987,8 +987,8 @@ void UpdateFrameMarginsForWindow(const HWND handle) margins.cyTopHeight = GetFrameSizeForWindow(handle, TRUE).top; } } else { - margins.cyTopHeight = 1; - //margins.cyTopHeight = GetFrameSizeForWindow(handle, TRUE).top; + //margins.cyTopHeight = 1; + margins.cyTopHeight = GetFrameSizeForWindow(handle, TRUE).top; } if (shouldUseNativeTitleBar()) { // If we are going to use the native title bar, @@ -1133,7 +1133,7 @@ bool displaySystemMenu_internal(const HWND handle, const bool isRtl, const LPARA return false; } -QString getCurrentScreenSerialNumber(const HWND handle) +QString getCurrentScreenIdentifier(const HWND handle) { Q_ASSERT(handle); if (WNEF_EXECUTE_WINAPI_RETURN(IsWindow, FALSE, handle)) { @@ -1326,7 +1326,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType, // Avoid initializing a same window twice. data->initialized = true; // Record the current screen. - data->currentScreen = getCurrentScreenSerialNumber(msg->hwnd); + data->currentScreen = getCurrentScreenIdentifier(msg->hwnd); Q_ASSERT(!data->currentScreen.isEmpty()); // Don't restore the window styles to default when you are // developing Qt Quick applications because the QWindow @@ -2003,6 +2003,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType, *result = ret; return true; } + case WM_ACTIVATE: case WM_DWMCOMPOSITIONCHANGED: { if (shouldUseNativeTitleBar()) { break; @@ -2059,7 +2060,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType, break; } - const QString sn = getCurrentScreenSerialNumber(msg->hwnd); + const QString sn = getCurrentScreenIdentifier(msg->hwnd); if (data->currentScreen.toUpper() != sn) { data->currentScreen = sn; updateWindow(msg->hwnd, true, true);