From 284afed52f7aabb957d2469d38e6812f2d8aee09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E5=AD=90=E6=A5=9A=5Czhuzi?= Date: Tue, 2 Jan 2024 22:33:47 +0800 Subject: [PATCH] update --- src/FluFramelessHelper.cpp | 72 +++++++++++-------- src/FluFramelessHelper.h | 1 + .../imports/FluentUI/Controls/FluWindow.qml | 4 ++ .../imports/FluentUI/Controls/FluWindow.qml | 4 ++ 4 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/FluFramelessHelper.cpp b/src/FluFramelessHelper.cpp index bc9df2c1..9d99254a 100644 --- a/src/FluFramelessHelper.cpp +++ b/src/FluFramelessHelper.cpp @@ -57,21 +57,26 @@ static inline bool isCompositionEnabled(){ } return composition_enabled; } - return false; + return true; } static inline void showShadow(HWND hwnd){ - const MARGINS shadow = { 0, 0, 1, 0 }; - typedef HRESULT (WINAPI* DwmExtendFrameIntoClientAreaPtr)(HWND hWnd, const MARGINS *pMarInset); - HMODULE module = LoadLibraryW(L"dwmapi.dll"); - if (module) - { - DwmExtendFrameIntoClientAreaPtr dwm_extendframe_into_client_area_; - dwm_extendframe_into_client_area_= reinterpret_cast(GetProcAddress(module, "DwmExtendFrameIntoClientArea")); - if (dwm_extendframe_into_client_area_) + if(isCompositionEnabled()){ + const MARGINS shadow = { 0, 0, 1, 0 }; + typedef HRESULT (WINAPI* DwmExtendFrameIntoClientAreaPtr)(HWND hWnd, const MARGINS *pMarInset); + HMODULE module = LoadLibraryW(L"dwmapi.dll"); + if (module) { - dwm_extendframe_into_client_area_(hwnd, &shadow); + DwmExtendFrameIntoClientAreaPtr dwm_extendframe_into_client_area_; + dwm_extendframe_into_client_area_= reinterpret_cast(GetProcAddress(module, "DwmExtendFrameIntoClientArea")); + if (dwm_extendframe_into_client_area_) + { + dwm_extendframe_into_client_area_(hwnd, &shadow); + } } + }else{ + ULONG_PTR cNewStyle = GetClassLongPtr(hwnd, GCL_STYLE) | CS_DROPSHADOW; + SetClassLongPtr(hwnd, GCL_STYLE, cNewStyle); } } @@ -111,7 +116,9 @@ bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void * }else if(uMsg == WM_NCCALCSIZE){ const auto clientRect = ((wParam == FALSE) ? reinterpret_cast(lParam) : &(reinterpret_cast(lParam))->rgrc[0]); const LONG originalTop = clientRect->top; + const LONG originalBottom = clientRect->bottom; const LONG originalLeft = clientRect->left; + const LONG originalRight = clientRect->right; const LRESULT hitTestResult = ::DefWindowProcW(hwnd, WM_NCCALCSIZE, wParam, lParam); if ((hitTestResult != HTERROR) && (hitTestResult != HTNOWHERE)) { *result = hitTestResult; @@ -133,7 +140,14 @@ bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void * offsetTop = 1; } } - clientRect->top = originalTop+offsetTop; + if(isCompositionEnabled()){ + clientRect->top = originalTop+offsetTop; + }else{ + clientRect->top = originalTop; + clientRect->bottom = originalBottom; + clientRect->left = originalLeft; + clientRect->right = originalRight; + } *result = WVR_REDRAW; return true; }if(uMsg == WM_NCHITTEST){ @@ -157,8 +171,8 @@ bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void * QGuiApplication::sendEvent(_helper->maximizeButton(),&event); } return false; - }else if(uMsg == WM_NCPAINT){ - *result = 0; + }else if(uMsg == WM_NCPAINT || uMsg == 0x00AE || uMsg == 0x00AF){ + *result = FALSE; return true; }else if(uMsg == WM_NCACTIVATE){ *result = DefWindowProcW(hwnd, WM_NCACTIVATE, wParam, -1); @@ -285,23 +299,21 @@ void FluFramelessHelper::componentComplete(){ _realHeight = QQmlProperty(window,"_realHeight"); _realWidth = QQmlProperty(window,"_realWidth"); _appBarHeight = QQmlProperty(window,"_appBarHeight"); + _enableMarginsBottomLeftRight = QQmlProperty(window,"_enableMarginsBottomLeftRight"); #ifdef Q_OS_WIN - if(isCompositionEnabled()){ - window->setFlag(Qt::CustomizeWindowHint,true); - _nativeEvent =new FramelessEventFilter(this); - qApp->installNativeEventFilter(_nativeEvent); - HWND hwnd = reinterpret_cast(window->winId()); - DWORD style = ::GetWindowLong(hwnd, GWL_STYLE); - if(resizeable()){ - SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_MAXIMIZEBOX | WS_THICKFRAME); - }else{ - SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_THICKFRAME); - } - SetWindowPos(hwnd,nullptr,0,0,0,0,SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); - + _enableMarginsBottomLeftRight.write(!isCompositionEnabled()); + window->setFlag(Qt::CustomizeWindowHint,true); + _nativeEvent =new FramelessEventFilter(this); + qApp->installNativeEventFilter(_nativeEvent); + HWND hwnd = reinterpret_cast(window->winId()); + DWORD style = ::GetWindowLong(hwnd, GWL_STYLE); + if(resizeable()){ + SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_MAXIMIZEBOX | WS_THICKFRAME); }else{ - window->setFlags((window->flags() & (~Qt::WindowMinMaxButtonsHint) & (~Qt::Dialog)) | Qt::FramelessWindowHint | Qt::Window); + SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_THICKFRAME); } + SetWindowPos(hwnd,nullptr,0,0,0,0,SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); + showShadow(hwnd); #else window->setFlags((window->flags() & (~Qt::WindowMinMaxButtonsHint) & (~Qt::Dialog)) | Qt::FramelessWindowHint | Qt::Window); #endif @@ -374,10 +386,8 @@ FluFramelessHelper::~FluFramelessHelper(){ if (!window.isNull()) { window->setFlags(Qt::Window); #ifdef Q_OS_WIN - if(isCompositionEnabled()){ - qApp->removeNativeEventFilter(_nativeEvent); - delete _nativeEvent; - } + qApp->removeNativeEventFilter(_nativeEvent); + delete _nativeEvent; #endif window->removeEventFilter(this); } diff --git a/src/FluFramelessHelper.h b/src/FluFramelessHelper.h index 445e67af..79a658e3 100644 --- a/src/FluFramelessHelper.h +++ b/src/FluFramelessHelper.h @@ -61,6 +61,7 @@ private: QQmlProperty _realHeight; QQmlProperty _realWidth; QQmlProperty _appBarHeight; + QQmlProperty _enableMarginsBottomLeftRight; }; #endif // FLUFRAMELESSHELPER_H diff --git a/src/Qt5/imports/FluentUI/Controls/FluWindow.qml b/src/Qt5/imports/FluentUI/Controls/FluWindow.qml index 31f4b7b8..bc2a28be 100644 --- a/src/Qt5/imports/FluentUI/Controls/FluWindow.qml +++ b/src/Qt5/imports/FluentUI/Controls/FluWindow.qml @@ -64,6 +64,7 @@ Window { property int _realHeight property int _realWidth property int _appBarHeight: appBar.height + property bool _enableMarginsBottomLeftRight: false id:window color:"transparent" Component.onCompleted: { @@ -209,6 +210,9 @@ Window { anchors{ fill:parent topMargin: _offsetXY.y + bottomMargin: _enableMarginsBottomLeftRight ? _offsetXY.y : 0 + leftMargin: _enableMarginsBottomLeftRight ? _offsetXY.x : 0 + rightMargin: _enableMarginsBottomLeftRight ? _offsetXY.x : 0 } onWidthChanged: { window.appBar.width = width diff --git a/src/Qt6/imports/FluentUI/Controls/FluWindow.qml b/src/Qt6/imports/FluentUI/Controls/FluWindow.qml index 65ed022f..e36354ec 100644 --- a/src/Qt6/imports/FluentUI/Controls/FluWindow.qml +++ b/src/Qt6/imports/FluentUI/Controls/FluWindow.qml @@ -63,6 +63,7 @@ Window { property int _realHeight property int _realWidth property int _appBarHeight: appBar.height + property bool _enableMarginsBottomLeftRight: false id:window color:"transparent" Component.onCompleted: { @@ -208,6 +209,9 @@ Window { anchors{ fill:parent topMargin: _offsetXY.y + bottomMargin: _enableMarginsBottomLeftRight ? _offsetXY.y : 0 + leftMargin: _enableMarginsBottomLeftRight ? _offsetXY.x : 0 + rightMargin: _enableMarginsBottomLeftRight ? _offsetXY.x : 0 } onWidthChanged: { window.appBar.width = width