Fix the context menu issue.

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2020-10-18 11:53:04 +08:00
parent d3f58ae98f
commit f38b86e992
3 changed files with 41 additions and 5 deletions

View File

@ -26,8 +26,14 @@
#include "../../winnativeeventfilter.h"
#include "ui_widget.h"
#include <QColorDialog>
#include <QDebug>
#include <QOperatingSystemVersion>
#include <QStyleOption>
#include <qt_windows.h>
// 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<LPMSG>(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);
}

View File

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

View File

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