From 1bf992ed691ec6013bc6ca7183c2b6d6e9a71fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E5=AD=90=E6=A5=9A=5Czhuzi?= Date: Mon, 18 Dec 2023 21:29:38 +0800 Subject: [PATCH] fix bug #373 --- src/FluFrameless.cpp | 59 ++++++++-- .../imports/FluentUI/Controls/FluWindow.qml | 109 ++++++++++-------- .../imports/FluentUI/Controls/FluWindow.qml | 109 ++++++++++-------- 3 files changed, 166 insertions(+), 111 deletions(-) diff --git a/src/FluFrameless.cpp b/src/FluFrameless.cpp index 4af0ccb6..8cb9d081 100644 --- a/src/FluFrameless.cpp +++ b/src/FluFrameless.cpp @@ -12,6 +12,23 @@ static inline QByteArray qtNativeEventType() } #endif +static inline bool isCompositionEnabled(){ + typedef HRESULT (WINAPI* DwmIsCompositionEnabledPtr)(BOOL *pfEnabled); + HMODULE module = LoadLibraryW(L"dwmapi.dll"); + if (module) + { + BOOL composition_enabled = false; + DwmIsCompositionEnabledPtr dwm_is_composition_enabled; + dwm_is_composition_enabled= reinterpret_cast(GetProcAddress(module, "DwmIsCompositionEnabled")); + if (dwm_is_composition_enabled) + { + dwm_is_composition_enabled(&composition_enabled); + } + return composition_enabled; + } + return false; +} + FramelessEventFilter::FramelessEventFilter(QQuickWindow* window){ _window = window; _current = window->winId(); @@ -23,25 +40,27 @@ bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void * return false; } const auto msg = static_cast(message); - const HWND hWnd = msg->hwnd; - if (!hWnd) { + const HWND hwnd = msg->hwnd; + if (!hwnd) { return false; } - const qint64 wid = reinterpret_cast(hWnd); + const qint64 wid = reinterpret_cast(hwnd); if(wid != _current){ return false; } const UINT uMsg = msg->message; - if (!msg || !msg->hwnd) + const WPARAM wParam = msg->wParam; + const LPARAM lParam = msg->lParam; + if (!msg || !hwnd) { return false; } if(uMsg == WM_WINDOWPOSCHANGING){ - WINDOWPOS* wp = reinterpret_cast(msg->lParam); + WINDOWPOS* wp = reinterpret_cast(lParam); if (wp != nullptr && (wp->flags & SWP_NOSIZE) == 0) { wp->flags |= SWP_NOCOPYBITS; - *result = DefWindowProc(msg->hwnd, msg->message, msg->wParam, msg->lParam); + *result = DefWindowProc(hwnd, uMsg, wParam, lParam); return true; } return false; @@ -49,8 +68,22 @@ bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void * *result = WVR_REDRAW; return true; }else if(uMsg == WM_NCPAINT){ - *result = WVR_REDRAW; + if(!isCompositionEnabled()){ + *result = WVR_REDRAW; + return true; + } return false; + }else if(uMsg == WM_NCACTIVATE){ + if(isCompositionEnabled()){ + *result = DefWindowProc(hwnd, uMsg, wParam, -1); + }else{ + if (wParam == FALSE) { + *result = TRUE; + } else { + *result = FALSE; + } + } + return true; } return false; #endif @@ -90,7 +123,7 @@ void FluFrameless::updateCursor(int edges){ } bool FluFrameless::eventFilter(QObject *obj, QEvent *ev){ - if (!_window.isNull() && _window->flags()& Qt::FramelessWindowHint) { + if (!_window.isNull()) { static int edges = 0; const int margin = 8; @@ -156,25 +189,29 @@ void FluFrameless::componentComplete(){ o = o->parent(); } if(!_window.isNull()){ - _window->setFlag(Qt::FramelessWindowHint,true); - _window->installEventFilter(this); #ifdef Q_OS_WIN _nativeEvent =new FramelessEventFilter(_window); qApp->installNativeEventFilter(_nativeEvent); HWND hwnd = reinterpret_cast(_window->winId()); ULONG_PTR cNewStyle = GetClassLongPtr(hwnd, GCL_STYLE) | CS_DROPSHADOW; SetClassLongPtr(hwnd, GCL_STYLE, cNewStyle); + DWORD style = GetWindowLongPtr(hwnd,GWL_STYLE); + SetWindowLongPtr(hwnd,GWL_STYLE,style &~ WS_SYSMENU); SetWindowPos(hwnd,nullptr,0,0,0,0,SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE |SWP_FRAMECHANGED); +#else + _window->setFlag(Qt::FramelessWindowHint,true); #endif + _window->installEventFilter(this); } } FluFrameless::~FluFrameless(){ if (!_window.isNull()) { - _window->setFlag(Qt::FramelessWindowHint,false); _window->removeEventFilter(this); #ifdef Q_OS_WIN qApp->removeNativeEventFilter(_nativeEvent); +#else + _window->setFlag(Qt::FramelessWindowHint,false); #endif } } diff --git a/src/Qt5/imports/FluentUI/Controls/FluWindow.qml b/src/Qt5/imports/FluentUI/Controls/FluWindow.qml index 710235ac..fd2f58f9 100644 --- a/src/Qt5/imports/FluentUI/Controls/FluWindow.qml +++ b/src/Qt5/imports/FluentUI/Controls/FluWindow.qml @@ -197,60 +197,69 @@ Window { FluLoader{ id:loader_frameless } - onWidthChanged: { - window.appBar.width = width - } - FluLoader{ - id:loader_app_bar - anchors { - top: parent.top - left: parent.left - right: parent.right - } - height: { - if(window.useSystemAppBar){ - return 0 - } - return window.fitsAppBarWindows ? 0 : window.appBar.height - } - sourceComponent: window.useSystemAppBar ? undefined : com_app_bar - } Item{ - id:container - anchors{ - top: loader_app_bar.bottom - left: parent.left - right: parent.right - bottom: parent.bottom - } - clip: true - } - FluLoader{ - property string loadingText: "加载中..." - property bool cancel: false - id:loader_loading - anchors.fill: container - } - FluInfoBar{ - id:infoBar - root: window - } - WindowLifecycle{ - id:lifecycle - } - Rectangle{ anchors.fill: parent - color:"transparent" - border.width: window.resizeBorderWidth - border.color: window.resizeBorderColor - visible: { - if(window.useSystemAppBar){ - return false + anchors.margins: { + if(FluTools.isWin() && !window.useSystemAppBar){ + return window.visibility === Window.Maximized ? 8 : 0 } - if(window.visibility == Window.Maximized || window.visibility == Window.FullScreen){ - return false + return 0 + } + onWidthChanged: { + window.appBar.width = width + } + FluLoader{ + id:loader_app_bar + anchors { + top: parent.top + left: parent.left + right: parent.right + } + height: { + if(window.useSystemAppBar){ + return 0 + } + return window.fitsAppBarWindows ? 0 : window.appBar.height + } + sourceComponent: window.useSystemAppBar ? undefined : com_app_bar + } + Item{ + id:container + anchors{ + top: loader_app_bar.bottom + left: parent.left + right: parent.right + bottom: parent.bottom + } + clip: true + } + FluLoader{ + property string loadingText: "加载中..." + property bool cancel: false + id:loader_loading + anchors.fill: container + } + FluInfoBar{ + id:infoBar + root: window + } + WindowLifecycle{ + id:lifecycle + } + Rectangle{ + anchors.fill: parent + color:"transparent" + border.width: window.resizeBorderWidth + border.color: window.resizeBorderColor + visible: { + if(window.useSystemAppBar){ + return false + } + if(window.visibility == Window.Maximized || window.visibility == Window.FullScreen){ + return false + } + return true } - return true } } function destoryOnClose(){ diff --git a/src/Qt6/imports/FluentUI/Controls/FluWindow.qml b/src/Qt6/imports/FluentUI/Controls/FluWindow.qml index 23c65a4a..e18ff8f4 100644 --- a/src/Qt6/imports/FluentUI/Controls/FluWindow.qml +++ b/src/Qt6/imports/FluentUI/Controls/FluWindow.qml @@ -196,60 +196,69 @@ Window { FluLoader{ id:loader_frameless } - onWidthChanged: { - window.appBar.width = width - } - FluLoader{ - id:loader_app_bar - anchors { - top: parent.top - left: parent.left - right: parent.right - } - height: { - if(window.useSystemAppBar){ - return 0 - } - return window.fitsAppBarWindows ? 0 : window.appBar.height - } - sourceComponent: window.useSystemAppBar ? undefined : com_app_bar - } Item{ - id:container - anchors{ - top: loader_app_bar.bottom - left: parent.left - right: parent.right - bottom: parent.bottom - } - clip: true - } - FluLoader{ - property string loadingText: "加载中..." - property bool cancel: false - id:loader_loading - anchors.fill: container - } - FluInfoBar{ - id:infoBar - root: window - } - WindowLifecycle{ - id:lifecycle - } - Rectangle{ anchors.fill: parent - color:"transparent" - border.width: window.resizeBorderWidth - border.color: window.resizeBorderColor - visible: { - if(window.useSystemAppBar){ - return false + anchors.margins: { + if(FluTools.isWin() && !window.useSystemAppBar){ + return window.visibility === Window.Maximized ? 8 : 0 } - if(window.visibility == Window.Maximized || window.visibility == Window.FullScreen){ - return false + return 0 + } + onWidthChanged: { + window.appBar.width = width + } + FluLoader{ + id:loader_app_bar + anchors { + top: parent.top + left: parent.left + right: parent.right + } + height: { + if(window.useSystemAppBar){ + return 0 + } + return window.fitsAppBarWindows ? 0 : window.appBar.height + } + sourceComponent: window.useSystemAppBar ? undefined : com_app_bar + } + Item{ + id:container + anchors{ + top: loader_app_bar.bottom + left: parent.left + right: parent.right + bottom: parent.bottom + } + clip: true + } + FluLoader{ + property string loadingText: "加载中..." + property bool cancel: false + id:loader_loading + anchors.fill: container + } + FluInfoBar{ + id:infoBar + root: window + } + WindowLifecycle{ + id:lifecycle + } + Rectangle{ + anchors.fill: parent + color:"transparent" + border.width: window.resizeBorderWidth + border.color: window.resizeBorderColor + visible: { + if(window.useSystemAppBar){ + return false + } + if(window.visibility == Window.Maximized || window.visibility == Window.FullScreen){ + return false + } + return true } - return true } } function destoryOnClose(){