This commit is contained in:
朱子楚\zhuzi 2024-01-02 22:33:47 +08:00
parent 924ce5d127
commit 284afed52f
4 changed files with 50 additions and 31 deletions

View File

@ -57,10 +57,11 @@ static inline bool isCompositionEnabled(){
}
return composition_enabled;
}
return false;
return true;
}
static inline void showShadow(HWND hwnd){
if(isCompositionEnabled()){
const MARGINS shadow = { 0, 0, 1, 0 };
typedef HRESULT (WINAPI* DwmExtendFrameIntoClientAreaPtr)(HWND hWnd, const MARGINS *pMarInset);
HMODULE module = LoadLibraryW(L"dwmapi.dll");
@ -73,6 +74,10 @@ static inline void showShadow(HWND hwnd){
dwm_extendframe_into_client_area_(hwnd, &shadow);
}
}
}else{
ULONG_PTR cNewStyle = GetClassLongPtr(hwnd, GCL_STYLE) | CS_DROPSHADOW;
SetClassLongPtr(hwnd, GCL_STYLE, cNewStyle);
}
}
#endif
@ -111,7 +116,9 @@ bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void *
}else if(uMsg == WM_NCCALCSIZE){
const auto clientRect = ((wParam == FALSE) ? reinterpret_cast<LPRECT>(lParam) : &(reinterpret_cast<LPNCCALCSIZE_PARAMS>(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;
}
}
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,8 +299,9 @@ 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()){
_enableMarginsBottomLeftRight.write(!isCompositionEnabled());
window->setFlag(Qt::CustomizeWindowHint,true);
_nativeEvent =new FramelessEventFilter(this);
qApp->installNativeEventFilter(_nativeEvent);
@ -298,10 +313,7 @@ void FluFramelessHelper::componentComplete(){
SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_THICKFRAME);
}
SetWindowPos(hwnd,nullptr,0,0,0,0,SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
}else{
window->setFlags((window->flags() & (~Qt::WindowMinMaxButtonsHint) & (~Qt::Dialog)) | Qt::FramelessWindowHint | Qt::Window);
}
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;
}
#endif
window->removeEventFilter(this);
}

View File

@ -61,6 +61,7 @@ private:
QQmlProperty _realHeight;
QQmlProperty _realWidth;
QQmlProperty _appBarHeight;
QQmlProperty _enableMarginsBottomLeftRight;
};
#endif // FLUFRAMELESSHELPER_H

View File

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

View File

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